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