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