altosui: Fix BT manage dialog so that the device lists resize
[fw/altos] / altosui / AltosBTManage.java
1 /*
2  * Copyright © 2011 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.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import javax.swing.event.*;
26 import javax.swing.plaf.basic.*;
27 import java.io.*;
28 import java.util.*;
29 import java.text.*;
30 import java.util.prefs.*;
31 import java.util.concurrent.*;
32
33 import libaltosJNI.*;
34
35 public class AltosBTManage extends JDialog implements ActionListener, Iterable<AltosBTDevice> {
36         LinkedBlockingQueue<AltosBTDevice> found_devices;
37         Frame frame;
38         LinkedList<ActionListener> listeners;
39         AltosBTKnown    bt_known;
40
41         class DeviceList extends JList implements Iterable<AltosBTDevice> {
42                 LinkedList<AltosBTDevice> devices;
43                 DefaultListModel        list_model;
44
45                 public void add (AltosBTDevice device) {
46                         if (!devices.contains(device)) {
47                                 devices.add(device);
48                                 list_model.addElement(device);
49                         }
50                 }
51
52                 public void remove (AltosBTDevice device) {
53                         if (devices.contains(device)) {
54                                 devices.remove(device);
55                                 list_model.removeElement(device);
56                         }
57                 }
58
59                 public boolean contains(AltosBTDevice device) {
60                         return devices.contains(device);
61                 }
62
63                 //Subclass JList to workaround bug 4832765, which can cause the
64                 //scroll pane to not let the user easily scroll up to the beginning
65                 //of the list.  An alternative would be to set the unitIncrement
66                 //of the JScrollBar to a fixed value. You wouldn't get the nice
67                 //aligned scrolling, but it should work.
68                 public int getScrollableUnitIncrement(Rectangle visibleRect,
69                                                       int orientation,
70                                                       int direction) {
71                         int row;
72                         if (orientation == SwingConstants.VERTICAL &&
73                             direction < 0 && (row = getFirstVisibleIndex()) != -1) {
74                                 Rectangle r = getCellBounds(row, row);
75                                 if ((r.y == visibleRect.y) && (row != 0))  {
76                                         Point loc = r.getLocation();
77                                         loc.y--;
78                                         int prevIndex = locationToIndex(loc);
79                                         Rectangle prevR = getCellBounds(prevIndex, prevIndex);
80
81                                         if (prevR == null || prevR.y >= r.y) {
82                                                 return 0;
83                                         }
84                                         return prevR.height;
85                                 }
86                         }
87                         return super.getScrollableUnitIncrement(
88                                 visibleRect, orientation, direction);
89                 }
90
91                 public Iterator<AltosBTDevice> iterator() {
92                         return devices.iterator();
93                 }
94
95                 public java.util.List<AltosBTDevice> selected_list() {
96                         java.util.LinkedList<AltosBTDevice> l = new java.util.LinkedList<AltosBTDevice>();
97                         Object[] a = getSelectedValues();
98                         for (int i = 0; i < a.length; i++)
99                                 l.add((AltosBTDevice)a[i]);
100                         return l;
101                 }
102
103                 public DeviceList() {
104                         devices = new LinkedList<AltosBTDevice>();
105                         list_model = new DefaultListModel();
106                         setModel(list_model);
107                         setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
108                         setLayoutOrientation(JList.HORIZONTAL_WRAP);
109                         setVisibleRowCount(-1);
110                 }
111         }
112
113         DeviceList      visible_devices;
114
115         DeviceList      known_devices;
116         Thread          bt_thread;
117
118         public Iterator<AltosBTDevice> iterator() {
119                 return known_devices.iterator();
120         }
121
122         public void commit() {
123                 bt_known.set(this);
124         }
125
126         public void add_known() {
127                 for (AltosBTDevice device : visible_devices.selected_list()) {
128                         System.out.printf("Add known %s\n", device.toString());
129                         known_devices.add(device);
130                         visible_devices.remove(device);
131                 }
132         }
133
134         public void remove_known() {
135                 for (AltosBTDevice device : known_devices.selected_list()) {
136                         System.out.printf("Remove known %s\n", device.toString());
137                         known_devices.remove(device);
138                         visible_devices.add(device);
139                 }
140         }
141
142         public void addActionListener(ActionListener l) {
143                 listeners.add(l);
144         }
145
146         private void forwardAction(ActionEvent e) {
147                 for (ActionListener l : listeners)
148                         l.actionPerformed(e);
149         }
150
151         public void actionPerformed(ActionEvent e) {
152                 String  command = e.getActionCommand();
153                 System.out.printf("manage command %s\n", command);
154                 if ("ok".equals(command)) {
155                         bt_thread.interrupt();
156                         commit();
157                         setVisible(false);
158                         forwardAction(e);
159                 } else if ("cancel".equals(command)) {
160                         bt_thread.interrupt();
161                         setVisible(false);
162                         forwardAction(e);
163                 } else if ("select".equals(command)) {
164                         add_known();
165                 } else if ("deselect".equals(command)) {
166                         remove_known();
167                 }
168         }
169
170         public void got_visible_device() {
171                 while (!found_devices.isEmpty()) {
172                         AltosBTDevice   device = found_devices.remove();
173                         if (!known_devices.contains(device))
174                                 visible_devices.add(device);
175                 }
176         }
177
178         class BTGetVisibleDevices implements Runnable {
179                 public void run () {
180                         for (;;)
181                                 for (int time = 1; time <= 8; time <<= 1) {
182                                         AltosBTDeviceIterator   i = new AltosBTDeviceIterator(time);
183                                         AltosBTDevice           device;
184
185                                         if (Thread.interrupted())
186                                                 return;
187                                         try {
188                                                 while ((device = i.next()) != null) {
189                                                         Runnable r;
190
191                                                         if (Thread.interrupted())
192                                                                 return;
193                                                         found_devices.add(device);
194                                                         r = new Runnable() {
195                                                                         public void run() {
196                                                                                 got_visible_device();
197                                                                         }
198                                                                 };
199                                                         SwingUtilities.invokeLater(r);
200                                                 }
201                                         } catch (Exception e) {
202                                                 System.out.printf("uh-oh, exception %s\n", e.toString());
203                                         }
204                                 }
205                 }
206         }
207
208         public static void show(Component frameComp, AltosBTKnown known) {
209                 Frame   frame = JOptionPane.getFrameForComponent(frameComp);
210                 AltosBTManage   dialog;
211
212                 dialog = new AltosBTManage(frame, known);
213                 dialog.setVisible(true);
214         }
215
216         public AltosBTManage(Frame in_frame, AltosBTKnown in_known) {
217                 super(in_frame, "Manage Bluetooth Devices", true);
218
219                 frame = in_frame;
220                 bt_known = in_known;
221                 BTGetVisibleDevices     get_visible_devices = new BTGetVisibleDevices();
222                 bt_thread = new Thread(get_visible_devices);
223                 bt_thread.start();
224
225                 listeners = new LinkedList<ActionListener>();
226
227                 found_devices = new LinkedBlockingQueue<AltosBTDevice>();
228
229                 Container pane = getContentPane();
230                 pane.setLayout(new GridBagLayout());
231
232                 GridBagConstraints c = new GridBagConstraints();
233                 c.insets = new Insets(4,4,4,4);
234
235                 /*
236                  * Known devices label and list
237                  */
238                 c.fill = GridBagConstraints.NONE;
239                 c.anchor = GridBagConstraints.WEST;
240                 c.gridx = 0;
241                 c.gridy = 0;
242                 c.gridwidth = 1;
243                 c.gridheight = 1;
244                 c.weightx = 0;
245                 c.weighty = 0;
246                 pane.add(new JLabel("Known Devices"), c);
247
248                 known_devices = new DeviceList();
249                 for (AltosBTDevice device : bt_known)
250                         known_devices.add(device);
251
252                 JScrollPane known_list_scroller = new JScrollPane(known_devices);
253                 known_list_scroller.setPreferredSize(new Dimension(400, 80));
254                 known_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
255                 c.fill = GridBagConstraints.BOTH;
256                 c.anchor = GridBagConstraints.WEST;
257                 c.gridx = 0;
258                 c.gridy = 1;
259                 c.gridwidth = 1;
260                 c.gridheight = 2;
261                 c.weightx = 1;
262                 c.weighty = 1;
263                 pane.add(known_list_scroller, c);
264
265                 /*
266                  * Visible devices label and list
267                  */
268                 c.fill = GridBagConstraints.NONE;
269                 c.anchor = GridBagConstraints.WEST;
270                 c.gridx = 2;
271                 c.gridy = 0;
272                 c.gridwidth = 1;
273                 c.gridheight = 1;
274                 c.weightx = 0;
275                 c.weighty = 0;
276
277                 pane.add(new JLabel("Visible Devices"), c);
278
279                 visible_devices = new DeviceList();
280                 JScrollPane visible_list_scroller = new JScrollPane(visible_devices);
281                 visible_list_scroller.setPreferredSize(new Dimension(400, 80));
282                 visible_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
283                 c.fill = GridBagConstraints.BOTH;
284                 c.anchor = GridBagConstraints.WEST;
285                 c.gridx = 2;
286                 c.gridy = 1;
287                 c.gridheight = 2;
288                 c.gridwidth = 1;
289                 c.weightx = 1;
290                 c.weighty = 1;
291                 pane.add(visible_list_scroller, c);
292
293                 /*
294                  * Arrows between the two lists
295                  */
296                 BasicArrowButton select_arrow = new BasicArrowButton(SwingConstants.WEST);
297                 select_arrow.setActionCommand("select");
298                 select_arrow.addActionListener(this);
299                 c.fill = GridBagConstraints.NONE;
300                 c.anchor = GridBagConstraints.SOUTH;
301                 c.gridx = 1;
302                 c.gridy = 1;
303                 c.gridheight = 1;
304                 c.gridwidth = 1;
305                 c.weightx = 0;
306                 c.weighty = 0;
307                 pane.add(select_arrow, c);
308
309                 BasicArrowButton deselect_arrow = new BasicArrowButton(SwingConstants.EAST);
310                 deselect_arrow.setActionCommand("deselect");
311                 deselect_arrow.addActionListener(this);
312                 c.fill = GridBagConstraints.NONE;
313                 c.anchor = GridBagConstraints.NORTH;
314                 c.gridx = 1;
315                 c.gridy = 2;
316                 c.gridheight = 1;
317                 c.gridwidth = 1;
318                 c.weightx = 0;
319                 c.weighty = 0;
320                 pane.add(deselect_arrow, c);
321
322                 JButton cancel_button = new JButton("Cancel");
323                 cancel_button.setActionCommand("cancel");
324                 cancel_button.addActionListener(this);
325                 c.fill = GridBagConstraints.NONE;
326                 c.anchor = GridBagConstraints.CENTER;
327                 c.gridx = 0;
328                 c.gridy = 3;
329                 c.gridheight = 1;
330                 c.gridwidth = 1;
331                 c.weightx = 0;
332                 c.weighty = 0;
333                 pane.add(cancel_button, c);
334
335                 JButton ok_button = new JButton("OK");
336                 ok_button.setActionCommand("ok");
337                 ok_button.addActionListener(this);
338                 c.fill = GridBagConstraints.NONE;
339                 c.anchor = GridBagConstraints.CENTER;
340                 c.gridx = 2;
341                 c.gridy = 3;
342                 c.gridheight = 1;
343                 c.gridwidth = 1;
344                 c.weightx = 0;
345                 c.weighty = 0;
346                 pane.add(ok_button, c);
347
348                 getRootPane().setDefaultButton(ok_button);
349
350                 pack();
351                 setLocationRelativeTo(frame);
352                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
353                 addWindowListener(new WindowAdapter() {
354                         @Override
355                         public void windowClosing(WindowEvent e) {
356                                 bt_thread.interrupt();
357                                 setVisible(false);
358                         }
359                 });
360         }
361 }