altoslib: Publish mapping from product name back to USB id
[fw/altos] / altosuilib / AltosUIConfigure.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 java.awt.*;
21 import java.awt.event.*;
22 import java.beans.*;
23 import javax.swing.*;
24 import javax.swing.event.*;
25
26 class DelegatingRenderer implements ListCellRenderer {
27
28         // ...
29         public static void install(JComboBox comboBox) {
30                 DelegatingRenderer renderer = new DelegatingRenderer(comboBox);
31                 renderer.initialise();
32                 comboBox.setRenderer(renderer);
33         }
34
35         // ...
36         private final JComboBox comboBox;
37
38         // ...
39         private ListCellRenderer delegate;
40
41         // ...
42         private DelegatingRenderer(JComboBox comboBox) {
43                 this.comboBox = comboBox;
44         }
45
46         // ...
47         private void initialise() {
48                 delegate = new JComboBox().getRenderer();
49                 comboBox.addPropertyChangeListener("UI", new PropertyChangeListener() {
50
51                                 public void propertyChange(PropertyChangeEvent evt) {
52                                         delegate = new JComboBox().getRenderer();
53                                 }
54                         });
55         }
56
57         // ...
58         public Component getListCellRendererComponent(JList list,
59                                                       Object value, int index, boolean isSelected, boolean cellHasFocus) {
60
61                 return delegate.getListCellRendererComponent(list,
62                                                              ((UIManager.LookAndFeelInfo) value).getName(),
63                                                              index, isSelected, cellHasFocus);
64         }
65 }
66
67 public class AltosUIConfigure
68         extends AltosUIDialog
69 {
70         public JFrame           owner;
71         public Container        pane;
72
73         public int              row;
74
75         final static String[] font_size_names = { "Small", "Medium", "Large" };
76
77         public GridBagConstraints constraints (int x, int width, int fill) {
78                 GridBagConstraints c = new GridBagConstraints();
79                 Insets insets = new Insets(4, 4, 4, 4);
80
81                 c.insets = insets;
82                 c.fill = fill;
83                 if (width == 3)
84                         c.anchor = GridBagConstraints.CENTER;
85                 else if (x == 2)
86                         c.anchor = GridBagConstraints.EAST;
87                 else
88                         c.anchor = GridBagConstraints.WEST;
89                 c.gridx = x;
90                 c.gridwidth = width;
91                 c.gridy = row;
92                 return c;
93         }
94
95         public GridBagConstraints constraints(int x, int width) {
96                 return constraints(x, width, GridBagConstraints.NONE);
97         }
98
99         public void add_voice() {
100         }
101
102         public void add_log_dir() {
103                 /* Log directory settings */
104                 pane.add(new JLabel("Log Directory"), constraints(0, 1));
105
106                 final JButton configure_log = new JButton(AltosUIPreferences.logdir().getPath());
107                 configure_log.addActionListener(new ActionListener() {
108                                 public void actionPerformed(ActionEvent e) {
109                                         AltosUIPreferences.ConfigureLog();
110                                         configure_log.setText(AltosUIPreferences.logdir().getPath());
111                                 }
112                         });
113                 pane.add(configure_log, constraints(1, 2));
114                 configure_log.setToolTipText("Which directory flight logs are stored in");
115                 row++;
116         }
117
118         public void add_callsign() {
119         }
120
121         public void add_units() {
122                 /* Imperial units setting */
123                 pane.add(new JLabel("Imperial Units"), constraints(0, 1));
124
125                 JRadioButton imperial_units = new JRadioButton("Enable", AltosUIPreferences.imperial_units());
126                 imperial_units.addActionListener(new ActionListener() {
127                                 public void actionPerformed(ActionEvent e) {
128                                         JRadioButton item = (JRadioButton) e.getSource();
129                                         boolean enabled = item.isSelected();
130                                         AltosUIPreferences.set_imperial_units(enabled);
131                                 }
132                         });
133                 imperial_units.setToolTipText("Use Imperial units instead of metric");
134                 pane.add(imperial_units, constraints(1, 2));
135                 row++;
136         }
137
138         public void add_font_size() {
139                 /* Font size setting */
140                 pane.add(new JLabel("Font size"), constraints(0, 1));
141
142                 final JComboBox font_size_value = new JComboBox(font_size_names);
143                 int font_size = AltosUIPreferences.font_size();
144                 font_size_value.setSelectedIndex(font_size - AltosUILib.font_size_small);
145                 font_size_value.addActionListener(new ActionListener() {
146                                 public void actionPerformed(ActionEvent e) {
147                                         int     size = font_size_value.getSelectedIndex() + AltosUILib.font_size_small;
148
149                                         AltosUIPreferences.set_font_size(size);
150                                 }
151                         });
152                 pane.add(font_size_value, constraints(1, 2, GridBagConstraints.BOTH));
153                 font_size_value.setToolTipText("Font size used in telemetry window");
154                 row++;
155         }
156
157         public void add_look_and_feel() {
158                 /* Look & Feel setting */
159                 pane.add(new JLabel("Look & feel"), constraints(0, 1));
160
161                 /*
162                 class LookAndFeelRenderer extends BasicComboBoxRenderer implements ListCellRenderer {
163
164                         public LookAndFeelRenderer() {
165                                 super();
166                         }
167
168                         public Component getListCellRendererComponent(
169                                 JList list,
170                                 Object value,
171                                 int index,
172                                 boolean isSelected,
173                                 boolean cellHasFocus)
174                         {
175                                 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
176                                 setText(((UIManager.LookAndFeelInfo) value).getName());
177                                 return this;
178                         }
179                 }
180                 */
181
182                 final UIManager.LookAndFeelInfo[] look_and_feels = UIManager.getInstalledLookAndFeels();
183
184                 final JComboBox look_and_feel_value = new JComboBox(look_and_feels);
185
186                 DelegatingRenderer.install(look_and_feel_value);
187
188                 String look_and_feel  = AltosUIPreferences.look_and_feel();
189                 for (int i = 0; i < look_and_feels.length; i++)
190                         if (look_and_feel.equals(look_and_feels[i].getClassName()))
191                                 look_and_feel_value.setSelectedIndex(i);
192
193                 look_and_feel_value.addActionListener(new ActionListener() {
194                                 public void actionPerformed(ActionEvent e) {
195                                         int     id = look_and_feel_value.getSelectedIndex();
196
197                                         AltosUIPreferences.set_look_and_feel(look_and_feels[id].getClassName());
198                                 }
199                         });
200                 pane.add(look_and_feel_value, constraints(1, 2, GridBagConstraints.BOTH));
201                 look_and_feel_value.setToolTipText("Look&feel used for new windows");
202                 row++;
203         }
204
205         public void add_position () {
206         }
207
208         public void add_serial_debug() {
209                 /* Serial debug setting */
210                 pane.add(new JLabel("Serial Debug"), constraints(0, 1));
211
212                 JRadioButton serial_debug = new JRadioButton("Enable", AltosUIPreferences.serial_debug());
213                 serial_debug.addActionListener(new ActionListener() {
214                                 public void actionPerformed(ActionEvent e) {
215                                         JRadioButton item = (JRadioButton) e.getSource();
216                                         boolean enabled = item.isSelected();
217                                         AltosUIPreferences.set_serial_debug(enabled);
218                                 }
219                         });
220                 serial_debug.setToolTipText("Enable/Disable USB I/O getting sent to the console");
221                 pane.add(serial_debug, constraints(1,2));
222                 row++;
223         }
224
225         public void add_bluetooth() {
226         }
227
228         public void add_frequencies() {
229         }
230
231         public AltosUIConfigure(JFrame in_owner) {
232                 super(in_owner, "Configure AltosUI", false);
233
234                 owner = in_owner;
235                 pane = getContentPane();
236                 pane.setLayout(new GridBagLayout());
237
238                 row = 0;
239
240                 /* Nice label at the top */
241                 pane.add(new JLabel ("Configure AltOS UI"),
242                          constraints(0, 3));
243                 row++;
244
245                 pane.add(new JLabel (String.format("AltOS version %s", AltosUIVersion.version)),
246                          constraints(0, 3));
247                 row++;
248
249                 add_voice();
250                 add_log_dir();
251                 add_callsign();
252                 add_units();
253                 add_serial_debug();
254                 add_font_size();
255                 add_look_and_feel();
256                 add_position();
257                 add_bluetooth();
258                 add_frequencies();
259
260                 /* And a close button at the bottom */
261                 JButton close = new JButton("Close");
262                 close.addActionListener(new ActionListener() {
263                                 public void actionPerformed(ActionEvent e) {
264                                         setVisible(false);
265                                 }
266                         });
267                 pane.add(close, constraints(0, 3));
268
269                 pack();
270                 setLocationRelativeTo(owner);
271                 setVisible(true);
272         }
273 }