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