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