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.*;
25 import libaltosJNI.libaltos;
26 import libaltosJNI.altos_device;
27 import libaltosJNI.SWIGTYPE_p_altos_file;
28 import libaltosJNI.SWIGTYPE_p_altos_list;
29 import altosui.AltosDevice;
31 public class AltosDeviceDialog extends JDialog implements ActionListener {
33 private static AltosDeviceDialog dialog;
34 private static altos_device value = null;
37 public static altos_device show (Component frameComp, String product) {
39 Frame frame = JOptionPane.getFrameForComponent(frameComp);
40 AltosDevice[] devices;
41 devices = AltosDevice.list(product);
43 if (devices != null & devices.length > 0) {
45 dialog = new AltosDeviceDialog(frame, frameComp,
49 dialog.setVisible(true);
52 JOptionPane.showMessageDialog(frame,
53 "No AltOS devices available",
55 JOptionPane.ERROR_MESSAGE);
60 private AltosDeviceDialog (Frame frame, Component location,
61 AltosDevice[] devices,
62 AltosDevice initial) {
63 super(frame, "Device Selection", true);
67 JButton cancelButton = new JButton("Cancel");
68 cancelButton.addActionListener(this);
70 final JButton selectButton = new JButton("Select");
71 selectButton.setActionCommand("select");
72 selectButton.addActionListener(this);
73 getRootPane().setDefaultButton(selectButton);
75 list = new JList(devices) {
76 //Subclass JList to workaround bug 4832765, which can cause the
77 //scroll pane to not let the user easily scroll up to the beginning
78 //of the list. An alternative would be to set the unitIncrement
79 //of the JScrollBar to a fixed value. You wouldn't get the nice
80 //aligned scrolling, but it should work.
81 public int getScrollableUnitIncrement(Rectangle visibleRect,
85 if (orientation == SwingConstants.VERTICAL &&
86 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
87 Rectangle r = getCellBounds(row, row);
88 if ((r.y == visibleRect.y) && (row != 0)) {
89 Point loc = r.getLocation();
91 int prevIndex = locationToIndex(loc);
92 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
94 if (prevR == null || prevR.y >= r.y) {
100 return super.getScrollableUnitIncrement(
101 visibleRect, orientation, direction);
105 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
106 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
107 list.setVisibleRowCount(-1);
108 list.addMouseListener(new MouseAdapter() {
109 public void mouseClicked(MouseEvent e) {
110 if (e.getClickCount() == 2) {
111 selectButton.doClick(); //emulate button click
115 JScrollPane listScroller = new JScrollPane(list);
116 listScroller.setPreferredSize(new Dimension(400, 80));
117 listScroller.setAlignmentX(LEFT_ALIGNMENT);
119 //Create a container so that we can add a title around
120 //the scroll pane. Can't add a title directly to the
121 //scroll pane because its background would be white.
122 //Lay out the label and scroll pane from top to bottom.
123 JPanel listPane = new JPanel();
124 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
126 JLabel label = new JLabel("Select Device");
127 label.setLabelFor(list);
129 listPane.add(Box.createRigidArea(new Dimension(0,5)));
130 listPane.add(listScroller);
131 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
133 //Lay out the buttons from left to right.
134 JPanel buttonPane = new JPanel();
135 buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
136 buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
137 buttonPane.add(Box.createHorizontalGlue());
138 buttonPane.add(cancelButton);
139 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
140 buttonPane.add(selectButton);
142 //Put everything together, using the content pane's BorderLayout.
143 Container contentPane = getContentPane();
144 contentPane.add(listPane, BorderLayout.CENTER);
145 contentPane.add(buttonPane, BorderLayout.PAGE_END);
148 list.setSelectedValue(initial, true);
150 setLocationRelativeTo(location);
153 //Handle clicks on the Set and Cancel buttons.
154 public void actionPerformed(ActionEvent e) {
155 if ("select".equals(e.getActionCommand())) {
156 System.out.printf("got select action\n");
157 AltosDeviceDialog.value = (altos_device)(list.getSelectedValue());
159 AltosDeviceDialog.dialog.setVisible(false);