ace245a0231d1e349d0082a971db8c6802b52a94
[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 import org.altusmetrum.AltosLib.*;
34
35 class DelegatingRenderer implements ListCellRenderer {
36
37         // ...
38         public static void install(JComboBox comboBox) {
39                 DelegatingRenderer renderer = new DelegatingRenderer(comboBox);
40                 renderer.initialise();
41                 comboBox.setRenderer(renderer);
42         }
43
44         // ...
45         private final JComboBox comboBox;
46
47         // ...
48         private ListCellRenderer delegate;
49
50         // ...
51         private DelegatingRenderer(JComboBox comboBox) {
52                 this.comboBox = comboBox;
53         }
54
55         // ...
56         private void initialise() {
57                 delegate = new JComboBox().getRenderer();
58                 comboBox.addPropertyChangeListener("UI", new PropertyChangeListener() {
59
60                                 public void propertyChange(PropertyChangeEvent evt) {
61                                         delegate = new JComboBox().getRenderer();
62                                 }
63                         });
64         }
65
66         // ...
67         public Component getListCellRendererComponent(JList list,
68                                                       Object value, int index, boolean isSelected, boolean cellHasFocus) {
69
70                 return delegate.getListCellRendererComponent(list,
71                                                              ((UIManager.LookAndFeelInfo) value).getName(),
72                                                              index, isSelected, cellHasFocus);
73         }
74 }
75
76 public class AltosConfigureUI
77         extends AltosDialog
78         implements DocumentListener
79 {
80         JFrame          owner;
81         AltosVoice      voice;
82         Container       pane;
83
84         JRadioButton    enable_voice;
85         JButton         test_voice;
86         JButton         close;
87
88         JButton         configure_log;
89         JTextField      log_directory;
90
91         JLabel          callsign_label;
92         JTextField      callsign_value;
93
94         JLabel          font_size_label;
95         JComboBox       font_size_value;
96
97         JLabel          look_and_feel_label;
98         JComboBox       look_and_feel_value;
99
100         JRadioButton    serial_debug;
101
102         JButton         manage_bluetooth;
103         JButton         manage_frequencies;
104
105         final static String[] font_size_names = { "Small", "Medium", "Large" };
106
107         /* DocumentListener interface methods */
108         public void changedUpdate(DocumentEvent e) {
109                 AltosUIPreferences.set_callsign(callsign_value.getText());
110         }
111
112         public void insertUpdate(DocumentEvent e) {
113                 changedUpdate(e);
114         }
115
116         public void removeUpdate(DocumentEvent e) {
117                 changedUpdate(e);
118         }
119
120         public AltosConfigureUI(JFrame in_owner, AltosVoice in_voice) {
121                 super(in_owner, "Configure AltosUI", false);
122
123                 GridBagConstraints      c;
124
125                 Insets insets = new Insets(4, 4, 4, 4);
126
127                 int row = 0;
128
129                 owner = in_owner;
130                 voice = in_voice;
131                 pane = getContentPane();
132                 pane.setLayout(new GridBagLayout());
133
134                 c = new GridBagConstraints();
135                 c.insets = insets;
136                 c.fill = GridBagConstraints.NONE;
137                 c.anchor = GridBagConstraints.WEST;
138
139                 /* Nice label at the top */
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 ("Configure AltOS UI"), c);
146
147                 c.gridx = 0;
148                 c.gridy = row++;
149                 c.gridwidth = 3;
150                 c.fill = GridBagConstraints.NONE;
151                 c.anchor = GridBagConstraints.CENTER;
152                 pane.add(new JLabel (String.format("AltOS version %s", AltosVersion.version)), c);
153
154                 /* Voice settings */
155                 c.gridx = 0;
156                 c.gridy = row;
157                 c.gridwidth = 1;
158                 c.fill = GridBagConstraints.NONE;
159                 c.anchor = GridBagConstraints.WEST;
160                 pane.add(new JLabel("Voice"), c);
161
162                 enable_voice = new JRadioButton("Enable", AltosUIPreferences.voice());
163                 enable_voice.addActionListener(new ActionListener() {
164                                 public void actionPerformed(ActionEvent e) {
165                                         JRadioButton item = (JRadioButton) e.getSource();
166                                         boolean enabled = item.isSelected();
167                                         AltosUIPreferences.set_voice(enabled);
168                                         if (enabled)
169                                                 voice.speak_always("Enable voice.");
170                                         else
171                                                 voice.speak_always("Disable voice.");
172                                 }
173                         });
174                 c.gridx = 1;
175                 c.gridy = row;
176                 c.gridwidth = 1;
177                 c.weightx = 1;
178                 c.fill = GridBagConstraints.NONE;
179                 c.anchor = GridBagConstraints.WEST;
180                 pane.add(enable_voice, c);
181                 enable_voice.setToolTipText("Enable/Disable all audio in-flight announcements");
182
183                 c.gridx = 2;
184                 c.gridy = row++;
185                 c.gridwidth = 1;
186                 c.weightx = 1;
187                 c.fill = GridBagConstraints.NONE;
188                 c.anchor = GridBagConstraints.EAST;
189                 test_voice = new JButton("Test Voice");
190                 test_voice.addActionListener(new ActionListener() {
191                                 public void actionPerformed(ActionEvent e) {
192                                         voice.speak("That's one small step for man; one giant leap for mankind.");
193                                 }
194                         });
195                 pane.add(test_voice, c);
196                 test_voice.setToolTipText("Play a stock audio clip to check volume");
197
198                 /* Log directory settings */
199                 c.gridx = 0;
200                 c.gridy = row;
201                 c.gridwidth = 1;
202                 c.fill = GridBagConstraints.NONE;
203                 c.anchor = GridBagConstraints.WEST;
204                 pane.add(new JLabel("Log Directory"), c);
205
206                 configure_log = new JButton(AltosUIPreferences.logdir().getPath());
207                 configure_log.addActionListener(new ActionListener() {
208                                 public void actionPerformed(ActionEvent e) {
209                                         AltosUIPreferences.ConfigureLog();
210                                         configure_log.setText(AltosUIPreferences.logdir().getPath());
211                                 }
212                         });
213                 c.gridx = 1;
214                 c.gridy = row++;
215                 c.gridwidth = 2;
216                 c.fill = GridBagConstraints.BOTH;
217                 c.anchor = GridBagConstraints.WEST;
218                 pane.add(configure_log, c);
219                 configure_log.setToolTipText("Which directory flight logs are stored in");
220
221                 /* Callsign setting */
222                 c.gridx = 0;
223                 c.gridy = row;
224                 c.gridwidth = 1;
225                 c.fill = GridBagConstraints.NONE;
226                 c.anchor = GridBagConstraints.WEST;
227                 pane.add(new JLabel("Callsign"), c);
228
229                 callsign_value = new JTextField(AltosUIPreferences.callsign());
230                 callsign_value.getDocument().addDocumentListener(this);
231                 c.gridx = 1;
232                 c.gridy = row++;
233                 c.gridwidth = 2;
234                 c.fill = GridBagConstraints.BOTH;
235                 c.anchor = GridBagConstraints.WEST;
236                 pane.add(callsign_value, c);
237                 callsign_value.setToolTipText("Callsign sent in packet mode");
238
239                 /* Font size setting */
240                 c.gridx = 0;
241                 c.gridy = row;
242                 c.gridwidth = 1;
243                 c.fill = GridBagConstraints.NONE;
244                 c.anchor = GridBagConstraints.WEST;
245                 pane.add(new JLabel("Font size"), c);
246
247                 font_size_value = new JComboBox(font_size_names);
248                 int font_size = AltosUIPreferences.font_size();
249                 font_size_value.setSelectedIndex(font_size - Altos.font_size_small);
250                 font_size_value.addActionListener(new ActionListener() {
251                                 public void actionPerformed(ActionEvent e) {
252                                         int     size = font_size_value.getSelectedIndex() + Altos.font_size_small;
253
254                                         AltosUIPreferences.set_font_size(size);
255                                 }
256                         });
257                 c.gridx = 1;
258                 c.gridy = row++;
259                 c.gridwidth = 2;
260                 c.fill = GridBagConstraints.BOTH;
261                 c.anchor = GridBagConstraints.WEST;
262                 pane.add(font_size_value, c);
263                 font_size_value.setToolTipText("Font size used in telemetry window");
264
265                 /* Look & Feel setting */
266                 c.gridx = 0;
267                 c.gridy = row;
268                 c.gridwidth = 1;
269                 c.fill = GridBagConstraints.NONE;
270                 c.anchor = GridBagConstraints.WEST;
271                 pane.add(new JLabel("Look & feel"), c);
272
273                 class LookAndFeelRenderer extends BasicComboBoxRenderer implements ListCellRenderer {
274
275                         public LookAndFeelRenderer() {
276                                 super();
277                         }
278
279                         public Component getListCellRendererComponent(
280                                 JList list,
281                                 Object value,
282                                 int index,
283                                 boolean isSelected,
284                                 boolean cellHasFocus)
285                         {
286                                 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
287                                 setText(((UIManager.LookAndFeelInfo) value).getName());
288                                 return this;
289                         }
290                 }
291
292                 final UIManager.LookAndFeelInfo[] look_and_feels = UIManager.getInstalledLookAndFeels();
293
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, AltosBTKnown.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 }