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.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
33 public class AltosBTManage extends JDialog implements ActionListener {
35 LinkedBlockingQueue<AltosBTDevice> found_devices;
38 class DeviceList extends JList implements Iterable<AltosBTDevice> {
39 LinkedList<AltosBTDevice> devices;
40 DefaultListModel list_model;
42 public void add (AltosBTDevice device) {
44 list_model.addElement(device);
47 //Subclass JList to workaround bug 4832765, which can cause the
48 //scroll pane to not let the user easily scroll up to the beginning
49 //of the list. An alternative would be to set the unitIncrement
50 //of the JScrollBar to a fixed value. You wouldn't get the nice
51 //aligned scrolling, but it should work.
52 public int getScrollableUnitIncrement(Rectangle visibleRect,
56 if (orientation == SwingConstants.VERTICAL &&
57 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
58 Rectangle r = getCellBounds(row, row);
59 if ((r.y == visibleRect.y) && (row != 0)) {
60 Point loc = r.getLocation();
62 int prevIndex = locationToIndex(loc);
63 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
65 if (prevR == null || prevR.y >= r.y) {
71 return super.getScrollableUnitIncrement(
72 visibleRect, orientation, direction);
75 public Iterator<AltosBTDevice> iterator() {
76 return devices.iterator();
80 devices = new LinkedList<AltosBTDevice>();
81 list_model = new DefaultListModel();
83 setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
84 setLayoutOrientation(JList.HORIZONTAL_WRAP);
85 setVisibleRowCount(-1);
89 DeviceList visible_devices;
91 DeviceList selected_devices;
93 public void actionPerformed(ActionEvent e) {
96 public void got_visible_device() {
97 while (!found_devices.isEmpty()) {
98 AltosBTDevice device = found_devices.remove();
99 visible_devices.add(device);
103 class BTGetVisibleDevices implements Runnable {
107 AltosBTDeviceIterator i = new AltosBTDeviceIterator(product);
108 AltosBTDevice device;
110 while ((device = i.next()) != null) {
113 found_devices.add(device);
116 got_visible_device();
119 SwingUtilities.invokeLater(r);
121 } catch (Exception e) {
122 System.out.printf("uh-oh, exception %s\n", e.toString());
127 public AltosBTManage(String product, JFrame in_frame) {
129 BTGetVisibleDevices get_visible_devices = new BTGetVisibleDevices();
130 Thread t = new Thread(get_visible_devices);
133 found_devices = new LinkedBlockingQueue<AltosBTDevice>();
135 JButton cancelButton = new JButton("Cancel");
136 cancelButton.addActionListener(this);
138 final JButton selectButton = new JButton("Select");
139 selectButton.setActionCommand("select");
140 selectButton.addActionListener(this);
141 getRootPane().setDefaultButton(selectButton);
143 selected_devices = new DeviceList();
144 JScrollPane selected_list_scroller = new JScrollPane(selected_devices);
145 selected_list_scroller.setPreferredSize(new Dimension(400, 80));
146 selected_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
148 visible_devices = new DeviceList();
149 JScrollPane visible_list_scroller = new JScrollPane(visible_devices);
150 visible_list_scroller.setPreferredSize(new Dimension(400, 80));
151 visible_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
153 //Create a container so that we can add a title around
154 //the scroll pane. Can't add a title directly to the
155 //scroll pane because its background would be white.
156 //Lay out the label and scroll pane from top to bottom.
157 JPanel listPane = new JPanel();
158 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
160 JLabel label = new JLabel("Select Device");
161 label.setLabelFor(selected_devices);
163 listPane.add(Box.createRigidArea(new Dimension(0,5)));
164 listPane.add(selected_list_scroller);
165 listPane.add(visible_list_scroller);
166 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
168 //Lay out the buttons from left to right.
169 JPanel buttonPane = new JPanel();
170 buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
171 buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
172 buttonPane.add(Box.createHorizontalGlue());
173 buttonPane.add(cancelButton);
174 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
175 buttonPane.add(selectButton);
177 //Put everything together, using the content pane's BorderLayout.
178 Container contentPane = getContentPane();
179 contentPane.add(listPane, BorderLayout.CENTER);
180 contentPane.add(buttonPane, BorderLayout.PAGE_END);
183 // list.setSelectedValue(initial, true);
185 setLocationRelativeTo(frame);