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