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