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.plaf.basic.*;
25 import java.util.concurrent.*;
26 import org.altusmetrum.altosuilib_1.*;
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 implements Iterable<AltosBTDevice> {
35 LinkedList<AltosBTDevice> devices;
36 DefaultListModel 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 java.util.LinkedList<AltosBTDevice> l = new java.util.LinkedList<AltosBTDevice>();
90 Object[] a = getSelectedValues();
91 for (int i = 0; i < a.length; i++)
92 l.add((AltosBTDevice)a[i]);
97 devices = new LinkedList<AltosBTDevice>();
98 list_model = new DefaultListModel();
100 setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
101 setLayoutOrientation(JList.HORIZONTAL_WRAP);
102 setVisibleRowCount(-1);
106 DeviceList visible_devices;
108 DeviceList known_devices;
111 public Iterator<AltosBTDevice> iterator() {
112 return known_devices.iterator();
115 public void commit() {
119 public void add_known() {
121 for (AltosBTDevice device : visible_devices.selected_list()) {
122 known_devices.add(device);
123 visible_devices.remove(device);
125 } catch (InterruptedException ie) {
129 public void remove_known() {
131 for (AltosBTDevice device : known_devices.selected_list()) {
132 known_devices.remove(device);
133 visible_devices.add(device);
135 } catch (InterruptedException ie) {
139 public void addActionListener(ActionListener l) {
143 private void forwardAction(ActionEvent e) {
144 for (ActionListener l : listeners)
145 l.actionPerformed(e);
148 public void actionPerformed(ActionEvent e) {
149 String command = e.getActionCommand();
150 if ("ok".equals(command)) {
151 bt_thread.interrupt();
155 } else if ("cancel".equals(command)) {
156 bt_thread.interrupt();
159 } else if ("select".equals(command)) {
161 } else if ("deselect".equals(command)) {
166 public void got_visible_device() {
167 while (!found_devices.isEmpty()) {
168 AltosBTDevice device = found_devices.remove();
169 if (!known_devices.contains(device))
170 visible_devices.add(device);
174 class BTGetVisibleDevices implements Runnable {
177 for (int time = 1; time <= 8; time <<= 1) {
178 AltosBTDeviceIterator i = new AltosBTDeviceIterator(time);
179 AltosBTDevice device;
181 if (Thread.interrupted())
184 while ((device = i.next()) != null) {
187 if (Thread.interrupted())
189 found_devices.add(device);
192 got_visible_device();
195 SwingUtilities.invokeLater(r);
197 } catch (Exception e) {
198 System.out.printf("uh-oh, exception %s\n", e.toString());
204 public static void show(Component frameComp, AltosBTKnown known) {
205 Frame frame = JOptionPane.getFrameForComponent(frameComp);
206 AltosBTManage dialog;
208 dialog = new AltosBTManage(frame, known);
209 dialog.setVisible(true);
212 public AltosBTManage(Frame in_frame, AltosBTKnown in_known) {
213 super(in_frame, "Manage Bluetooth Devices", true);
217 BTGetVisibleDevices get_visible_devices = new BTGetVisibleDevices();
218 bt_thread = new Thread(get_visible_devices);
221 listeners = new LinkedList<ActionListener>();
223 found_devices = new LinkedBlockingQueue<AltosBTDevice>();
225 Container pane = getContentPane();
226 pane.setLayout(new GridBagLayout());
228 GridBagConstraints c = new GridBagConstraints();
229 c.insets = new Insets(4,4,4,4);
232 * Known devices label and list
234 c.fill = GridBagConstraints.NONE;
235 c.anchor = GridBagConstraints.WEST;
242 pane.add(new JLabel("Known Devices"), c);
244 known_devices = new DeviceList();
245 for (AltosBTDevice device : bt_known)
246 known_devices.add(device);
248 JScrollPane known_list_scroller = new JScrollPane(known_devices);
249 known_list_scroller.setPreferredSize(new Dimension(400, 80));
250 known_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
251 c.fill = GridBagConstraints.BOTH;
252 c.anchor = GridBagConstraints.WEST;
259 pane.add(known_list_scroller, c);
262 * Visible devices label and list
264 c.fill = GridBagConstraints.NONE;
265 c.anchor = GridBagConstraints.WEST;
273 pane.add(new JLabel("Visible Devices"), c);
275 visible_devices = new DeviceList();
276 JScrollPane visible_list_scroller = new JScrollPane(visible_devices);
277 visible_list_scroller.setPreferredSize(new Dimension(400, 80));
278 visible_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
279 c.fill = GridBagConstraints.BOTH;
280 c.anchor = GridBagConstraints.WEST;
287 pane.add(visible_list_scroller, c);
290 * Arrows between the two lists
292 BasicArrowButton select_arrow = new BasicArrowButton(SwingConstants.WEST);
293 select_arrow.setActionCommand("select");
294 select_arrow.addActionListener(this);
295 c.fill = GridBagConstraints.NONE;
296 c.anchor = GridBagConstraints.SOUTH;
303 pane.add(select_arrow, c);
305 BasicArrowButton deselect_arrow = new BasicArrowButton(SwingConstants.EAST);
306 deselect_arrow.setActionCommand("deselect");
307 deselect_arrow.addActionListener(this);
308 c.fill = GridBagConstraints.NONE;
309 c.anchor = GridBagConstraints.NORTH;
316 pane.add(deselect_arrow, c);
318 JButton cancel_button = new JButton("Cancel");
319 cancel_button.setActionCommand("cancel");
320 cancel_button.addActionListener(this);
321 c.fill = GridBagConstraints.NONE;
322 c.anchor = GridBagConstraints.CENTER;
329 pane.add(cancel_button, c);
331 JButton ok_button = new JButton("OK");
332 ok_button.setActionCommand("ok");
333 ok_button.addActionListener(this);
334 c.fill = GridBagConstraints.NONE;
335 c.anchor = GridBagConstraints.CENTER;
342 pane.add(ok_button, c);
344 getRootPane().setDefaultButton(ok_button);
347 setLocationRelativeTo(frame);
348 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
349 addWindowListener(new WindowAdapter() {
351 public void windowClosing(WindowEvent e) {
352 bt_thread.interrupt();