altos: Rename telemetry to telemetry_orig
[fw/altos] / altosui / 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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.lang.*;
21 import java.util.*;
22 import javax.swing.*;
23 import java.awt.*;
24 import java.awt.event.*;
25 import libaltosJNI.*;
26
27 public class AltosDeviceDialog extends JDialog implements ActionListener {
28
29         private AltosDevice     value;
30         private JList           list;
31         private JButton         cancel_button;
32         private JButton         select_button;
33         private JButton         manage_bluetooth_button;
34         private Frame           frame;
35         private int             product;
36
37         private AltosDevice getValue() {
38                 return value;
39         }
40
41         private AltosDevice[] devices() {
42                 java.util.List<AltosDevice>     usb_devices = AltosUSBDevice.list(product);
43                 java.util.List<AltosDevice>     bt_devices = Altos.bt_known.list(product);
44                 AltosDevice[]                   devices = new AltosDevice[usb_devices.size() + bt_devices.size()];
45
46                 for (int i = 0; i < usb_devices.size(); i++)
47                         devices[i] = usb_devices.get(i);
48                 int off = usb_devices.size();
49                 for (int j = 0; j < bt_devices.size(); j++)
50                         devices[off + j] = bt_devices.get(j);
51                 return devices;
52         }
53
54         private void update_devices() {
55                 AltosDevice[] devices = devices();
56                 list.setListData(devices);
57                 select_button.setEnabled(devices.length > 0);
58         }
59
60         private AltosDeviceDialog (Frame in_frame, Component location, int in_product) {
61                 super(in_frame, "Device Selection", true);
62
63                 product = in_product;
64                 frame = in_frame;
65                 value = null;
66
67                 AltosDevice[]   devices = devices();
68
69                 cancel_button = new JButton("Cancel");
70                 cancel_button.setActionCommand("cancel");
71                 cancel_button.addActionListener(this);
72
73                 manage_bluetooth_button = new JButton("Manage Bluetooth");
74                 manage_bluetooth_button.setActionCommand("manage");
75                 manage_bluetooth_button.addActionListener(this);
76
77                 select_button = new JButton("Select");
78                 select_button.setActionCommand("select");
79                 select_button.addActionListener(this);
80                 if (devices.length == 0)
81                         select_button.setEnabled(false);
82                 getRootPane().setDefaultButton(select_button);
83
84                 list = new JList(devices) {
85                                 //Subclass JList to workaround bug 4832765, which can cause the
86                                 //scroll pane to not let the user easily scroll up to the beginning
87                                 //of the list.  An alternative would be to set the unitIncrement
88                                 //of the JScrollBar to a fixed value. You wouldn't get the nice
89                                 //aligned scrolling, but it should work.
90                                 public int getScrollableUnitIncrement(Rectangle visibleRect,
91                                                                       int orientation,
92                                                                       int direction) {
93                                         int row;
94                                         if (orientation == SwingConstants.VERTICAL &&
95                                             direction < 0 && (row = getFirstVisibleIndex()) != -1) {
96                                                 Rectangle r = getCellBounds(row, row);
97                                                 if ((r.y == visibleRect.y) && (row != 0))  {
98                                                         Point loc = r.getLocation();
99                                                         loc.y--;
100                                                         int prevIndex = locationToIndex(loc);
101                                                         Rectangle prevR = getCellBounds(prevIndex, prevIndex);
102
103                                                         if (prevR == null || prevR.y >= r.y) {
104                                                                 return 0;
105                                                         }
106                                                         return prevR.height;
107                                                 }
108                                         }
109                                         return super.getScrollableUnitIncrement(
110                                                 visibleRect, orientation, direction);
111                                 }
112                         };
113
114                 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
115                 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
116                 list.setVisibleRowCount(-1);
117                 list.addMouseListener(new MouseAdapter() {
118                                  public void mouseClicked(MouseEvent e) {
119                                          if (e.getClickCount() == 2) {
120                                                  select_button.doClick(); //emulate button click
121                                          }
122                                  }
123                         });
124                 JScrollPane listScroller = new JScrollPane(list);
125                 listScroller.setPreferredSize(new Dimension(400, 80));
126                 listScroller.setAlignmentX(LEFT_ALIGNMENT);
127
128                 //Create a container so that we can add a title around
129                 //the scroll pane.  Can't add a title directly to the
130                 //scroll pane because its background would be white.
131                 //Lay out the label and scroll pane from top to bottom.
132                 JPanel listPane = new JPanel();
133                 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
134
135                 JLabel label = new JLabel("Select Device");
136                 label.setLabelFor(list);
137                 listPane.add(label);
138                 listPane.add(Box.createRigidArea(new Dimension(0,5)));
139                 listPane.add(listScroller);
140                 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
141
142                 //Lay out the buttons from left to right.
143                 JPanel buttonPane = new JPanel();
144                 buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
145                 buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
146                 buttonPane.add(Box.createHorizontalGlue());
147                 buttonPane.add(cancel_button);
148                 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
149                 buttonPane.add(manage_bluetooth_button);
150                 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
151                 buttonPane.add(select_button);
152
153                 //Put everything together, using the content pane's BorderLayout.
154                 Container contentPane = getContentPane();
155                 contentPane.add(listPane, BorderLayout.CENTER);
156                 contentPane.add(buttonPane, BorderLayout.PAGE_END);
157
158                 //Initialize values.
159                 if (devices != null && devices.length != 0)
160                         list.setSelectedValue(devices[0], true);
161                 pack();
162                 setLocationRelativeTo(location);
163         }
164
165         //Handle clicks on the Set and Cancel buttons.
166         public void actionPerformed(ActionEvent e) {
167                 if ("select".equals(e.getActionCommand()))
168                         value = (AltosDevice)(list.getSelectedValue());
169                 if ("manage".equals(e.getActionCommand())) {
170                         AltosBTManage.show(frame, Altos.bt_known);
171                         update_devices();
172                         return;
173                 }
174                 setVisible(false);
175         }
176
177         public static AltosDevice show (Component frameComp, int product) {
178
179                 Frame                           frame = JOptionPane.getFrameForComponent(frameComp);
180                 AltosDeviceDialog       dialog;
181
182                 dialog = new AltosDeviceDialog(frame, frameComp, product);
183                 dialog.setVisible(true);
184                 return dialog.getValue();
185         }
186 }