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