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