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