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;
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;
30 private JButton manage_bluetooth_button;
34 public AltosDevice getValue() {
38 public abstract AltosDevice[] devices();
40 private void update_devices() {
41 AltosDevice[] devices = devices();
42 list.setListData(devices);
43 select_button.setEnabled(devices.length > 0);
46 public AltosDeviceDialog (Frame in_frame, Component location, int in_product) {
47 super(in_frame, "Device Selection", true);
53 AltosDevice[] devices = devices();
55 cancel_button = new JButton("Cancel");
56 cancel_button.setActionCommand("cancel");
57 cancel_button.addActionListener(this);
59 // manage_bluetooth_button = new JButton("Manage Bluetooth");
60 // manage_bluetooth_button.setActionCommand("manage");
61 // manage_bluetooth_button.addActionListener(this);
63 select_button = new JButton("Select");
64 select_button.setActionCommand("select");
65 select_button.addActionListener(this);
66 if (devices.length == 0)
67 select_button.setEnabled(false);
68 getRootPane().setDefaultButton(select_button);
70 list = new JList(devices) {
71 //Subclass JList to workaround bug 4832765, which can cause the
72 //scroll pane to not let the user easily scroll up to the beginning
73 //of the list. An alternative would be to set the unitIncrement
74 //of the JScrollBar to a fixed value. You wouldn't get the nice
75 //aligned scrolling, but it should work.
76 public int getScrollableUnitIncrement(Rectangle visibleRect,
80 if (orientation == SwingConstants.VERTICAL &&
81 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
82 Rectangle r = getCellBounds(row, row);
83 if ((r.y == visibleRect.y) && (row != 0)) {
84 Point loc = r.getLocation();
86 int prevIndex = locationToIndex(loc);
87 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
89 if (prevR == null || prevR.y >= r.y) {
95 return super.getScrollableUnitIncrement(
96 visibleRect, orientation, direction);
100 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
101 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
102 list.setVisibleRowCount(-1);
103 list.addMouseListener(new MouseAdapter() {
104 public void mouseClicked(MouseEvent e) {
105 if (e.getClickCount() == 2) {
106 select_button.doClick(); //emulate button click
110 JScrollPane listScroller = new JScrollPane(list);
111 listScroller.setPreferredSize(new Dimension(400, 80));
112 listScroller.setAlignmentX(LEFT_ALIGNMENT);
114 //Create a container so that we can add a title around
115 //the scroll pane. Can't add a title directly to the
116 //scroll pane because its background would be white.
117 //Lay out the label and scroll pane from top to bottom.
118 JPanel listPane = new JPanel();
119 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
121 JLabel label = new JLabel("Select Device");
122 label.setLabelFor(list);
124 listPane.add(Box.createRigidArea(new Dimension(0,5)));
125 listPane.add(listScroller);
126 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
128 //Lay out the buttons from left to right.
129 JPanel buttonPane = new JPanel();
130 buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
131 buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
132 buttonPane.add(Box.createHorizontalGlue());
133 buttonPane.add(cancel_button);
134 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
135 // buttonPane.add(manage_bluetooth_button);
136 // buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
137 buttonPane.add(select_button);
139 //Put everything together, using the content pane's BorderLayout.
140 Container contentPane = getContentPane();
141 contentPane.add(listPane, BorderLayout.CENTER);
142 contentPane.add(buttonPane, BorderLayout.PAGE_END);
145 if (devices != null && devices.length != 0)
146 list.setSelectedValue(devices[0], true);
148 setLocationRelativeTo(location);
151 //Handle clicks on the Set and Cancel buttons.
152 public void actionPerformed(ActionEvent e) {
153 if ("select".equals(e.getActionCommand()))
154 value = (AltosDevice)(list.getSelectedValue());
155 // if ("manage".equals(e.getActionCommand())) {
156 // AltosBTManage.show(frame, AltosBTKnown.bt_known());