altosui: Use persistent list of bluetooth devices for device dialogs
[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                 pane.add(new JLabel("Known Devices"), c);
245
246                 known_devices = new DeviceList();
247                 for (AltosBTDevice device : bt_known)
248                         known_devices.add(device);
249
250                 JScrollPane known_list_scroller = new JScrollPane(known_devices);
251                 known_list_scroller.setPreferredSize(new Dimension(400, 80));
252                 known_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
253                 c.fill = GridBagConstraints.BOTH;
254                 c.anchor = GridBagConstraints.WEST;
255                 c.gridx = 0;
256                 c.gridy = 1;
257                 c.gridwidth = 1;
258                 c.gridheight = 2;
259                 pane.add(known_list_scroller, c);
260
261                 /*
262                  * Visible devices label and list
263                  */
264                 c.fill = GridBagConstraints.NONE;
265                 c.anchor = GridBagConstraints.WEST;
266                 c.gridx = 2;
267                 c.gridy = 0;
268                 c.gridwidth = 1;
269                 c.gridheight = 1;
270                 pane.add(new JLabel("Visible Devices"), c);
271
272                 visible_devices = new DeviceList();
273                 JScrollPane visible_list_scroller = new JScrollPane(visible_devices);
274                 visible_list_scroller.setPreferredSize(new Dimension(400, 80));
275                 visible_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
276                 c.fill = GridBagConstraints.BOTH;
277                 c.anchor = GridBagConstraints.WEST;
278                 c.gridx = 2;
279                 c.gridy = 1;
280                 c.gridheight = 2;
281                 c.gridwidth = 1;
282                 pane.add(visible_list_scroller, c);
283
284                 /*
285                  * Arrows between the two lists
286                  */
287                 BasicArrowButton select_arrow = new BasicArrowButton(SwingConstants.WEST);
288                 select_arrow.setActionCommand("select");
289                 select_arrow.addActionListener(this);
290                 c.fill = GridBagConstraints.NONE;
291                 c.anchor = GridBagConstraints.SOUTH;
292                 c.gridx = 1;
293                 c.gridy = 1;
294                 c.gridheight = 1;
295                 c.gridwidth = 1;
296                 pane.add(select_arrow, c);
297
298                 BasicArrowButton deselect_arrow = new BasicArrowButton(SwingConstants.EAST);
299                 deselect_arrow.setActionCommand("deselect");
300                 deselect_arrow.addActionListener(this);
301                 c.fill = GridBagConstraints.NONE;
302                 c.anchor = GridBagConstraints.NORTH;
303                 c.gridx = 1;
304                 c.gridy = 2;
305                 c.gridheight = 1;
306                 c.gridwidth = 1;
307                 pane.add(deselect_arrow, c);
308
309                 JButton cancel_button = new JButton("Cancel");
310                 cancel_button.setActionCommand("cancel");
311                 cancel_button.addActionListener(this);
312                 c.fill = GridBagConstraints.NONE;
313                 c.anchor = GridBagConstraints.CENTER;
314                 c.gridx = 0;
315                 c.gridy = 3;
316                 c.gridheight = 1;
317                 c.gridwidth = 1;
318                 pane.add(cancel_button, c);
319
320                 JButton ok_button = new JButton("OK");
321                 ok_button.setActionCommand("ok");
322                 ok_button.addActionListener(this);
323                 c.fill = GridBagConstraints.NONE;
324                 c.anchor = GridBagConstraints.CENTER;
325                 c.gridx = 2;
326                 c.gridy = 3;
327                 c.gridheight = 1;
328                 c.gridwidth = 1;
329                 pane.add(ok_button, c);
330
331                 getRootPane().setDefaultButton(ok_button);
332
333                 pack();
334                 setLocationRelativeTo(frame);
335                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
336                 addWindowListener(new WindowAdapter() {
337                         @Override
338                         public void windowClosing(WindowEvent e) {
339                                 bt_thread.interrupt();
340                                 setVisible(false);
341                         }
342                 });
343         }
344 }