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