7ab08b30aeba264373e90092e1bd33be9d13714c
[fw/altos] / altosui / AltosDeviceDialog.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package AltosUI;
19
20 import java.lang.*;
21 import java.util.*;
22 import javax.swing.*;
23 import java.awt.*;
24 import java.awt.event.*;
25 import libaltosJNI.*;
26
27 public class AltosDeviceDialog extends AltosDialog implements ActionListener {
28
29         private AltosDevice     value;
30         private JList           list;
31         private JButton         cancel_button;
32         private JButton         select_button;
33         private JButton         manage_bluetooth_button;
34         private Frame           frame;
35         private int             product;
36
37         private AltosDevice getValue() {
38                 return value;
39         }
40
41         private AltosDevice[] devices() {
42                 java.util.List<AltosDevice>     usb_devices = AltosUSBDevice.list(product);
43                 int                             num_devices = usb_devices.size();
44                 java.util.List<AltosDevice>     bt_devices = AltosBTKnown.bt_known().list(product);
45                 num_devices += bt_devices.size();
46                 AltosDevice[]                   devices = new AltosDevice[num_devices];
47
48                 for (int i = 0; i < usb_devices.size(); i++)
49                         devices[i] = usb_devices.get(i);
50                 int off = usb_devices.size();
51                 for (int j = 0; j < bt_devices.size(); j++)
52                         devices[off + j] = bt_devices.get(j);
53                 return devices;
54         }
55
56         private void update_devices() {
57                 AltosDevice[] devices = devices();
58                 list.setListData(devices);
59                 select_button.setEnabled(devices.length > 0);
60         }
61
62         private AltosDeviceDialog (Frame in_frame, Component location, int in_product) {
63                 super(in_frame, "Device Selection", true);
64
65                 product = in_product;
66                 frame = in_frame;
67                 value = null;
68
69                 AltosDevice[]   devices = devices();
70
71                 cancel_button = new JButton("Cancel");
72                 cancel_button.setActionCommand("cancel");
73                 cancel_button.addActionListener(this);
74
75                 manage_bluetooth_button = new JButton("Manage Bluetooth");
76                 manage_bluetooth_button.setActionCommand("manage");
77                 manage_bluetooth_button.addActionListener(this);
78
79                 select_button = new JButton("Select");
80                 select_button.setActionCommand("select");
81                 select_button.addActionListener(this);
82                 if (devices.length == 0)
83                         select_button.setEnabled(false);
84                 getRootPane().setDefaultButton(select_button);
85
86                 list = new JList(devices) {
87                                 //Subclass JList to workaround bug 4832765, which can cause the
88                                 //scroll pane to not let the user easily scroll up to the beginning
89                                 //of the list.  An alternative would be to set the unitIncrement
90                                 //of the JScrollBar to a fixed value. You wouldn't get the nice
91                                 //aligned scrolling, but it should work.
92                                 public int getScrollableUnitIncrement(Rectangle visibleRect,
93                                                                       int orientation,
94                                                                       int direction) {
95                                         int row;
96                                         if (orientation == SwingConstants.VERTICAL &&
97                                             direction < 0 && (row = getFirstVisibleIndex()) != -1) {
98                                                 Rectangle r = getCellBounds(row, row);
99                                                 if ((r.y == visibleRect.y) && (row != 0))  {
100                                                         Point loc = r.getLocation();
101                                                         loc.y--;
102                                                         int prevIndex = locationToIndex(loc);
103                                                         Rectangle prevR = getCellBounds(prevIndex, prevIndex);
104
105                                                         if (prevR == null || prevR.y >= r.y) {
106                                                                 return 0;
107                                                         }
108                                                         return prevR.height;
109                                                 }
110                                         }
111                                         return super.getScrollableUnitIncrement(
112                                                 visibleRect, orientation, direction);
113                                 }
114                         };
115
116                 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
117                 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
118                 list.setVisibleRowCount(-1);
119                 list.addMouseListener(new MouseAdapter() {
120                                  public void mouseClicked(MouseEvent e) {
121                                          if (e.getClickCount() == 2) {
122                                                  select_button.doClick(); //emulate button click
123                                          }
124                                  }
125                         });
126                 JScrollPane listScroller = new JScrollPane(list);
127                 listScroller.setPreferredSize(new Dimension(400, 80));
128                 listScroller.setAlignmentX(LEFT_ALIGNMENT);
129
130                 //Create a container so that we can add a title around
131                 //the scroll pane.  Can't add a title directly to the
132                 //scroll pane because its background would be white.
133                 //Lay out the label and scroll pane from top to bottom.
134                 JPanel listPane = new JPanel();
135                 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
136
137                 JLabel label = new JLabel("Select Device");
138                 label.setLabelFor(list);
139                 listPane.add(label);
140                 listPane.add(Box.createRigidArea(new Dimension(0,5)));
141                 listPane.add(listScroller);
142                 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
143
144                 //Lay out the buttons from left to right.
145                 JPanel buttonPane = new JPanel();
146                 buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
147                 buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
148                 buttonPane.add(Box.createHorizontalGlue());
149                 buttonPane.add(cancel_button);
150                 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
151                 buttonPane.add(manage_bluetooth_button);
152                 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
153                 buttonPane.add(select_button);
154
155                 //Put everything together, using the content pane's BorderLayout.
156                 Container contentPane = getContentPane();
157                 contentPane.add(listPane, BorderLayout.CENTER);
158                 contentPane.add(buttonPane, BorderLayout.PAGE_END);
159
160                 //Initialize values.
161                 if (devices != null && devices.length != 0)
162                         list.setSelectedValue(devices[0], true);
163                 pack();
164                 setLocationRelativeTo(location);
165         }
166
167         //Handle clicks on the Set and Cancel buttons.
168         public void actionPerformed(ActionEvent e) {
169                 if ("select".equals(e.getActionCommand()))
170                         value = (AltosDevice)(list.getSelectedValue());
171                 if ("manage".equals(e.getActionCommand())) {
172                         AltosBTManage.show(frame, AltosBTKnown.bt_known());
173                         update_devices();
174                         return;
175                 }
176                 setVisible(false);
177         }
178
179         public static AltosDevice show (Component frameComp, int product) {
180
181                 Frame                           frame = JOptionPane.getFrameForComponent(frameComp);
182                 AltosDeviceDialog       dialog;
183
184                 dialog = new AltosDeviceDialog(frame, frameComp, product);
185                 dialog.setVisible(true);
186                 return dialog.getValue();
187         }
188 }