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;
244 pane.add(new JLabel("Known Devices"), c);
246 known_devices = new DeviceList();
247 for (AltosBTDevice device : bt_known)
248 known_devices.add(device);
250 JScrollPane known_list_scroller = new JScrollPane(known_devices);
251 known_list_scroller.setPreferredSize(new Dimension(400, 80));
252 known_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
253 c.fill = GridBagConstraints.BOTH;
254 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;
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;
282 pane.add(visible_list_scroller, c);
285 * Arrows between the two lists
287 BasicArrowButton select_arrow = new BasicArrowButton(SwingConstants.WEST);
288 select_arrow.setActionCommand("select");
289 select_arrow.addActionListener(this);
290 c.fill = GridBagConstraints.NONE;
291 c.anchor = GridBagConstraints.SOUTH;
296 pane.add(select_arrow, c);
298 BasicArrowButton deselect_arrow = new BasicArrowButton(SwingConstants.EAST);
299 deselect_arrow.setActionCommand("deselect");
300 deselect_arrow.addActionListener(this);
301 c.fill = GridBagConstraints.NONE;
302 c.anchor = GridBagConstraints.NORTH;
307 pane.add(deselect_arrow, c);
309 JButton cancel_button = new JButton("Cancel");
310 cancel_button.setActionCommand("cancel");
311 cancel_button.addActionListener(this);
312 c.fill = GridBagConstraints.NONE;
313 c.anchor = GridBagConstraints.CENTER;
318 pane.add(cancel_button, c);
320 JButton ok_button = new JButton("OK");
321 ok_button.setActionCommand("ok");
322 ok_button.addActionListener(this);
323 c.fill = GridBagConstraints.NONE;
324 c.anchor = GridBagConstraints.CENTER;
329 pane.add(ok_button, c);
331 getRootPane().setDefaultButton(ok_button);
334 setLocationRelativeTo(frame);
335 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
336 addWindowListener(new WindowAdapter() {
338 public void windowClosing(WindowEvent e) {
339 bt_thread.interrupt();