Merge remote-tracking branch 'origin/master' into micropeak-logging
[fw/altos] / altosuilib / AltosConfigureUI.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;
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 AltosConfigureUI
68         extends AltosUIDialog
69         implements DocumentListener
70 {
71         JFrame          owner;
72         Container       pane;
73
74         JRadioButton    enable_voice;
75         JButton         test_voice;
76         JButton         close;
77
78         JButton         configure_log;
79         JTextField      log_directory;
80
81         JLabel          callsign_label;
82         JTextField      callsign_value;
83
84         JRadioButton    imperial_units;
85
86         JLabel          font_size_label;
87         JComboBox       font_size_value;
88
89         JLabel          look_and_feel_label;
90         JComboBox       look_and_feel_value;
91
92         JRadioButton    serial_debug;
93
94         JButton         manage_bluetooth;
95         JButton         manage_frequencies;
96
97         int             row;
98
99         final static String[] font_size_names = { "Small", "Medium", "Large" };
100
101         /* DocumentListener interface methods */
102         public void changedUpdate(DocumentEvent e) {
103                 if (callsign_value != null) 
104                         AltosUIPreferences.set_callsign(callsign_value.getText());
105         }
106
107         public void insertUpdate(DocumentEvent e) {
108                 changedUpdate(e);
109         }
110
111         public void removeUpdate(DocumentEvent e) {
112                 changedUpdate(e);
113         }
114
115         public GridBagConstraints constraints (int x, int width, int fill) {
116                 GridBagConstraints c = new GridBagConstraints();
117                 Insets insets = new Insets(4, 4, 4, 4);
118
119                 c.insets = insets;
120                 c.fill = fill;
121                 if (width == 3)
122                         c.anchor = GridBagConstraints.CENTER;
123                 else
124                         c.anchor = GridBagConstraints.WEST;
125                 c.gridx = x;
126                 c.gridwidth = width;
127                 c.gridy = row;
128                 return c;
129         }
130
131         public GridBagConstraints constraints(int x, int width) {
132                 return constraints(x, width, GridBagConstraints.NONE);
133         }
134
135         public void add_voice() {
136 //              GridBagConstraints c = new GridBagConstraints();
137 //
138 //              /* Voice settings */
139 //              c.gridx = 0;
140 //              c.gridy = row;
141 //              c.gridwidth = 1;
142 //              c.fill = GridBagConstraints.NONE;
143 //              c.anchor = GridBagConstraints.WEST;
144 //              pane.add(new JLabel("Voice"), c);
145 //
146 //              enable_voice = new JRadioButton("Enable", AltosUIPreferences.voice());
147 //              enable_voice.addActionListener(new ActionListener() {
148 //                              public void actionPerformed(ActionEvent e) {
149 //                                      JRadioButton item = (JRadioButton) e.getSource();
150 //                                      boolean enabled = item.isSelected();
151 //                                      AltosUIPreferences.set_voice(enabled);
152 //                                      if (enabled)
153 //                                              voice.speak_always("Enable voice.");
154 //                                      else
155 //                                              voice.speak_always("Disable voice.");
156 //                              }
157 //                      });
158 //              c.gridx = 1;
159 //              c.gridy = row;
160 //              c.gridwidth = 1;
161 //              c.weightx = 1;
162 //              c.fill = GridBagConstraints.NONE;
163 //              c.anchor = GridBagConstraints.WEST;
164 //              pane.add(enable_voice, c);
165 //              enable_voice.setToolTipText("Enable/Disable all audio in-flight announcements");
166 //
167 //              c.gridx = 2;
168 //              c.gridy = row++;
169 //              c.gridwidth = 1;
170 //              c.weightx = 1;
171 //              c.fill = GridBagConstraints.NONE;
172 //              c.anchor = GridBagConstraints.EAST;
173 //              test_voice = new JButton("Test Voice");
174 //              test_voice.addActionListener(new ActionListener() {
175 //                              public void actionPerformed(ActionEvent e) {
176 //                                      voice.speak("That's one small step for man; one giant leap for mankind.");
177 //                              }
178 //                      });
179 //              pane.add(test_voice, c);
180 //              test_voice.setToolTipText("Play a stock audio clip to check volume");
181 //              row++;
182         }
183
184         public void add_log_dir() {
185                 /* Log directory settings */
186                 pane.add(new JLabel("Log Directory"), constraints(0, 1));
187
188                 configure_log = new JButton(AltosUIPreferences.logdir().getPath());
189                 configure_log.addActionListener(new ActionListener() {
190                                 public void actionPerformed(ActionEvent e) {
191                                         AltosUIPreferences.ConfigureLog();
192                                         configure_log.setText(AltosUIPreferences.logdir().getPath());
193                                 }
194                         });
195                 pane.add(configure_log, constraints(1, 2));
196                 configure_log.setToolTipText("Which directory flight logs are stored in");
197                 row++;
198         }
199
200         public void add_callsign() {
201 //              /* Callsign setting */
202 //              pane.add(new JLabel("Callsign"), constraints(0, 1));
203 //
204 //              callsign_value = new JTextField(AltosUIPreferences.callsign());
205 //              callsign_value.getDocument().addDocumentListener(this);
206 //              callsign_value.setToolTipText("Callsign sent in packet mode");
207 //              pane.add(callsign_value, constraints(1, 2, GridBagConstraints.BOTH));
208 //              row++;
209         }
210
211         public void add_units() {
212                 /* Imperial units setting */
213                 pane.add(new JLabel("Imperial Units"), constraints(0, 1));
214
215                 imperial_units = new JRadioButton("Enable", AltosUIPreferences.imperial_units());
216                 imperial_units.addActionListener(new ActionListener() {
217                                 public void actionPerformed(ActionEvent e) {
218                                         JRadioButton item = (JRadioButton) e.getSource();
219                                         boolean enabled = item.isSelected();
220                                         AltosUIPreferences.set_imperial_units(enabled);
221                                 }
222                         });
223                 imperial_units.setToolTipText("Use Imperial units instead of metric");
224                 pane.add(imperial_units, constraints(1, 2));
225                 row++;
226         }
227
228         public void add_font_size() {
229                 /* Font size setting */
230                 pane.add(new JLabel("Font size"), constraints(0, 1));
231
232                 font_size_value = new JComboBox(font_size_names);
233                 int font_size = AltosUIPreferences.font_size();
234                 font_size_value.setSelectedIndex(font_size - AltosUILib.font_size_small);
235                 font_size_value.addActionListener(new ActionListener() {
236                                 public void actionPerformed(ActionEvent e) {
237                                         int     size = font_size_value.getSelectedIndex() + AltosUILib.font_size_small;
238
239                                         AltosUIPreferences.set_font_size(size);
240                                 }
241                         });
242                 pane.add(font_size_value, constraints(1, 2, GridBagConstraints.BOTH));
243                 font_size_value.setToolTipText("Font size used in telemetry window");
244                 row++;
245         }
246
247         public void add_look_and_feel() {
248                 /* Look & Feel setting */
249                 pane.add(new JLabel("Look & feel"), constraints(0, 1));
250
251                 /*
252                 class LookAndFeelRenderer extends BasicComboBoxRenderer implements ListCellRenderer {
253
254                         public LookAndFeelRenderer() {
255                                 super();
256                         }
257
258                         public Component getListCellRendererComponent(
259                                 JList list,
260                                 Object value,
261                                 int index,
262                                 boolean isSelected,
263                                 boolean cellHasFocus)
264                         {
265                                 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
266                                 setText(((UIManager.LookAndFeelInfo) value).getName());
267                                 return this;
268                         }
269                 }
270                 */
271
272                 final UIManager.LookAndFeelInfo[] look_and_feels = UIManager.getInstalledLookAndFeels();
273
274                 look_and_feel_value = new JComboBox(look_and_feels);
275
276                 DelegatingRenderer.install(look_and_feel_value);
277
278                 String look_and_feel  = AltosUIPreferences.look_and_feel();
279                 for (int i = 0; i < look_and_feels.length; i++)
280                         if (look_and_feel.equals(look_and_feels[i].getClassName()))
281                                 look_and_feel_value.setSelectedIndex(i);
282
283                 look_and_feel_value.addActionListener(new ActionListener() {
284                                 public void actionPerformed(ActionEvent e) {
285                                         int     id = look_and_feel_value.getSelectedIndex();
286
287                                         AltosUIPreferences.set_look_and_feel(look_and_feels[id].getClassName());
288                                 }
289                         });
290                 pane.add(look_and_feel_value, constraints(1, 2, GridBagConstraints.BOTH));
291                 look_and_feel_value.setToolTipText("Look&feel used for new windows");
292                 row++;
293         }
294
295         public void add_serial_debug() {
296                 GridBagConstraints c = new GridBagConstraints();
297
298                 /* Serial debug setting */
299                 pane.add(new JLabel("Serial Debug"), constraints(0, 1));
300
301                 serial_debug = new JRadioButton("Enable", AltosUIPreferences.serial_debug());
302                 serial_debug.addActionListener(new ActionListener() {
303                                 public void actionPerformed(ActionEvent e) {
304                                         JRadioButton item = (JRadioButton) e.getSource();
305                                         boolean enabled = item.isSelected();
306                                         AltosUIPreferences.set_serial_debug(enabled);
307                                 }
308                         });
309                 serial_debug.setToolTipText("Enable/Disable USB I/O getting sent to the console");
310         }
311
312         public void add_bluetooth() {
313 //              GridBagConstraints c = new GridBagConstraints();
314 //              c.gridx = 1;
315 //              c.gridy = row++;
316 //              c.gridwidth = 3;
317 //              c.fill = GridBagConstraints.NONE;
318 //              c.anchor = GridBagConstraints.WEST;
319 //              pane.add(serial_debug, c);
320 //
321 //              manage_bluetooth = new JButton("Manage Bluetooth");
322 //              manage_bluetooth.addActionListener(new ActionListener() {
323 //                              public void actionPerformed(ActionEvent e) {
324 //                                      AltosBTManage.show(owner, AltosBTKnown.bt_known());
325 //                              }
326 //                      });
327 //              c.gridx = 0;
328 //              c.gridy = row;
329 //              c.gridwidth = 2;
330 //              c.fill = GridBagConstraints.NONE;
331 //              c.anchor = GridBagConstraints.WEST;
332 //              pane.add(manage_bluetooth, c);
333         }
334
335         public void add_frequencies() {
336 //              GridBagConstraints c = new GridBagConstraints();
337 //              manage_frequencies = new JButton("Manage Frequencies");
338 //              manage_frequencies.addActionListener(new ActionListener() {
339 //                              public void actionPerformed(ActionEvent e) {
340 //                                      AltosConfigFreqUI.show(owner);
341 //                              }
342 //                      });
343 //              manage_frequencies.setToolTipText("Configure which values are shown in frequency menus");
344 //              c.gridx = 2;
345 //              c.gridx = 2;
346 //              c.gridy = row++;
347 //              c.gridwidth = 2;
348 //              c.fill = GridBagConstraints.NONE;
349 //              c.anchor = GridBagConstraints.WEST;
350 //              pane.add(manage_frequencies, c);
351         }
352
353         public AltosConfigureUI(JFrame in_owner) {
354                 super(in_owner, "Configure AltosUI", false);
355
356                 owner = in_owner;
357                 pane = getContentPane();
358                 pane.setLayout(new GridBagLayout());
359
360                 row = 0;
361
362                 /* Nice label at the top */
363                 pane.add(new JLabel ("Configure AltOS UI"),
364                          constraints(0, 3));
365                 row++;
366
367                 pane.add(new JLabel (String.format("AltOS version %s", AltosUIVersion.version)),
368                          constraints(0, 3));
369                 row++;
370
371                 add_voice();
372                 add_log_dir();
373                 add_callsign();
374                 add_units();
375                 add_font_size();
376                 add_look_and_feel();
377                 add_bluetooth();
378                 add_frequencies();
379
380                 /* And a close button at the bottom */
381                 close = new JButton("Close");
382                 close.addActionListener(new ActionListener() {
383                                 public void actionPerformed(ActionEvent e) {
384                                         setVisible(false);
385                                 }
386                         });
387                 pane.add(close, constraints(0, 3));
388
389                 pack();
390                 setLocationRelativeTo(owner);
391                 setVisible(true);
392         }
393 }