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_13;
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 AltosDevice[] devices;
37 public AltosDevice getValue() {
41 public abstract AltosDevice[] devices();
43 public void update_devices() {
44 AltosDevice selected = list.getSelectedValue();
47 list.setListData(devices);
48 for (AltosDevice d : devices) {
49 if (d.equals(selected)) {
50 list.setSelectedValue(d, true);
54 select_button.setEnabled(devices.length > 0);
57 public void add_bluetooth() { }
59 public AltosDeviceDialog (Frame in_frame, Component location, int in_product) {
60 super(in_frame, "Device Selection", true);
66 cancel_button = new JButton("Cancel");
67 cancel_button.setActionCommand("cancel");
68 cancel_button.addActionListener(this);
70 select_button = new JButton("Select");
71 select_button.setActionCommand("select");
72 select_button.addActionListener(this);
73 select_button.setEnabled(false);
74 getRootPane().setDefaultButton(select_button);
76 list = new JList<AltosDevice>(devices) {
77 //Subclass JList to workaround bug 4832765, which can cause the
78 //scroll pane to not let the user easily scroll up to the beginning
79 //of the list. An alternative would be to set the unitIncrement
80 //of the JScrollBar to a fixed value. You wouldn't get the nice
81 //aligned scrolling, but it should work.
82 public int getScrollableUnitIncrement(Rectangle visibleRect,
86 if (orientation == SwingConstants.VERTICAL &&
87 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
88 Rectangle r = getCellBounds(row, row);
89 if ((r.y == visibleRect.y) && (row != 0)) {
90 Point loc = r.getLocation();
92 int prevIndex = locationToIndex(loc);
93 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
95 if (prevR == null || prevR.y >= r.y) {
101 return super.getScrollableUnitIncrement(
102 visibleRect, orientation, direction);
106 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
107 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
108 list.setVisibleRowCount(-1);
109 list.addMouseListener(new MouseAdapter() {
110 public void mouseClicked(MouseEvent e) {
111 if (e.getClickCount() == 2) {
112 select_button.doClick(); //emulate button click
116 JScrollPane listScroller = new JScrollPane(list);
117 listScroller.setPreferredSize(new Dimension(400, 80));
118 listScroller.setAlignmentX(LEFT_ALIGNMENT);
120 //Create a container so that we can add a title around
121 //the scroll pane. Can't add a title directly to the
122 //scroll pane because its background would be white.
123 //Lay out the label and scroll pane from top to bottom.
124 JPanel listPane = new JPanel();
125 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
127 JLabel label = new JLabel("Select Device");
128 label.setLabelFor(list);
130 listPane.add(Box.createRigidArea(new Dimension(0,5)));
131 listPane.add(listScroller);
132 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
134 //Lay out the buttons from left to right.
135 buttonPane = new JPanel();
136 buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
137 buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
138 buttonPane.add(Box.createHorizontalGlue());
139 buttonPane.add(cancel_button);
140 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
142 if (AltosUILib.has_bluetooth)
145 buttonPane.add(select_button);
147 //Put everything together, using the content pane's BorderLayout.
148 Container contentPane = getContentPane();
149 contentPane.add(listPane, BorderLayout.CENTER);
150 contentPane.add(buttonPane, BorderLayout.PAGE_END);
154 if (devices != null && devices.length != 0)
155 list.setSelectedValue(devices[0], true);
157 setLocationRelativeTo(location);
159 timer = new Timer(1000, new ActionListener () {
160 public void actionPerformed(ActionEvent evt) {
165 addComponentListener(new ComponentListener() {
166 public void componentShown(ComponentEvent e) {
169 public void componentMoved(ComponentEvent e) {
171 public void componentResized(ComponentEvent e) {
173 public void componentHidden(ComponentEvent e) {
180 //Handle clicks on the Set and Cancel buttons.
181 public void actionPerformed(ActionEvent e) {
182 if ("select".equals(e.getActionCommand())) {
183 value = (AltosDevice)(list.getSelectedValue());
186 if ("cancel".equals(e.getActionCommand()))