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() {
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() {
120 for (AltosBTDevice device : visible_devices.selected_list()) {
121 known_devices.add(device);
122 visible_devices.remove(device);
126 public void remove_known() {
127 for (AltosBTDevice device : known_devices.selected_list()) {
128 known_devices.remove(device);
129 visible_devices.add(device);
133 public void addActionListener(ActionListener l) {
137 private void forwardAction(ActionEvent e) {
138 for (ActionListener l : listeners)
139 l.actionPerformed(e);
142 public void actionPerformed(ActionEvent e) {
143 String command = e.getActionCommand();
144 if ("ok".equals(command)) {
145 bt_thread.interrupt();
149 } else if ("cancel".equals(command)) {
150 bt_thread.interrupt();
153 } else if ("select".equals(command)) {
155 } else if ("deselect".equals(command)) {
160 public void got_visible_device() {
161 while (!found_devices.isEmpty()) {
162 AltosBTDevice device = found_devices.remove();
163 if (!known_devices.contains(device))
164 visible_devices.add(device);
168 class BTGetVisibleDevices implements Runnable {
171 for (int time = 1; time <= 8; time <<= 1) {
172 AltosBTDeviceIterator i = new AltosBTDeviceIterator(time);
173 AltosBTDevice device;
175 if (Thread.interrupted())
178 while ((device = i.next()) != null) {
181 if (Thread.interrupted())
183 found_devices.add(device);
186 got_visible_device();
189 SwingUtilities.invokeLater(r);
191 } catch (Exception e) {
192 System.out.printf("uh-oh, exception %s\n", e.toString());
198 public static void show(Component frameComp, AltosBTKnown known) {
199 Frame frame = JOptionPane.getFrameForComponent(frameComp);
200 AltosBTManage dialog;
202 dialog = new AltosBTManage(frame, known);
203 dialog.setVisible(true);
206 public AltosBTManage(Frame in_frame, AltosBTKnown in_known) {
207 super(in_frame, "Manage Bluetooth Devices", true);
211 BTGetVisibleDevices get_visible_devices = new BTGetVisibleDevices();
212 bt_thread = new Thread(get_visible_devices);
215 listeners = new LinkedList<ActionListener>();
217 found_devices = new LinkedBlockingQueue<AltosBTDevice>();
219 Container pane = getContentPane();
220 pane.setLayout(new GridBagLayout());
222 GridBagConstraints c = new GridBagConstraints();
223 c.insets = new Insets(4,4,4,4);
226 * Known devices label and list
228 c.fill = GridBagConstraints.NONE;
229 c.anchor = GridBagConstraints.WEST;
236 pane.add(new JLabel("Known Devices"), c);
238 known_devices = new DeviceList();
239 for (AltosBTDevice device : bt_known)
240 known_devices.add(device);
242 JScrollPane known_list_scroller = new JScrollPane(known_devices);
243 known_list_scroller.setPreferredSize(new Dimension(400, 80));
244 known_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
245 c.fill = GridBagConstraints.BOTH;
246 c.anchor = GridBagConstraints.WEST;
253 pane.add(known_list_scroller, c);
256 * Visible devices label and list
258 c.fill = GridBagConstraints.NONE;
259 c.anchor = GridBagConstraints.WEST;
267 pane.add(new JLabel("Visible Devices"), c);
269 visible_devices = new DeviceList();
270 JScrollPane visible_list_scroller = new JScrollPane(visible_devices);
271 visible_list_scroller.setPreferredSize(new Dimension(400, 80));
272 visible_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
273 c.fill = GridBagConstraints.BOTH;
274 c.anchor = GridBagConstraints.WEST;
281 pane.add(visible_list_scroller, c);
284 * Arrows between the two lists
286 BasicArrowButton select_arrow = new BasicArrowButton(SwingConstants.WEST);
287 select_arrow.setActionCommand("select");
288 select_arrow.addActionListener(this);
289 c.fill = GridBagConstraints.NONE;
290 c.anchor = GridBagConstraints.SOUTH;
297 pane.add(select_arrow, c);
299 BasicArrowButton deselect_arrow = new BasicArrowButton(SwingConstants.EAST);
300 deselect_arrow.setActionCommand("deselect");
301 deselect_arrow.addActionListener(this);
302 c.fill = GridBagConstraints.NONE;
303 c.anchor = GridBagConstraints.NORTH;
310 pane.add(deselect_arrow, c);
312 JButton cancel_button = new JButton("Cancel");
313 cancel_button.setActionCommand("cancel");
314 cancel_button.addActionListener(this);
315 c.fill = GridBagConstraints.NONE;
316 c.anchor = GridBagConstraints.CENTER;
323 pane.add(cancel_button, c);
325 JButton ok_button = new JButton("OK");
326 ok_button.setActionCommand("ok");
327 ok_button.addActionListener(this);
328 c.fill = GridBagConstraints.NONE;
329 c.anchor = GridBagConstraints.CENTER;
336 pane.add(ok_button, c);
338 getRootPane().setDefaultButton(ok_button);
341 setLocationRelativeTo(frame);
342 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
343 addWindowListener(new WindowAdapter() {
345 public void windowClosing(WindowEvent e) {
346 bt_thread.interrupt();