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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altosuilib_12;
23 import java.awt.event.*;
25 public abstract class AltosDeviceDialog extends AltosUIDialog implements ActionListener {
27 private AltosDevice value;
28 private JList<AltosDevice> list;
29 private JButton cancel_button;
30 private JButton select_button;
33 public JPanel buttonPane;
35 public AltosDevice getValue() {
39 public abstract AltosDevice[] devices();
41 public void update_devices() {
42 AltosDevice[] devices = devices();
43 list.setListData(devices);
44 select_button.setEnabled(devices.length > 0);
47 public void add_bluetooth() { }
49 public AltosDeviceDialog (Frame in_frame, Component location, int in_product) {
50 super(in_frame, "Device Selection", true);
56 AltosDevice[] devices = devices();
58 cancel_button = new JButton("Cancel");
59 cancel_button.setActionCommand("cancel");
60 cancel_button.addActionListener(this);
62 select_button = new JButton("Select");
63 select_button.setActionCommand("select");
64 select_button.addActionListener(this);
65 if (devices.length == 0)
66 select_button.setEnabled(false);
67 getRootPane().setDefaultButton(select_button);
69 list = new JList<AltosDevice>(devices) {
70 //Subclass JList to workaround bug 4832765, which can cause the
71 //scroll pane to not let the user easily scroll up to the beginning
72 //of the list. An alternative would be to set the unitIncrement
73 //of the JScrollBar to a fixed value. You wouldn't get the nice
74 //aligned scrolling, but it should work.
75 public int getScrollableUnitIncrement(Rectangle visibleRect,
79 if (orientation == SwingConstants.VERTICAL &&
80 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
81 Rectangle r = getCellBounds(row, row);
82 if ((r.y == visibleRect.y) && (row != 0)) {
83 Point loc = r.getLocation();
85 int prevIndex = locationToIndex(loc);
86 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
88 if (prevR == null || prevR.y >= r.y) {
94 return super.getScrollableUnitIncrement(
95 visibleRect, orientation, direction);
99 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
100 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
101 list.setVisibleRowCount(-1);
102 list.addMouseListener(new MouseAdapter() {
103 public void mouseClicked(MouseEvent e) {
104 if (e.getClickCount() == 2) {
105 select_button.doClick(); //emulate button click
109 JScrollPane listScroller = new JScrollPane(list);
110 listScroller.setPreferredSize(new Dimension(400, 80));
111 listScroller.setAlignmentX(LEFT_ALIGNMENT);
113 //Create a container so that we can add a title around
114 //the scroll pane. Can't add a title directly to the
115 //scroll pane because its background would be white.
116 //Lay out the label and scroll pane from top to bottom.
117 JPanel listPane = new JPanel();
118 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
120 JLabel label = new JLabel("Select Device");
121 label.setLabelFor(list);
123 listPane.add(Box.createRigidArea(new Dimension(0,5)));
124 listPane.add(listScroller);
125 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
127 //Lay out the buttons from left to right.
128 buttonPane = new JPanel();
129 buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
130 buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
131 buttonPane.add(Box.createHorizontalGlue());
132 buttonPane.add(cancel_button);
133 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
135 if (AltosUILib.has_bluetooth)
138 buttonPane.add(select_button);
140 //Put everything together, using the content pane's BorderLayout.
141 Container contentPane = getContentPane();
142 contentPane.add(listPane, BorderLayout.CENTER);
143 contentPane.add(buttonPane, BorderLayout.PAGE_END);
146 if (devices != null && devices.length != 0)
147 list.setSelectedValue(devices[0], true);
149 setLocationRelativeTo(location);
152 //Handle clicks on the Set and Cancel buttons.
153 public void actionPerformed(ActionEvent e) {
154 if ("select".equals(e.getActionCommand())) {
155 value = (AltosDevice)(list.getSelectedValue());
158 if ("cancel".equals(e.getActionCommand()))