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