2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import javax.swing.event.*;
26 import javax.swing.plaf.basic.*;
30 import java.util.prefs.*;
31 import java.util.concurrent.*;
32 import org.altusmetrum.AltosLib.*;
36 public class AltosBTManage extends AltosDialog implements ActionListener, Iterable<AltosBTDevice> {
37 LinkedBlockingQueue<AltosBTDevice> found_devices;
39 LinkedList<ActionListener> listeners;
40 AltosBTKnown bt_known;
42 class DeviceList extends JList implements Iterable<AltosBTDevice> {
43 LinkedList<AltosBTDevice> devices;
44 DefaultListModel list_model;
46 public void add (AltosBTDevice device) {
47 if (!devices.contains(device)) {
49 list_model.addElement(device);
53 public void remove (AltosBTDevice device) {
54 if (devices.contains(device)) {
55 devices.remove(device);
56 list_model.removeElement(device);
60 public boolean contains(AltosBTDevice device) {
61 return devices.contains(device);
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,
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();
79 int prevIndex = locationToIndex(loc);
80 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
82 if (prevR == null || prevR.y >= r.y) {
88 return super.getScrollableUnitIncrement(
89 visibleRect, orientation, direction);
92 public Iterator<AltosBTDevice> iterator() {
93 return devices.iterator();
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]);
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);
114 DeviceList visible_devices;
116 DeviceList known_devices;
119 public Iterator<AltosBTDevice> iterator() {
120 return known_devices.iterator();
123 public void commit() {
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);
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);
143 public void addActionListener(ActionListener l) {
147 private void forwardAction(ActionEvent e) {
148 for (ActionListener l : listeners)
149 l.actionPerformed(e);
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();
160 } else if ("cancel".equals(command)) {
161 bt_thread.interrupt();
164 } else if ("select".equals(command)) {
166 } else if ("deselect".equals(command)) {
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);
179 class BTGetVisibleDevices implements Runnable {
182 for (int time = 1; time <= 8; time <<= 1) {
183 AltosBTDeviceIterator i = new AltosBTDeviceIterator(time);
184 AltosBTDevice device;
186 if (Thread.interrupted())
189 while ((device = i.next()) != null) {
192 if (Thread.interrupted())
194 found_devices.add(device);
197 got_visible_device();
200 SwingUtilities.invokeLater(r);
202 } catch (Exception e) {
203 System.out.printf("uh-oh, exception %s\n", e.toString());
209 public static void show(Component frameComp, AltosBTKnown known) {
210 Frame frame = JOptionPane.getFrameForComponent(frameComp);
211 AltosBTManage dialog;
213 dialog = new AltosBTManage(frame, known);
214 dialog.setVisible(true);
217 public AltosBTManage(Frame in_frame, AltosBTKnown in_known) {
218 super(in_frame, "Manage Bluetooth Devices", true);
222 BTGetVisibleDevices get_visible_devices = new BTGetVisibleDevices();
223 bt_thread = new Thread(get_visible_devices);
226 listeners = new LinkedList<ActionListener>();
228 found_devices = new LinkedBlockingQueue<AltosBTDevice>();
230 Container pane = getContentPane();
231 pane.setLayout(new GridBagLayout());
233 GridBagConstraints c = new GridBagConstraints();
234 c.insets = new Insets(4,4,4,4);
237 * Known devices label and list
239 c.fill = GridBagConstraints.NONE;
240 c.anchor = GridBagConstraints.WEST;
247 pane.add(new JLabel("Known Devices"), c);
249 known_devices = new DeviceList();
250 for (AltosBTDevice device : bt_known)
251 known_devices.add(device);
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;
264 pane.add(known_list_scroller, c);
267 * Visible devices label and list
269 c.fill = GridBagConstraints.NONE;
270 c.anchor = GridBagConstraints.WEST;
278 pane.add(new JLabel("Visible Devices"), c);
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;
292 pane.add(visible_list_scroller, c);
295 * Arrows between the two lists
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;
308 pane.add(select_arrow, c);
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;
321 pane.add(deselect_arrow, c);
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;
334 pane.add(cancel_button, c);
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;
347 pane.add(ok_button, c);
349 getRootPane().setDefaultButton(ok_button);
352 setLocationRelativeTo(frame);
353 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
354 addWindowListener(new WindowAdapter() {
356 public void windowClosing(WindowEvent e) {
357 bt_thread.interrupt();