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.*;
32 import org.altusmetrum.AltosLib.*;
36 public class AltosBTManage extends AltosDialog implements ActionListener, Iterable<AltosBTDevice> {
37 LinkedBlockingQueue<AltosBTDevice> found_devices;
39 LinkedList<ActionListener> listeners;
40 AltosBTKnown bt_known;
42 class DeviceList extends JList implements Iterable<AltosBTDevice> {
43 LinkedList<AltosBTDevice> devices;
44 DefaultListModel list_model;
46 public void add (AltosBTDevice device) {
47 if (!devices.contains(device)) {
49 list_model.addElement(device);
53 public void remove (AltosBTDevice device) {
54 if (devices.contains(device)) {
55 devices.remove(device);
56 list_model.removeElement(device);
60 public boolean contains(AltosBTDevice device) {
61 return devices.contains(device);
64 //Subclass JList to workaround bug 4832765, which can cause the
65 //scroll pane to not let the user easily scroll up to the beginning
66 //of the list. An alternative would be to set the unitIncrement
67 //of the JScrollBar to a fixed value. You wouldn't get the nice
68 //aligned scrolling, but it should work.
69 public int getScrollableUnitIncrement(Rectangle visibleRect,
73 if (orientation == SwingConstants.VERTICAL &&
74 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
75 Rectangle r = getCellBounds(row, row);
76 if ((r.y == visibleRect.y) && (row != 0)) {
77 Point loc = r.getLocation();
79 int prevIndex = locationToIndex(loc);
80 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
82 if (prevR == null || prevR.y >= r.y) {
88 return super.getScrollableUnitIncrement(
89 visibleRect, orientation, direction);
92 public Iterator<AltosBTDevice> iterator() {
93 return devices.iterator();
96 public java.util.List<AltosBTDevice> selected_list() {
97 java.util.LinkedList<AltosBTDevice> l = new java.util.LinkedList<AltosBTDevice>();
98 Object[] a = getSelectedValues();
99 for (int i = 0; i < a.length; i++)
100 l.add((AltosBTDevice)a[i]);
104 public DeviceList() {
105 devices = new LinkedList<AltosBTDevice>();
106 list_model = new DefaultListModel();
107 setModel(list_model);
108 setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
109 setLayoutOrientation(JList.HORIZONTAL_WRAP);
110 setVisibleRowCount(-1);
114 DeviceList visible_devices;
116 DeviceList known_devices;
119 public Iterator<AltosBTDevice> iterator() {
120 return known_devices.iterator();
123 public void commit() {
127 public void add_known() {
128 for (AltosBTDevice device : visible_devices.selected_list()) {
129 known_devices.add(device);
130 visible_devices.remove(device);
134 public void remove_known() {
135 for (AltosBTDevice device : known_devices.selected_list()) {
136 known_devices.remove(device);
137 visible_devices.add(device);
141 public void addActionListener(ActionListener l) {
145 private void forwardAction(ActionEvent e) {
146 for (ActionListener l : listeners)
147 l.actionPerformed(e);
150 public void actionPerformed(ActionEvent e) {
151 String command = e.getActionCommand();
152 if ("ok".equals(command)) {
153 bt_thread.interrupt();
157 } else if ("cancel".equals(command)) {
158 bt_thread.interrupt();
161 } else if ("select".equals(command)) {
163 } else if ("deselect".equals(command)) {
168 public void got_visible_device() {
169 while (!found_devices.isEmpty()) {
170 AltosBTDevice device = found_devices.remove();
171 if (!known_devices.contains(device))
172 visible_devices.add(device);
176 class BTGetVisibleDevices implements Runnable {
179 for (int time = 1; time <= 8; time <<= 1) {
180 AltosBTDeviceIterator i = new AltosBTDeviceIterator(time);
181 AltosBTDevice device;
183 if (Thread.interrupted())
186 while ((device = i.next()) != null) {
189 if (Thread.interrupted())
191 found_devices.add(device);
194 got_visible_device();
197 SwingUtilities.invokeLater(r);
199 } catch (Exception e) {
200 System.out.printf("uh-oh, exception %s\n", e.toString());
206 public static void show(Component frameComp, AltosBTKnown known) {
207 Frame frame = JOptionPane.getFrameForComponent(frameComp);
208 AltosBTManage dialog;
210 dialog = new AltosBTManage(frame, known);
211 dialog.setVisible(true);
214 public AltosBTManage(Frame in_frame, AltosBTKnown in_known) {
215 super(in_frame, "Manage Bluetooth Devices", true);
219 BTGetVisibleDevices get_visible_devices = new BTGetVisibleDevices();
220 bt_thread = new Thread(get_visible_devices);
223 listeners = new LinkedList<ActionListener>();
225 found_devices = new LinkedBlockingQueue<AltosBTDevice>();
227 Container pane = getContentPane();
228 pane.setLayout(new GridBagLayout());
230 GridBagConstraints c = new GridBagConstraints();
231 c.insets = new Insets(4,4,4,4);
234 * Known devices label and list
236 c.fill = GridBagConstraints.NONE;
237 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;
261 pane.add(known_list_scroller, c);
264 * Visible devices label and list
266 c.fill = GridBagConstraints.NONE;
267 c.anchor = GridBagConstraints.WEST;
275 pane.add(new JLabel("Visible Devices"), c);
277 visible_devices = new DeviceList();
278 JScrollPane visible_list_scroller = new JScrollPane(visible_devices);
279 visible_list_scroller.setPreferredSize(new Dimension(400, 80));
280 visible_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
281 c.fill = GridBagConstraints.BOTH;
282 c.anchor = GridBagConstraints.WEST;
289 pane.add(visible_list_scroller, c);
292 * Arrows between the two lists
294 BasicArrowButton select_arrow = new BasicArrowButton(SwingConstants.WEST);
295 select_arrow.setActionCommand("select");
296 select_arrow.addActionListener(this);
297 c.fill = GridBagConstraints.NONE;
298 c.anchor = GridBagConstraints.SOUTH;
305 pane.add(select_arrow, c);
307 BasicArrowButton deselect_arrow = new BasicArrowButton(SwingConstants.EAST);
308 deselect_arrow.setActionCommand("deselect");
309 deselect_arrow.addActionListener(this);
310 c.fill = GridBagConstraints.NONE;
311 c.anchor = GridBagConstraints.NORTH;
318 pane.add(deselect_arrow, c);
320 JButton cancel_button = new JButton("Cancel");
321 cancel_button.setActionCommand("cancel");
322 cancel_button.addActionListener(this);
323 c.fill = GridBagConstraints.NONE;
324 c.anchor = GridBagConstraints.CENTER;
331 pane.add(cancel_button, c);
333 JButton ok_button = new JButton("OK");
334 ok_button.setActionCommand("ok");
335 ok_button.addActionListener(this);
336 c.fill = GridBagConstraints.NONE;
337 c.anchor = GridBagConstraints.CENTER;
344 pane.add(ok_button, c);
346 getRootPane().setDefaultButton(ok_button);
349 setLocationRelativeTo(frame);
350 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
351 addWindowListener(new WindowAdapter() {
353 public void windowClosing(WindowEvent e) {
354 bt_thread.interrupt();