Bump java lib versions in preparation for 1.9.2
[fw/altos] / altosuilib / AltosDeviceDialog.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.altosuilib_14;
20
21 import javax.swing.*;
22 import java.awt.*;
23 import java.awt.event.*;
24
25 public abstract class AltosDeviceDialog extends AltosUIDialog implements ActionListener {
26
27         private AltosDevice             value;
28         private JList<AltosDevice>      list;
29         private JButton                 cancel_button;
30         private JButton                 select_button;
31         public Frame                    frame;
32         public int                      product;
33         public JPanel                   buttonPane;
34         private Timer                   timer;
35         AltosDevice[]                   devices;
36
37         public AltosDevice getValue() {
38                 return value;
39         }
40
41         public abstract AltosDevice[] devices();
42
43         public void update_devices() {
44                 AltosDevice selected = list.getSelectedValue();
45
46                 devices = devices();
47                 list.setListData(devices);
48                 for (AltosDevice d : devices) {
49                         if (d.equals(selected)) {
50                                 list.setSelectedValue(d, true);
51                                 break;
52                         }
53                 }
54                 select_button.setEnabled(devices.length > 0);
55         }
56
57         public void add_bluetooth() { }
58
59         public AltosDeviceDialog (Frame in_frame, Component location, int in_product) {
60                 super(in_frame, "Device Selection", true);
61
62                 product = in_product;
63                 frame = in_frame;
64                 value = null;
65
66                 cancel_button = new JButton("Cancel");
67                 cancel_button.setActionCommand("cancel");
68                 cancel_button.addActionListener(this);
69
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);
75
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,
83                                                                       int orientation,
84                                                                       int direction) {
85                                         int row;
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();
91                                                         loc.y--;
92                                                         int prevIndex = locationToIndex(loc);
93                                                         Rectangle prevR = getCellBounds(prevIndex, prevIndex);
94
95                                                         if (prevR == null || prevR.y >= r.y) {
96                                                                 return 0;
97                                                         }
98                                                         return prevR.height;
99                                                 }
100                                         }
101                                         return super.getScrollableUnitIncrement(
102                                                 visibleRect, orientation, direction);
103                                 }
104                         };
105
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
113                                          }
114                                  }
115                         });
116                 JScrollPane listScroller = new JScrollPane(list);
117                 listScroller.setPreferredSize(new Dimension(400, 80));
118                 listScroller.setAlignmentX(LEFT_ALIGNMENT);
119
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));
126
127                 JLabel label = new JLabel("Select Device");
128                 label.setLabelFor(list);
129                 listPane.add(label);
130                 listPane.add(Box.createRigidArea(new Dimension(0,5)));
131                 listPane.add(listScroller);
132                 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
133
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)));
141
142                 if (AltosUILib.has_bluetooth)
143                         add_bluetooth();
144
145                 buttonPane.add(select_button);
146
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);
151
152                 //Initialize values.
153                 update_devices();
154                 if (devices != null && devices.length != 0)
155                         list.setSelectedValue(devices[0], true);
156                 pack();
157                 setLocationRelativeTo(location);
158
159                 timer = new Timer(1000, new ActionListener () {
160                                 public void actionPerformed(ActionEvent evt) {
161                                         update_devices();
162                                 }
163                         });
164
165                 addComponentListener(new ComponentListener() {
166                                 public void componentShown(ComponentEvent e) {
167                                         timer.start();
168                                 }
169                                 public void componentMoved(ComponentEvent e) {
170                                 }
171                                 public void componentResized(ComponentEvent e) {
172                                 }
173                                 public void componentHidden(ComponentEvent e) {
174                                         timer.stop();
175                                 }
176                         });
177
178         }
179
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());
184                         setVisible(false);
185                 }
186                 if ("cancel".equals(e.getActionCommand()))
187                         setVisible(false);
188         }
189
190 }