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.*;
27 public class AltosBTManage extends AltosDialog implements ActionListener, Iterable<AltosBTDevice> {
28 LinkedBlockingQueue<AltosBTDevice> found_devices;
30 LinkedList<ActionListener> listeners;
31 AltosBTKnown bt_known;
33 class DeviceList extends JList implements Iterable<AltosBTDevice> {
34 LinkedList<AltosBTDevice> devices;
35 DefaultListModel list_model;
37 public void add (AltosBTDevice device) {
38 if (!devices.contains(device)) {
40 list_model.addElement(device);
44 public void remove (AltosBTDevice device) {
45 if (devices.contains(device)) {
46 devices.remove(device);
47 list_model.removeElement(device);
51 public boolean contains(AltosBTDevice device) {
52 return devices.contains(device);
55 //Subclass JList to workaround bug 4832765, which can cause the
56 //scroll pane to not let the user easily scroll up to the beginning
57 //of the list. An alternative would be to set the unitIncrement
58 //of the JScrollBar to a fixed value. You wouldn't get the nice
59 //aligned scrolling, but it should work.
60 public int getScrollableUnitIncrement(Rectangle visibleRect,
64 if (orientation == SwingConstants.VERTICAL &&
65 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
66 Rectangle r = getCellBounds(row, row);
67 if ((r.y == visibleRect.y) && (row != 0)) {
68 Point loc = r.getLocation();
70 int prevIndex = locationToIndex(loc);
71 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
73 if (prevR == null || prevR.y >= r.y) {
79 return super.getScrollableUnitIncrement(
80 visibleRect, orientation, direction);
83 public Iterator<AltosBTDevice> iterator() {
84 return devices.iterator();
87 public java.util.List<AltosBTDevice> selected_list() {
88 java.util.LinkedList<AltosBTDevice> l = new java.util.LinkedList<AltosBTDevice>();
89 Object[] a = getSelectedValues();
90 for (int i = 0; i < a.length; i++)
91 l.add((AltosBTDevice)a[i]);
96 devices = new LinkedList<AltosBTDevice>();
97 list_model = new DefaultListModel();
99 setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
100 setLayoutOrientation(JList.HORIZONTAL_WRAP);
101 setVisibleRowCount(-1);
105 DeviceList visible_devices;
107 DeviceList known_devices;
110 public Iterator<AltosBTDevice> iterator() {
111 return known_devices.iterator();
114 public void commit() {
118 public void add_known() {
119 for (AltosBTDevice device : visible_devices.selected_list()) {
120 known_devices.add(device);
121 visible_devices.remove(device);
125 public void remove_known() {
126 for (AltosBTDevice device : known_devices.selected_list()) {
127 known_devices.remove(device);
128 visible_devices.add(device);
132 public void addActionListener(ActionListener l) {
136 private void forwardAction(ActionEvent e) {
137 for (ActionListener l : listeners)
138 l.actionPerformed(e);
141 public void actionPerformed(ActionEvent e) {
142 String command = e.getActionCommand();
143 if ("ok".equals(command)) {
144 bt_thread.interrupt();
148 } else if ("cancel".equals(command)) {
149 bt_thread.interrupt();
152 } else if ("select".equals(command)) {
154 } else if ("deselect".equals(command)) {
159 public void got_visible_device() {
160 while (!found_devices.isEmpty()) {
161 AltosBTDevice device = found_devices.remove();
162 if (!known_devices.contains(device))
163 visible_devices.add(device);
167 class BTGetVisibleDevices implements Runnable {
170 for (int time = 1; time <= 8; time <<= 1) {
171 AltosBTDeviceIterator i = new AltosBTDeviceIterator(time);
172 AltosBTDevice device;
174 if (Thread.interrupted())
177 while ((device = i.next()) != null) {
180 if (Thread.interrupted())
182 found_devices.add(device);
185 got_visible_device();
188 SwingUtilities.invokeLater(r);
190 } catch (Exception e) {
191 System.out.printf("uh-oh, exception %s\n", e.toString());
197 public static void show(Component frameComp, AltosBTKnown known) {
198 Frame frame = JOptionPane.getFrameForComponent(frameComp);
199 AltosBTManage dialog;
201 dialog = new AltosBTManage(frame, known);
202 dialog.setVisible(true);
205 public AltosBTManage(Frame in_frame, AltosBTKnown in_known) {
206 super(in_frame, "Manage Bluetooth Devices", true);
210 BTGetVisibleDevices get_visible_devices = new BTGetVisibleDevices();
211 bt_thread = new Thread(get_visible_devices);
214 listeners = new LinkedList<ActionListener>();
216 found_devices = new LinkedBlockingQueue<AltosBTDevice>();
218 Container pane = getContentPane();
219 pane.setLayout(new GridBagLayout());
221 GridBagConstraints c = new GridBagConstraints();
222 c.insets = new Insets(4,4,4,4);
225 * Known devices label and list
227 c.fill = GridBagConstraints.NONE;
228 c.anchor = GridBagConstraints.WEST;
235 pane.add(new JLabel("Known Devices"), c);
237 known_devices = new DeviceList();
238 for (AltosBTDevice device : bt_known)
239 known_devices.add(device);
241 JScrollPane known_list_scroller = new JScrollPane(known_devices);
242 known_list_scroller.setPreferredSize(new Dimension(400, 80));
243 known_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
244 c.fill = GridBagConstraints.BOTH;
245 c.anchor = GridBagConstraints.WEST;
252 pane.add(known_list_scroller, c);
255 * Visible devices label and list
257 c.fill = GridBagConstraints.NONE;
258 c.anchor = GridBagConstraints.WEST;
266 pane.add(new JLabel("Visible Devices"), c);
268 visible_devices = new DeviceList();
269 JScrollPane visible_list_scroller = new JScrollPane(visible_devices);
270 visible_list_scroller.setPreferredSize(new Dimension(400, 80));
271 visible_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
272 c.fill = GridBagConstraints.BOTH;
273 c.anchor = GridBagConstraints.WEST;
280 pane.add(visible_list_scroller, c);
283 * Arrows between the two lists
285 BasicArrowButton select_arrow = new BasicArrowButton(SwingConstants.WEST);
286 select_arrow.setActionCommand("select");
287 select_arrow.addActionListener(this);
288 c.fill = GridBagConstraints.NONE;
289 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;
309 pane.add(deselect_arrow, c);
311 JButton cancel_button = new JButton("Cancel");
312 cancel_button.setActionCommand("cancel");
313 cancel_button.addActionListener(this);
314 c.fill = GridBagConstraints.NONE;
315 c.anchor = GridBagConstraints.CENTER;
322 pane.add(cancel_button, c);
324 JButton ok_button = new JButton("OK");
325 ok_button.setActionCommand("ok");
326 ok_button.addActionListener(this);
327 c.fill = GridBagConstraints.NONE;
328 c.anchor = GridBagConstraints.CENTER;
335 pane.add(ok_button, c);
337 getRootPane().setDefaultButton(ok_button);
340 setLocationRelativeTo(frame);
341 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
342 addWindowListener(new WindowAdapter() {
344 public void windowClosing(WindowEvent e) {
345 bt_thread.interrupt();