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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altosuilib_11;
22 import java.awt.event.*;
24 import javax.swing.plaf.basic.*;
26 import java.util.concurrent.*;
27 import org.altusmetrum.altoslib_11.*;
29 public class AltosBTManage extends AltosUIDialog implements ActionListener, Iterable<AltosBTDevice> {
30 LinkedBlockingQueue<AltosBTDevice> found_devices;
32 LinkedList<ActionListener> listeners;
33 AltosBTKnown bt_known;
35 class DeviceList extends JList<AltosBTDevice> implements Iterable<AltosBTDevice> {
36 LinkedList<AltosBTDevice> devices;
37 DefaultListModel<AltosBTDevice> list_model;
39 public void add (AltosBTDevice device) {
40 if (!devices.contains(device)) {
42 list_model.addElement(device);
46 public void remove (AltosBTDevice device) {
47 if (devices.contains(device)) {
48 devices.remove(device);
49 list_model.removeElement(device);
53 public boolean contains(AltosBTDevice device) {
54 return devices.contains(device);
57 //Subclass JList to workaround bug 4832765, which can cause the
58 //scroll pane to not let the user easily scroll up to the beginning
59 //of the list. An alternative would be to set the unitIncrement
60 //of the JScrollBar to a fixed value. You wouldn't get the nice
61 //aligned scrolling, but it should work.
62 public int getScrollableUnitIncrement(Rectangle visibleRect,
66 if (orientation == SwingConstants.VERTICAL &&
67 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
68 Rectangle r = getCellBounds(row, row);
69 if ((r.y == visibleRect.y) && (row != 0)) {
70 Point loc = r.getLocation();
72 int prevIndex = locationToIndex(loc);
73 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
75 if (prevR == null || prevR.y >= r.y) {
81 return super.getScrollableUnitIncrement(
82 visibleRect, orientation, direction);
85 public Iterator<AltosBTDevice> iterator() {
86 return devices.iterator();
89 public java.util.List<AltosBTDevice> selected_list() throws InterruptedException {
90 return getSelectedValuesList();
94 devices = new LinkedList<AltosBTDevice>();
95 list_model = new DefaultListModel<AltosBTDevice>();
97 setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
98 setLayoutOrientation(JList.HORIZONTAL_WRAP);
99 setVisibleRowCount(-1);
103 DeviceList visible_devices;
105 DeviceList known_devices;
108 public Iterator<AltosBTDevice> iterator() {
109 return known_devices.iterator();
112 public void commit() {
116 public void add_known() {
118 for (AltosBTDevice device : visible_devices.selected_list()) {
119 known_devices.add(device);
120 visible_devices.remove(device);
122 } catch (InterruptedException ie) {
126 public void remove_known() {
128 for (AltosBTDevice device : known_devices.selected_list()) {
129 known_devices.remove(device);
130 visible_devices.add(device);
132 } catch (InterruptedException ie) {
136 public void addActionListener(ActionListener l) {
140 private void forwardAction(ActionEvent e) {
141 for (ActionListener l : listeners)
142 l.actionPerformed(e);
145 public void actionPerformed(ActionEvent e) {
146 String command = e.getActionCommand();
147 if ("ok".equals(command)) {
148 bt_thread.interrupt();
152 } else if ("cancel".equals(command)) {
153 bt_thread.interrupt();
156 } else if ("select".equals(command)) {
158 } else if ("deselect".equals(command)) {
163 public void got_visible_device() {
164 while (!found_devices.isEmpty()) {
165 AltosBTDevice device = found_devices.remove();
166 if (!known_devices.contains(device))
167 visible_devices.add(device);
171 class BTGetVisibleDevices implements Runnable {
174 for (int time = 1; time <= 8; time <<= 1) {
175 AltosBTDeviceIterator i = new AltosBTDeviceIterator(time);
176 AltosBTDevice device;
178 if (Thread.interrupted())
181 while ((device = i.next()) != null) {
184 if (Thread.interrupted())
186 found_devices.add(device);
189 got_visible_device();
192 SwingUtilities.invokeLater(r);
194 } catch (Exception e) {
195 System.out.printf("uh-oh, exception %s\n", e.toString());
201 public static void show(Component frameComp, AltosBTKnown known) {
202 Frame frame = JOptionPane.getFrameForComponent(frameComp);
203 AltosBTManage dialog;
205 dialog = new AltosBTManage(frame, known);
206 dialog.setVisible(true);
209 public AltosBTManage(Frame in_frame, AltosBTKnown in_known) {
210 super(in_frame, "Manage Bluetooth Devices", true);
214 BTGetVisibleDevices get_visible_devices = new BTGetVisibleDevices();
215 bt_thread = new Thread(get_visible_devices);
218 listeners = new LinkedList<ActionListener>();
220 found_devices = new LinkedBlockingQueue<AltosBTDevice>();
222 Container pane = getContentPane();
223 pane.setLayout(new GridBagLayout());
225 GridBagConstraints c = new GridBagConstraints();
226 c.insets = new Insets(4,4,4,4);
229 * Known devices label and list
231 c.fill = GridBagConstraints.NONE;
232 c.anchor = GridBagConstraints.WEST;
239 pane.add(new JLabel("Known Devices"), c);
241 known_devices = new DeviceList();
242 for (AltosBTDevice device : bt_known)
243 known_devices.add(device);
245 JScrollPane known_list_scroller = new JScrollPane(known_devices);
246 known_list_scroller.setPreferredSize(new Dimension(400, 80));
247 known_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
248 c.fill = GridBagConstraints.BOTH;
249 c.anchor = GridBagConstraints.WEST;
256 pane.add(known_list_scroller, c);
259 * Visible devices label and list
261 c.fill = GridBagConstraints.NONE;
262 c.anchor = GridBagConstraints.WEST;
270 pane.add(new JLabel("Visible Devices"), c);
272 visible_devices = new DeviceList();
273 JScrollPane visible_list_scroller = new JScrollPane(visible_devices);
274 visible_list_scroller.setPreferredSize(new Dimension(400, 80));
275 visible_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
276 c.fill = GridBagConstraints.BOTH;
277 c.anchor = GridBagConstraints.WEST;
284 pane.add(visible_list_scroller, c);
287 * Arrows between the two lists
289 BasicArrowButton select_arrow = new BasicArrowButton(SwingConstants.WEST);
290 select_arrow.setActionCommand("select");
291 select_arrow.addActionListener(this);
292 c.fill = GridBagConstraints.NONE;
293 c.anchor = GridBagConstraints.SOUTH;
300 pane.add(select_arrow, c);
302 BasicArrowButton deselect_arrow = new BasicArrowButton(SwingConstants.EAST);
303 deselect_arrow.setActionCommand("deselect");
304 deselect_arrow.addActionListener(this);
305 c.fill = GridBagConstraints.NONE;
306 c.anchor = GridBagConstraints.NORTH;
313 pane.add(deselect_arrow, c);
315 JButton cancel_button = new JButton("Cancel");
316 cancel_button.setActionCommand("cancel");
317 cancel_button.addActionListener(this);
318 c.fill = GridBagConstraints.NONE;
319 c.anchor = GridBagConstraints.CENTER;
326 pane.add(cancel_button, c);
328 JButton ok_button = new JButton("OK");
329 ok_button.setActionCommand("ok");
330 ok_button.addActionListener(this);
331 c.fill = GridBagConstraints.NONE;
332 c.anchor = GridBagConstraints.CENTER;
339 pane.add(ok_button, c);
341 getRootPane().setDefaultButton(ok_button);
344 setLocationRelativeTo(frame);
345 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
346 addWindowListener(new WindowAdapter() {
348 public void windowClosing(WindowEvent e) {
349 bt_thread.interrupt();