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;
34 // private JButton manage_bluetooth_button;
38 private AltosDevice getValue() {
42 private AltosDevice[] devices() {
43 java.util.List<AltosDevice> usb_devices = AltosUSBDevice.list(product);
44 int num_devices = usb_devices.size();
46 // java.util.List<AltosDevice> bt_devices = Altos.bt_known.list(product);
47 // num_devices += bt_devices.size();
48 AltosDevice[] devices = new AltosDevice[num_devices];
50 for (int i = 0; i < usb_devices.size(); i++)
51 devices[i] = usb_devices.get(i);
53 // int off = usb_devices.size();
54 // for (int j = 0; j < bt_devices.size(); j++)
55 // devices[off + j] = bt_devices.get(j);
59 private void update_devices() {
60 AltosDevice[] devices = devices();
61 list.setListData(devices);
62 select_button.setEnabled(devices.length > 0);
65 private AltosDeviceDialog (Frame in_frame, Component location, int in_product) {
66 super(in_frame, "Device Selection", true);
72 AltosDevice[] devices = devices();
74 cancel_button = new JButton("Cancel");
75 cancel_button.setActionCommand("cancel");
76 cancel_button.addActionListener(this);
79 // manage_bluetooth_button = new JButton("Manage Bluetooth");
80 // manage_bluetooth_button.setActionCommand("manage");
81 // manage_bluetooth_button.addActionListener(this);
83 select_button = new JButton("Select");
84 select_button.setActionCommand("select");
85 select_button.addActionListener(this);
86 if (devices.length == 0)
87 select_button.setEnabled(false);
88 getRootPane().setDefaultButton(select_button);
90 list = new JList(devices) {
91 //Subclass JList to workaround bug 4832765, which can cause the
92 //scroll pane to not let the user easily scroll up to the beginning
93 //of the list. An alternative would be to set the unitIncrement
94 //of the JScrollBar to a fixed value. You wouldn't get the nice
95 //aligned scrolling, but it should work.
96 public int getScrollableUnitIncrement(Rectangle visibleRect,
100 if (orientation == SwingConstants.VERTICAL &&
101 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
102 Rectangle r = getCellBounds(row, row);
103 if ((r.y == visibleRect.y) && (row != 0)) {
104 Point loc = r.getLocation();
106 int prevIndex = locationToIndex(loc);
107 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
109 if (prevR == null || prevR.y >= r.y) {
115 return super.getScrollableUnitIncrement(
116 visibleRect, orientation, direction);
120 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
121 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
122 list.setVisibleRowCount(-1);
123 list.addMouseListener(new MouseAdapter() {
124 public void mouseClicked(MouseEvent e) {
125 if (e.getClickCount() == 2) {
126 select_button.doClick(); //emulate button click
130 JScrollPane listScroller = new JScrollPane(list);
131 listScroller.setPreferredSize(new Dimension(400, 80));
132 listScroller.setAlignmentX(LEFT_ALIGNMENT);
134 //Create a container so that we can add a title around
135 //the scroll pane. Can't add a title directly to the
136 //scroll pane because its background would be white.
137 //Lay out the label and scroll pane from top to bottom.
138 JPanel listPane = new JPanel();
139 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
141 JLabel label = new JLabel("Select Device");
142 label.setLabelFor(list);
144 listPane.add(Box.createRigidArea(new Dimension(0,5)));
145 listPane.add(listScroller);
146 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
148 //Lay out the buttons from left to right.
149 JPanel buttonPane = new JPanel();
150 buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
151 buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
152 buttonPane.add(Box.createHorizontalGlue());
153 buttonPane.add(cancel_button);
154 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
156 // buttonPane.add(manage_bluetooth_button);
157 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
158 buttonPane.add(select_button);
160 //Put everything together, using the content pane's BorderLayout.
161 Container contentPane = getContentPane();
162 contentPane.add(listPane, BorderLayout.CENTER);
163 contentPane.add(buttonPane, BorderLayout.PAGE_END);
166 if (devices != null && devices.length != 0)
167 list.setSelectedValue(devices[0], true);
169 setLocationRelativeTo(location);
172 //Handle clicks on the Set and Cancel buttons.
173 public void actionPerformed(ActionEvent e) {
174 if ("select".equals(e.getActionCommand()))
175 value = (AltosDevice)(list.getSelectedValue());
177 // if ("manage".equals(e.getActionCommand())) {
178 // AltosBTManage.show(frame, Altos.bt_known);
185 public static AltosDevice show (Component frameComp, int product) {
187 Frame frame = JOptionPane.getFrameForComponent(frameComp);
188 AltosDeviceDialog dialog;
190 dialog = new AltosDeviceDialog(frame, frameComp, product);
191 dialog.setVisible(true);
192 return dialog.getValue();