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