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