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