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.
18 package org.altusmetrum.altosuilib_1;
22 import java.awt.event.*;
24 public abstract class AltosDeviceDialog extends AltosUIDialog implements ActionListener {
26 private AltosDevice value;
28 private JButton cancel_button;
29 private JButton select_button;
32 public JPanel buttonPane;
34 public AltosDevice getValue() {
38 public abstract AltosDevice[] devices();
40 public void update_devices() {
41 AltosDevice[] devices = devices();
42 list.setListData(devices);
43 select_button.setEnabled(devices.length > 0);
46 public void add_bluetooth() { }
48 public AltosDeviceDialog (Frame in_frame, Component location, int in_product) {
49 super(in_frame, "Device Selection", true);
55 AltosDevice[] devices = devices();
57 cancel_button = new JButton("Cancel");
58 cancel_button.setActionCommand("cancel");
59 cancel_button.addActionListener(this);
61 select_button = new JButton("Select");
62 select_button.setActionCommand("select");
63 select_button.addActionListener(this);
64 if (devices.length == 0)
65 select_button.setEnabled(false);
66 getRootPane().setDefaultButton(select_button);
68 list = new JList(devices) {
69 //Subclass JList to workaround bug 4832765, which can cause the
70 //scroll pane to not let the user easily scroll up to the beginning
71 //of the list. An alternative would be to set the unitIncrement
72 //of the JScrollBar to a fixed value. You wouldn't get the nice
73 //aligned scrolling, but it should work.
74 public int getScrollableUnitIncrement(Rectangle visibleRect,
78 if (orientation == SwingConstants.VERTICAL &&
79 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
80 Rectangle r = getCellBounds(row, row);
81 if ((r.y == visibleRect.y) && (row != 0)) {
82 Point loc = r.getLocation();
84 int prevIndex = locationToIndex(loc);
85 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
87 if (prevR == null || prevR.y >= r.y) {
93 return super.getScrollableUnitIncrement(
94 visibleRect, orientation, direction);
98 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
99 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
100 list.setVisibleRowCount(-1);
101 list.addMouseListener(new MouseAdapter() {
102 public void mouseClicked(MouseEvent e) {
103 if (e.getClickCount() == 2) {
104 select_button.doClick(); //emulate button click
108 JScrollPane listScroller = new JScrollPane(list);
109 listScroller.setPreferredSize(new Dimension(400, 80));
110 listScroller.setAlignmentX(LEFT_ALIGNMENT);
112 //Create a container so that we can add a title around
113 //the scroll pane. Can't add a title directly to the
114 //scroll pane because its background would be white.
115 //Lay out the label and scroll pane from top to bottom.
116 JPanel listPane = new JPanel();
117 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
119 JLabel label = new JLabel("Select Device");
120 label.setLabelFor(list);
122 listPane.add(Box.createRigidArea(new Dimension(0,5)));
123 listPane.add(listScroller);
124 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
126 //Lay out the buttons from left to right.
127 buttonPane = new JPanel();
128 buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
129 buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
130 buttonPane.add(Box.createHorizontalGlue());
131 buttonPane.add(cancel_button);
132 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
136 buttonPane.add(select_button);
138 //Put everything together, using the content pane's BorderLayout.
139 Container contentPane = getContentPane();
140 contentPane.add(listPane, BorderLayout.CENTER);
141 contentPane.add(buttonPane, BorderLayout.PAGE_END);
144 if (devices != null && devices.length != 0)
145 list.setSelectedValue(devices[0], true);
147 setLocationRelativeTo(location);
150 //Handle clicks on the Set and Cancel buttons.
151 public void actionPerformed(ActionEvent e) {
152 if ("select".equals(e.getActionCommand())) {
153 value = (AltosDevice)(list.getSelectedValue());
156 if ("cancel".equals(e.getActionCommand()))