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