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.
18 package org.altusmetrum.altosuilib_9;
21 import java.awt.event.*;
23 import javax.swing.plaf.basic.*;
25 import java.util.concurrent.*;
26 import org.altusmetrum.altoslib_9.*;
28 public class AltosBTManage extends AltosUIDialog implements ActionListener, Iterable<AltosBTDevice> {
29 LinkedBlockingQueue<AltosBTDevice> found_devices;
31 LinkedList<ActionListener> listeners;
32 AltosBTKnown bt_known;
34 class DeviceList extends JList<AltosBTDevice> implements Iterable<AltosBTDevice> {
35 LinkedList<AltosBTDevice> devices;
36 DefaultListModel<AltosBTDevice> list_model;
38 public void add (AltosBTDevice device) {
39 if (!devices.contains(device)) {
41 list_model.addElement(device);
45 public void remove (AltosBTDevice device) {
46 if (devices.contains(device)) {
47 devices.remove(device);
48 list_model.removeElement(device);
52 public boolean contains(AltosBTDevice device) {
53 return devices.contains(device);
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,
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();
71 int prevIndex = locationToIndex(loc);
72 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
74 if (prevR == null || prevR.y >= r.y) {
80 return super.getScrollableUnitIncrement(
81 visibleRect, orientation, direction);
84 public Iterator<AltosBTDevice> iterator() {
85 return devices.iterator();
88 public java.util.List<AltosBTDevice> selected_list() throws InterruptedException {
89 return getSelectedValuesList();
93 devices = new LinkedList<AltosBTDevice>();
94 list_model = new DefaultListModel<AltosBTDevice>();
96 setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
97 setLayoutOrientation(JList.HORIZONTAL_WRAP);
98 setVisibleRowCount(-1);
102 DeviceList visible_devices;
104 DeviceList known_devices;
107 public Iterator<AltosBTDevice> iterator() {
108 return known_devices.iterator();
111 public void commit() {
115 public void add_known() {
117 for (AltosBTDevice device : visible_devices.selected_list()) {
118 known_devices.add(device);
119 visible_devices.remove(device);
121 } catch (InterruptedException ie) {
125 public void remove_known() {
127 for (AltosBTDevice device : known_devices.selected_list()) {
128 known_devices.remove(device);
129 visible_devices.add(device);
131 } catch (InterruptedException ie) {
135 public void addActionListener(ActionListener l) {
139 private void forwardAction(ActionEvent e) {
140 for (ActionListener l : listeners)
141 l.actionPerformed(e);
144 public void actionPerformed(ActionEvent e) {
145 String command = e.getActionCommand();
146 if ("ok".equals(command)) {
147 bt_thread.interrupt();
151 } else if ("cancel".equals(command)) {
152 bt_thread.interrupt();
155 } else if ("select".equals(command)) {
157 } else if ("deselect".equals(command)) {
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);
170 class BTGetVisibleDevices implements Runnable {
173 for (int time = 1; time <= 8; time <<= 1) {
174 AltosBTDeviceIterator i = new AltosBTDeviceIterator(time);
175 AltosBTDevice device;
177 if (Thread.interrupted())
180 while ((device = i.next()) != null) {
183 if (Thread.interrupted())
185 found_devices.add(device);
188 got_visible_device();
191 SwingUtilities.invokeLater(r);
193 } catch (Exception e) {
194 System.out.printf("uh-oh, exception %s\n", e.toString());
200 public static void show(Component frameComp, AltosBTKnown known) {
201 Frame frame = JOptionPane.getFrameForComponent(frameComp);
202 AltosBTManage dialog;
204 dialog = new AltosBTManage(frame, known);
205 dialog.setVisible(true);
208 public AltosBTManage(Frame in_frame, AltosBTKnown in_known) {
209 super(in_frame, "Manage Bluetooth Devices", true);
213 BTGetVisibleDevices get_visible_devices = new BTGetVisibleDevices();
214 bt_thread = new Thread(get_visible_devices);
217 listeners = new LinkedList<ActionListener>();
219 found_devices = new LinkedBlockingQueue<AltosBTDevice>();
221 Container pane = getContentPane();
222 pane.setLayout(new GridBagLayout());
224 GridBagConstraints c = new GridBagConstraints();
225 c.insets = new Insets(4,4,4,4);
228 * Known devices label and list
230 c.fill = GridBagConstraints.NONE;
231 c.anchor = GridBagConstraints.WEST;
238 pane.add(new JLabel("Known Devices"), c);
240 known_devices = new DeviceList();
241 for (AltosBTDevice device : bt_known)
242 known_devices.add(device);
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;
255 pane.add(known_list_scroller, c);
258 * Visible devices label and list
260 c.fill = GridBagConstraints.NONE;
261 c.anchor = GridBagConstraints.WEST;
269 pane.add(new JLabel("Visible Devices"), c);
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;
283 pane.add(visible_list_scroller, c);
286 * Arrows between the two lists
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;
299 pane.add(select_arrow, c);
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;
312 pane.add(deselect_arrow, c);
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;
325 pane.add(cancel_button, c);
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;
338 pane.add(ok_button, c);
340 getRootPane().setDefaultButton(ok_button);
343 setLocationRelativeTo(frame);
344 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
345 addWindowListener(new WindowAdapter() {
347 public void windowClosing(WindowEvent e) {
348 bt_thread.interrupt();