altos: Use installed pdclib
[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() {
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                 for (AltosBTDevice device : visible_devices.selected_list()) {
121                         known_devices.add(device);
122                         visible_devices.remove(device);
123                 }
124         }
125
126         public void remove_known() {
127                 for (AltosBTDevice device : known_devices.selected_list()) {
128                         known_devices.remove(device);
129                         visible_devices.add(device);
130                 }
131         }
132
133         public void addActionListener(ActionListener l) {
134                 listeners.add(l);
135         }
136
137         private void forwardAction(ActionEvent e) {
138                 for (ActionListener l : listeners)
139                         l.actionPerformed(e);
140         }
141
142         public void actionPerformed(ActionEvent e) {
143                 String  command = e.getActionCommand();
144                 if ("ok".equals(command)) {
145                         bt_thread.interrupt();
146                         commit();
147                         setVisible(false);
148                         forwardAction(e);
149                 } else if ("cancel".equals(command)) {
150                         bt_thread.interrupt();
151                         setVisible(false);
152                         forwardAction(e);
153                 } else if ("select".equals(command)) {
154                         add_known();
155                 } else if ("deselect".equals(command)) {
156                         remove_known();
157                 }
158         }
159
160         public void got_visible_device() {
161                 while (!found_devices.isEmpty()) {
162                         AltosBTDevice   device = found_devices.remove();
163                         if (!known_devices.contains(device))
164                                 visible_devices.add(device);
165                 }
166         }
167
168         class BTGetVisibleDevices implements Runnable {
169                 public void run () {
170                         for (;;)
171                                 for (int time = 1; time <= 8; time <<= 1) {
172                                         AltosBTDeviceIterator   i = new AltosBTDeviceIterator(time);
173                                         AltosBTDevice           device;
174
175                                         if (Thread.interrupted())
176                                                 return;
177                                         try {
178                                                 while ((device = i.next()) != null) {
179                                                         Runnable r;
180
181                                                         if (Thread.interrupted())
182                                                                 return;
183                                                         found_devices.add(device);
184                                                         r = new Runnable() {
185                                                                         public void run() {
186                                                                                 got_visible_device();
187                                                                         }
188                                                                 };
189                                                         SwingUtilities.invokeLater(r);
190                                                 }
191                                         } catch (Exception e) {
192                                                 System.out.printf("uh-oh, exception %s\n", e.toString());
193                                         }
194                                 }
195                 }
196         }
197
198         public static void show(Component frameComp, AltosBTKnown known) {
199                 Frame   frame = JOptionPane.getFrameForComponent(frameComp);
200                 AltosBTManage   dialog;
201
202                 dialog = new AltosBTManage(frame, known);
203                 dialog.setVisible(true);
204         }
205
206         public AltosBTManage(Frame in_frame, AltosBTKnown in_known) {
207                 super(in_frame, "Manage Bluetooth Devices", true);
208
209                 frame = in_frame;
210                 bt_known = in_known;
211                 BTGetVisibleDevices     get_visible_devices = new BTGetVisibleDevices();
212                 bt_thread = new Thread(get_visible_devices);
213                 bt_thread.start();
214
215                 listeners = new LinkedList<ActionListener>();
216
217                 found_devices = new LinkedBlockingQueue<AltosBTDevice>();
218
219                 Container pane = getContentPane();
220                 pane.setLayout(new GridBagLayout());
221
222                 GridBagConstraints c = new GridBagConstraints();
223                 c.insets = new Insets(4,4,4,4);
224
225                 /*
226                  * Known devices label and list
227                  */
228                 c.fill = GridBagConstraints.NONE;
229                 c.anchor = GridBagConstraints.WEST;
230                 c.gridx = 0;
231                 c.gridy = 0;
232                 c.gridwidth = 1;
233                 c.gridheight = 1;
234                 c.weightx = 0;
235                 c.weighty = 0;
236                 pane.add(new JLabel("Known Devices"), c);
237
238                 known_devices = new DeviceList();
239                 for (AltosBTDevice device : bt_known)
240                         known_devices.add(device);
241
242                 JScrollPane known_list_scroller = new JScrollPane(known_devices);
243                 known_list_scroller.setPreferredSize(new Dimension(400, 80));
244                 known_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
245                 c.fill = GridBagConstraints.BOTH;
246                 c.anchor = GridBagConstraints.WEST;
247                 c.gridx = 0;
248                 c.gridy = 1;
249                 c.gridwidth = 1;
250                 c.gridheight = 2;
251                 c.weightx = 1;
252                 c.weighty = 1;
253                 pane.add(known_list_scroller, c);
254
255                 /*
256                  * Visible devices label and list
257                  */
258                 c.fill = GridBagConstraints.NONE;
259                 c.anchor = GridBagConstraints.WEST;
260                 c.gridx = 2;
261                 c.gridy = 0;
262                 c.gridwidth = 1;
263                 c.gridheight = 1;
264                 c.weightx = 0;
265                 c.weighty = 0;
266
267                 pane.add(new JLabel("Visible Devices"), c);
268
269                 visible_devices = new DeviceList();
270                 JScrollPane visible_list_scroller = new JScrollPane(visible_devices);
271                 visible_list_scroller.setPreferredSize(new Dimension(400, 80));
272                 visible_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
273                 c.fill = GridBagConstraints.BOTH;
274                 c.anchor = GridBagConstraints.WEST;
275                 c.gridx = 2;
276                 c.gridy = 1;
277                 c.gridheight = 2;
278                 c.gridwidth = 1;
279                 c.weightx = 1;
280                 c.weighty = 1;
281                 pane.add(visible_list_scroller, c);
282
283                 /*
284                  * Arrows between the two lists
285                  */
286                 BasicArrowButton select_arrow = new BasicArrowButton(SwingConstants.WEST);
287                 select_arrow.setActionCommand("select");
288                 select_arrow.addActionListener(this);
289                 c.fill = GridBagConstraints.NONE;
290                 c.anchor = GridBagConstraints.SOUTH;
291                 c.gridx = 1;
292                 c.gridy = 1;
293                 c.gridheight = 1;
294                 c.gridwidth = 1;
295                 c.weightx = 0;
296                 c.weighty = 0;
297                 pane.add(select_arrow, c);
298
299                 BasicArrowButton deselect_arrow = new BasicArrowButton(SwingConstants.EAST);
300                 deselect_arrow.setActionCommand("deselect");
301                 deselect_arrow.addActionListener(this);
302                 c.fill = GridBagConstraints.NONE;
303                 c.anchor = GridBagConstraints.NORTH;
304                 c.gridx = 1;
305                 c.gridy = 2;
306                 c.gridheight = 1;
307                 c.gridwidth = 1;
308                 c.weightx = 0;
309                 c.weighty = 0;
310                 pane.add(deselect_arrow, c);
311
312                 JButton cancel_button = new JButton("Cancel");
313                 cancel_button.setActionCommand("cancel");
314                 cancel_button.addActionListener(this);
315                 c.fill = GridBagConstraints.NONE;
316                 c.anchor = GridBagConstraints.CENTER;
317                 c.gridx = 0;
318                 c.gridy = 3;
319                 c.gridheight = 1;
320                 c.gridwidth = 1;
321                 c.weightx = 0;
322                 c.weighty = 0;
323                 pane.add(cancel_button, c);
324
325                 JButton ok_button = new JButton("OK");
326                 ok_button.setActionCommand("ok");
327                 ok_button.addActionListener(this);
328                 c.fill = GridBagConstraints.NONE;
329                 c.anchor = GridBagConstraints.CENTER;
330                 c.gridx = 2;
331                 c.gridy = 3;
332                 c.gridheight = 1;
333                 c.gridwidth = 1;
334                 c.weightx = 0;
335                 c.weighty = 0;
336                 pane.add(ok_button, c);
337
338                 getRootPane().setDefaultButton(ok_button);
339
340                 pack();
341                 setLocationRelativeTo(frame);
342                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
343                 addWindowListener(new WindowAdapter() {
344                         @Override
345                         public void windowClosing(WindowEvent e) {
346                                 bt_thread.interrupt();
347                                 setVisible(false);
348                         }
349                 });
350         }
351 }