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.*;
35 public class AltosBTManage extends JDialog implements ActionListener, Iterable<AltosBTDevice> {
36 LinkedBlockingQueue<AltosBTDevice> found_devices;
38 LinkedList<ActionListener> listeners;
39 AltosBTKnown bt_known;
41 class DeviceList extends JList implements Iterable<AltosBTDevice> {
42 LinkedList<AltosBTDevice> devices;
43 DefaultListModel list_model;
45 public void add (AltosBTDevice device) {
46 if (!devices.contains(device)) {
48 list_model.addElement(device);
52 public void remove (AltosBTDevice device) {
53 if (devices.contains(device)) {
54 devices.remove(device);
55 list_model.removeElement(device);
59 public boolean contains(AltosBTDevice device) {
60 return devices.contains(device);
63 //Subclass JList to workaround bug 4832765, which can cause the
64 //scroll pane to not let the user easily scroll up to the beginning
65 //of the list. An alternative would be to set the unitIncrement
66 //of the JScrollBar to a fixed value. You wouldn't get the nice
67 //aligned scrolling, but it should work.
68 public int getScrollableUnitIncrement(Rectangle visibleRect,
72 if (orientation == SwingConstants.VERTICAL &&
73 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
74 Rectangle r = getCellBounds(row, row);
75 if ((r.y == visibleRect.y) && (row != 0)) {
76 Point loc = r.getLocation();
78 int prevIndex = locationToIndex(loc);
79 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
81 if (prevR == null || prevR.y >= r.y) {
87 return super.getScrollableUnitIncrement(
88 visibleRect, orientation, direction);
91 public Iterator<AltosBTDevice> iterator() {
92 return devices.iterator();
95 public java.util.List<AltosBTDevice> selected_list() {
96 java.util.LinkedList<AltosBTDevice> l = new java.util.LinkedList<AltosBTDevice>();
97 Object[] a = getSelectedValues();
98 for (int i = 0; i < a.length; i++)
99 l.add((AltosBTDevice)a[i]);
103 public DeviceList() {
104 devices = new LinkedList<AltosBTDevice>();
105 list_model = new DefaultListModel();
106 setModel(list_model);
107 setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
108 setLayoutOrientation(JList.HORIZONTAL_WRAP);
109 setVisibleRowCount(-1);
113 DeviceList visible_devices;
115 DeviceList known_devices;
118 public Iterator<AltosBTDevice> iterator() {
119 return known_devices.iterator();
122 public void commit() {
126 public void add_known() {
127 for (AltosBTDevice device : visible_devices.selected_list()) {
128 System.out.printf("Add known %s\n", device.toString());
129 known_devices.add(device);
130 visible_devices.remove(device);
134 public void remove_known() {
135 for (AltosBTDevice device : known_devices.selected_list()) {
136 System.out.printf("Remove known %s\n", device.toString());
137 known_devices.remove(device);
138 visible_devices.add(device);
142 public void addActionListener(ActionListener l) {
146 private void forwardAction(ActionEvent e) {
147 for (ActionListener l : listeners)
148 l.actionPerformed(e);
151 public void actionPerformed(ActionEvent e) {
152 String command = e.getActionCommand();
153 System.out.printf("manage command %s\n", command);
154 if ("ok".equals(command)) {
155 bt_thread.interrupt();
159 } else if ("cancel".equals(command)) {
160 bt_thread.interrupt();
163 } else if ("select".equals(command)) {
165 } else if ("deselect".equals(command)) {
170 public void got_visible_device() {
171 while (!found_devices.isEmpty()) {
172 AltosBTDevice device = found_devices.remove();
173 if (!known_devices.contains(device))
174 visible_devices.add(device);
178 class BTGetVisibleDevices implements Runnable {
181 for (int time = 1; time <= 8; time <<= 1) {
182 AltosBTDeviceIterator i = new AltosBTDeviceIterator(time);
183 AltosBTDevice device;
185 if (Thread.interrupted())
188 while ((device = i.next()) != null) {
191 if (Thread.interrupted())
193 found_devices.add(device);
196 got_visible_device();
199 SwingUtilities.invokeLater(r);
201 } catch (Exception e) {
202 System.out.printf("uh-oh, exception %s\n", e.toString());
208 public static void show(Component frameComp, AltosBTKnown known) {
209 Frame frame = JOptionPane.getFrameForComponent(frameComp);
210 AltosBTManage dialog;
212 dialog = new AltosBTManage(frame, known);
213 dialog.setVisible(true);
216 public AltosBTManage(Frame in_frame, AltosBTKnown in_known) {
217 super(in_frame, "Manage Bluetooth Devices", true);
221 BTGetVisibleDevices get_visible_devices = new BTGetVisibleDevices();
222 bt_thread = new Thread(get_visible_devices);
225 listeners = new LinkedList<ActionListener>();
227 found_devices = new LinkedBlockingQueue<AltosBTDevice>();
229 Container pane = getContentPane();
230 pane.setLayout(new GridBagLayout());
232 GridBagConstraints c = new GridBagConstraints();
233 c.insets = new Insets(4,4,4,4);
236 * Known devices label and list
238 c.fill = GridBagConstraints.NONE;
239 c.anchor = GridBagConstraints.WEST;
246 pane.add(new JLabel("Known Devices"), c);
248 known_devices = new DeviceList();
249 for (AltosBTDevice device : bt_known)
250 known_devices.add(device);
252 JScrollPane known_list_scroller = new JScrollPane(known_devices);
253 known_list_scroller.setPreferredSize(new Dimension(400, 80));
254 known_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
255 c.fill = GridBagConstraints.BOTH;
256 c.anchor = GridBagConstraints.WEST;
263 pane.add(known_list_scroller, c);
266 * Visible devices label and list
268 c.fill = GridBagConstraints.NONE;
269 c.anchor = GridBagConstraints.WEST;
277 pane.add(new JLabel("Visible Devices"), c);
279 visible_devices = new DeviceList();
280 JScrollPane visible_list_scroller = new JScrollPane(visible_devices);
281 visible_list_scroller.setPreferredSize(new Dimension(400, 80));
282 visible_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
283 c.fill = GridBagConstraints.BOTH;
284 c.anchor = GridBagConstraints.WEST;
291 pane.add(visible_list_scroller, c);
294 * Arrows between the two lists
296 BasicArrowButton select_arrow = new BasicArrowButton(SwingConstants.WEST);
297 select_arrow.setActionCommand("select");
298 select_arrow.addActionListener(this);
299 c.fill = GridBagConstraints.NONE;
300 c.anchor = GridBagConstraints.SOUTH;
307 pane.add(select_arrow, c);
309 BasicArrowButton deselect_arrow = new BasicArrowButton(SwingConstants.EAST);
310 deselect_arrow.setActionCommand("deselect");
311 deselect_arrow.addActionListener(this);
312 c.fill = GridBagConstraints.NONE;
313 c.anchor = GridBagConstraints.NORTH;
320 pane.add(deselect_arrow, c);
322 JButton cancel_button = new JButton("Cancel");
323 cancel_button.setActionCommand("cancel");
324 cancel_button.addActionListener(this);
325 c.fill = GridBagConstraints.NONE;
326 c.anchor = GridBagConstraints.CENTER;
333 pane.add(cancel_button, c);
335 JButton ok_button = new JButton("OK");
336 ok_button.setActionCommand("ok");
337 ok_button.addActionListener(this);
338 c.fill = GridBagConstraints.NONE;
339 c.anchor = GridBagConstraints.CENTER;
346 pane.add(ok_button, c);
348 getRootPane().setDefaultButton(ok_button);
351 setLocationRelativeTo(frame);
352 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
353 addWindowListener(new WindowAdapter() {
355 public void windowClosing(WindowEvent e) {
356 bt_thread.interrupt();