altoslib,altosuilib: Bump library version numbers
[fw/altos] / altosuilib / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altosuilib_12;
20
21 import javax.swing.*;
22 import java.awt.*;
23 import java.awt.event.*;
24
25 public abstract class AltosDeviceDialog extends AltosUIDialog implements ActionListener {
26
27         private AltosDevice             value;
28         private JList<AltosDevice>      list;
29         private JButton                 cancel_button;
30         private JButton                 select_button;
31         public Frame                    frame;
32         public int                      product;
33         public JPanel                   buttonPane;
34
35         public AltosDevice getValue() {
36                 return value;
37         }
38
39         public abstract AltosDevice[] devices();
40
41         public void update_devices() {
42                 AltosDevice[] devices = devices();
43                 list.setListData(devices);
44                 select_button.setEnabled(devices.length > 0);
45         }
46
47         public void add_bluetooth() { }
48
49         public AltosDeviceDialog (Frame in_frame, Component location, int in_product) {
50                 super(in_frame, "Device Selection", true);
51
52                 product = in_product;
53                 frame = in_frame;
54                 value = null;
55
56                 AltosDevice[]   devices = devices();
57
58                 cancel_button = new JButton("Cancel");
59                 cancel_button.setActionCommand("cancel");
60                 cancel_button.addActionListener(this);
61
62                 select_button = new JButton("Select");
63                 select_button.setActionCommand("select");
64                 select_button.addActionListener(this);
65                 if (devices.length == 0)
66                         select_button.setEnabled(false);
67                 getRootPane().setDefaultButton(select_button);
68
69                 list = new JList<AltosDevice>(devices) {
70                                 //Subclass JList to workaround bug 4832765, which can cause the
71                                 //scroll pane to not let the user easily scroll up to the beginning
72                                 //of the list.  An alternative would be to set the unitIncrement
73                                 //of the JScrollBar to a fixed value. You wouldn't get the nice
74                                 //aligned scrolling, but it should work.
75                                 public int getScrollableUnitIncrement(Rectangle visibleRect,
76                                                                       int orientation,
77                                                                       int direction) {
78                                         int row;
79                                         if (orientation == SwingConstants.VERTICAL &&
80                                             direction < 0 && (row = getFirstVisibleIndex()) != -1) {
81                                                 Rectangle r = getCellBounds(row, row);
82                                                 if ((r.y == visibleRect.y) && (row != 0))  {
83                                                         Point loc = r.getLocation();
84                                                         loc.y--;
85                                                         int prevIndex = locationToIndex(loc);
86                                                         Rectangle prevR = getCellBounds(prevIndex, prevIndex);
87
88                                                         if (prevR == null || prevR.y >= r.y) {
89                                                                 return 0;
90                                                         }
91                                                         return prevR.height;
92                                                 }
93                                         }
94                                         return super.getScrollableUnitIncrement(
95                                                 visibleRect, orientation, direction);
96                                 }
97                         };
98
99                 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
100                 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
101                 list.setVisibleRowCount(-1);
102                 list.addMouseListener(new MouseAdapter() {
103                                  public void mouseClicked(MouseEvent e) {
104                                          if (e.getClickCount() == 2) {
105                                                  select_button.doClick(); //emulate button click
106                                          }
107                                  }
108                         });
109                 JScrollPane listScroller = new JScrollPane(list);
110                 listScroller.setPreferredSize(new Dimension(400, 80));
111                 listScroller.setAlignmentX(LEFT_ALIGNMENT);
112
113                 //Create a container so that we can add a title around
114                 //the scroll pane.  Can't add a title directly to the
115                 //scroll pane because its background would be white.
116                 //Lay out the label and scroll pane from top to bottom.
117                 JPanel listPane = new JPanel();
118                 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
119
120                 JLabel label = new JLabel("Select Device");
121                 label.setLabelFor(list);
122                 listPane.add(label);
123                 listPane.add(Box.createRigidArea(new Dimension(0,5)));
124                 listPane.add(listScroller);
125                 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
126
127                 //Lay out the buttons from left to right.
128                 buttonPane = new JPanel();
129                 buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
130                 buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
131                 buttonPane.add(Box.createHorizontalGlue());
132                 buttonPane.add(cancel_button);
133                 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
134
135                 if (AltosUILib.has_bluetooth)
136                         add_bluetooth();
137
138                 buttonPane.add(select_button);
139
140                 //Put everything together, using the content pane's BorderLayout.
141                 Container contentPane = getContentPane();
142                 contentPane.add(listPane, BorderLayout.CENTER);
143                 contentPane.add(buttonPane, BorderLayout.PAGE_END);
144
145                 //Initialize values.
146                 if (devices != null && devices.length != 0)
147                         list.setSelectedValue(devices[0], true);
148                 pack();
149                 setLocationRelativeTo(location);
150         }
151
152         //Handle clicks on the Set and Cancel buttons.
153         public void actionPerformed(ActionEvent e) {
154                 if ("select".equals(e.getActionCommand())) {
155                         value = (AltosDevice)(list.getSelectedValue());
156                         setVisible(false);
157                 }
158                 if ("cancel".equals(e.getActionCommand()))
159                         setVisible(false);
160         }
161
162 }