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 AltosDialog 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 int num_devices = usb_devices.size();
44 java.util.List<AltosDevice> bt_devices = Altos.bt_known.list(product);
45 num_devices += bt_devices.size();
46 AltosDevice[] devices = new AltosDevice[num_devices];
48 for (int i = 0; i < usb_devices.size(); i++)
49 devices[i] = usb_devices.get(i);
50 int off = usb_devices.size();
51 for (int j = 0; j < bt_devices.size(); j++)
52 devices[off + j] = bt_devices.get(j);
56 private void update_devices() {
57 AltosDevice[] devices = devices();
58 list.setListData(devices);
59 select_button.setEnabled(devices.length > 0);
62 private AltosDeviceDialog (Frame in_frame, Component location, int in_product) {
63 super(in_frame, "Device Selection", true);
69 AltosDevice[] devices = devices();
71 cancel_button = new JButton("Cancel");
72 cancel_button.setActionCommand("cancel");
73 cancel_button.addActionListener(this);
75 manage_bluetooth_button = new JButton("Manage Bluetooth");
76 manage_bluetooth_button.setActionCommand("manage");
77 manage_bluetooth_button.addActionListener(this);
79 select_button = new JButton("Select");
80 select_button.setActionCommand("select");
81 select_button.addActionListener(this);
82 if (devices.length == 0)
83 select_button.setEnabled(false);
84 getRootPane().setDefaultButton(select_button);
86 list = new JList(devices) {
87 //Subclass JList to workaround bug 4832765, which can cause the
88 //scroll pane to not let the user easily scroll up to the beginning
89 //of the list. An alternative would be to set the unitIncrement
90 //of the JScrollBar to a fixed value. You wouldn't get the nice
91 //aligned scrolling, but it should work.
92 public int getScrollableUnitIncrement(Rectangle visibleRect,
96 if (orientation == SwingConstants.VERTICAL &&
97 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
98 Rectangle r = getCellBounds(row, row);
99 if ((r.y == visibleRect.y) && (row != 0)) {
100 Point loc = r.getLocation();
102 int prevIndex = locationToIndex(loc);
103 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
105 if (prevR == null || prevR.y >= r.y) {
111 return super.getScrollableUnitIncrement(
112 visibleRect, orientation, direction);
116 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
117 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
118 list.setVisibleRowCount(-1);
119 list.addMouseListener(new MouseAdapter() {
120 public void mouseClicked(MouseEvent e) {
121 if (e.getClickCount() == 2) {
122 select_button.doClick(); //emulate button click
126 JScrollPane listScroller = new JScrollPane(list);
127 listScroller.setPreferredSize(new Dimension(400, 80));
128 listScroller.setAlignmentX(LEFT_ALIGNMENT);
130 //Create a container so that we can add a title around
131 //the scroll pane. Can't add a title directly to the
132 //scroll pane because its background would be white.
133 //Lay out the label and scroll pane from top to bottom.
134 JPanel listPane = new JPanel();
135 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
137 JLabel label = new JLabel("Select Device");
138 label.setLabelFor(list);
140 listPane.add(Box.createRigidArea(new Dimension(0,5)));
141 listPane.add(listScroller);
142 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
144 //Lay out the buttons from left to right.
145 JPanel buttonPane = new JPanel();
146 buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
147 buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
148 buttonPane.add(Box.createHorizontalGlue());
149 buttonPane.add(cancel_button);
150 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
151 buttonPane.add(manage_bluetooth_button);
152 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
153 buttonPane.add(select_button);
155 //Put everything together, using the content pane's BorderLayout.
156 Container contentPane = getContentPane();
157 contentPane.add(listPane, BorderLayout.CENTER);
158 contentPane.add(buttonPane, BorderLayout.PAGE_END);
161 if (devices != null && devices.length != 0)
162 list.setSelectedValue(devices[0], true);
164 setLocationRelativeTo(location);
167 //Handle clicks on the Set and Cancel buttons.
168 public void actionPerformed(ActionEvent e) {
169 if ("select".equals(e.getActionCommand()))
170 value = (AltosDevice)(list.getSelectedValue());
171 if ("manage".equals(e.getActionCommand())) {
172 AltosBTManage.show(frame, Altos.bt_known);
179 public static AltosDevice show (Component frameComp, int product) {
181 Frame frame = JOptionPane.getFrameForComponent(frameComp);
182 AltosDeviceDialog dialog;
184 dialog = new AltosDeviceDialog(frame, frameComp, product);
185 dialog.setVisible(true);
186 return dialog.getValue();