2 * Copyright © 2010 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.
24 import java.awt.event.*;
27 public class AltosDeviceDialog extends JDialog implements ActionListener {
29 private AltosDevice value;
31 private JButton cancel_button;
32 private JButton select_button;
33 private JButton manage_bluetooth_button;
37 private AltosDevice getValue() {
41 private AltosDevice[] devices() {
42 java.util.List<AltosDevice> usb_devices = AltosUSBDevice.list(product);
43 java.util.List<AltosDevice> bt_devices = Altos.bt_known.list(product);
44 AltosDevice[] devices = new AltosDevice[usb_devices.size() + bt_devices.size()];
46 for (int i = 0; i < usb_devices.size(); i++)
47 devices[i] = usb_devices.get(i);
48 int off = usb_devices.size();
49 for (int j = 0; j < bt_devices.size(); j++)
50 devices[off + j] = bt_devices.get(j);
54 private void update_devices() {
55 AltosDevice[] devices = devices();
56 list.setListData(devices);
57 select_button.setEnabled(devices.length > 0);
60 private AltosDeviceDialog (Frame in_frame, Component location, int in_product) {
61 super(in_frame, "Device Selection", true);
67 AltosDevice[] devices = devices();
69 cancel_button = new JButton("Cancel");
70 cancel_button.setActionCommand("cancel");
71 cancel_button.addActionListener(this);
73 manage_bluetooth_button = new JButton("Manage Bluetooth");
74 manage_bluetooth_button.setActionCommand("manage");
75 manage_bluetooth_button.addActionListener(this);
77 select_button = new JButton("Select");
78 select_button.setActionCommand("select");
79 select_button.addActionListener(this);
80 if (devices.length == 0)
81 select_button.setEnabled(false);
82 getRootPane().setDefaultButton(select_button);
84 list = new JList(devices) {
85 //Subclass JList to workaround bug 4832765, which can cause the
86 //scroll pane to not let the user easily scroll up to the beginning
87 //of the list. An alternative would be to set the unitIncrement
88 //of the JScrollBar to a fixed value. You wouldn't get the nice
89 //aligned scrolling, but it should work.
90 public int getScrollableUnitIncrement(Rectangle visibleRect,
94 if (orientation == SwingConstants.VERTICAL &&
95 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
96 Rectangle r = getCellBounds(row, row);
97 if ((r.y == visibleRect.y) && (row != 0)) {
98 Point loc = r.getLocation();
100 int prevIndex = locationToIndex(loc);
101 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
103 if (prevR == null || prevR.y >= r.y) {
109 return super.getScrollableUnitIncrement(
110 visibleRect, orientation, direction);
114 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
115 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
116 list.setVisibleRowCount(-1);
117 list.addMouseListener(new MouseAdapter() {
118 public void mouseClicked(MouseEvent e) {
119 if (e.getClickCount() == 2) {
120 select_button.doClick(); //emulate button click
124 JScrollPane listScroller = new JScrollPane(list);
125 listScroller.setPreferredSize(new Dimension(400, 80));
126 listScroller.setAlignmentX(LEFT_ALIGNMENT);
128 //Create a container so that we can add a title around
129 //the scroll pane. Can't add a title directly to the
130 //scroll pane because its background would be white.
131 //Lay out the label and scroll pane from top to bottom.
132 JPanel listPane = new JPanel();
133 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
135 JLabel label = new JLabel("Select Device");
136 label.setLabelFor(list);
138 listPane.add(Box.createRigidArea(new Dimension(0,5)));
139 listPane.add(listScroller);
140 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
142 //Lay out the buttons from left to right.
143 JPanel buttonPane = new JPanel();
144 buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
145 buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
146 buttonPane.add(Box.createHorizontalGlue());
147 buttonPane.add(cancel_button);
148 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
149 buttonPane.add(manage_bluetooth_button);
150 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
151 buttonPane.add(select_button);
153 //Put everything together, using the content pane's BorderLayout.
154 Container contentPane = getContentPane();
155 contentPane.add(listPane, BorderLayout.CENTER);
156 contentPane.add(buttonPane, BorderLayout.PAGE_END);
159 if (devices != null && devices.length != 0)
160 list.setSelectedValue(devices[0], true);
162 setLocationRelativeTo(location);
165 //Handle clicks on the Set and Cancel buttons.
166 public void actionPerformed(ActionEvent e) {
167 if ("select".equals(e.getActionCommand()))
168 value = (AltosDevice)(list.getSelectedValue());
169 if ("manage".equals(e.getActionCommand())) {
170 AltosBTManage.show(frame, Altos.bt_known);
177 public static AltosDevice show (Component frameComp, int product) {
179 Frame frame = JOptionPane.getFrameForComponent(frameComp);
180 AltosDeviceDialog dialog;
182 dialog = new AltosDeviceDialog(frame, frameComp, product);
183 dialog.setVisible(true);
184 return dialog.getValue();