2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 import java.awt.event.*;
24 import javax.swing.event.*;
25 import javax.swing.plaf.basic.*;
27 class DelegatingRenderer implements ListCellRenderer {
30 public static void install(JComboBox comboBox) {
31 DelegatingRenderer renderer = new DelegatingRenderer(comboBox);
32 renderer.initialise();
33 comboBox.setRenderer(renderer);
37 private final JComboBox comboBox;
40 private ListCellRenderer delegate;
43 private DelegatingRenderer(JComboBox comboBox) {
44 this.comboBox = comboBox;
48 private void initialise() {
49 delegate = new JComboBox().getRenderer();
50 comboBox.addPropertyChangeListener("UI", new PropertyChangeListener() {
52 public void propertyChange(PropertyChangeEvent evt) {
53 delegate = new JComboBox().getRenderer();
59 public Component getListCellRendererComponent(JList list,
60 Object value, int index, boolean isSelected, boolean cellHasFocus) {
62 return delegate.getListCellRendererComponent(list,
63 ((UIManager.LookAndFeelInfo) value).getName(),
64 index, isSelected, cellHasFocus);
68 public class AltosConfigureUI
70 implements DocumentListener
76 JRadioButton enable_voice;
80 JButton configure_log;
81 JTextField log_directory;
83 JLabel callsign_label;
84 JTextField callsign_value;
86 JRadioButton imperial_units;
88 JLabel font_size_label;
89 JComboBox font_size_value;
91 JLabel look_and_feel_label;
92 JComboBox look_and_feel_value;
94 JRadioButton serial_debug;
96 JButton manage_bluetooth;
97 JButton manage_frequencies;
99 final static String[] font_size_names = { "Small", "Medium", "Large" };
101 /* DocumentListener interface methods */
102 public void changedUpdate(DocumentEvent e) {
103 AltosUIPreferences.set_callsign(callsign_value.getText());
106 public void insertUpdate(DocumentEvent e) {
110 public void removeUpdate(DocumentEvent e) {
114 public AltosConfigureUI(JFrame in_owner, AltosVoice in_voice) {
115 super(in_owner, "Configure AltosUI", false);
117 GridBagConstraints c;
119 Insets insets = new Insets(4, 4, 4, 4);
125 pane = getContentPane();
126 pane.setLayout(new GridBagLayout());
128 c = new GridBagConstraints();
130 c.fill = GridBagConstraints.NONE;
131 c.anchor = GridBagConstraints.WEST;
133 /* Nice label at the top */
137 c.fill = GridBagConstraints.NONE;
138 c.anchor = GridBagConstraints.CENTER;
139 pane.add(new JLabel ("Configure AltOS UI"), c);
144 c.fill = GridBagConstraints.NONE;
145 c.anchor = GridBagConstraints.CENTER;
146 pane.add(new JLabel (String.format("AltOS version %s", AltosVersion.version)), c);
152 c.fill = GridBagConstraints.NONE;
153 c.anchor = GridBagConstraints.WEST;
154 pane.add(new JLabel("Voice"), c);
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);
163 voice.speak_always("Enable voice.");
165 voice.speak_always("Disable voice.");
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");
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.");
189 pane.add(test_voice, c);
190 test_voice.setToolTipText("Play a stock audio clip to check volume");
192 /* Log directory settings */
196 c.fill = GridBagConstraints.NONE;
197 c.anchor = GridBagConstraints.WEST;
198 pane.add(new JLabel("Log Directory"), c);
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());
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");
215 /* Callsign setting */
219 c.fill = GridBagConstraints.NONE;
220 c.anchor = GridBagConstraints.WEST;
221 pane.add(new JLabel("Callsign"), c);
223 callsign_value = new JTextField(AltosUIPreferences.callsign());
224 callsign_value.getDocument().addDocumentListener(this);
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");
233 /* Imperial units setting */
237 c.fill = GridBagConstraints.NONE;
238 c.anchor = GridBagConstraints.WEST;
239 pane.add(new JLabel("Imperial Units"), c);
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);
249 imperial_units.setToolTipText("Use Imperial units instead of metric");
254 c.fill = GridBagConstraints.NONE;
255 c.anchor = GridBagConstraints.WEST;
256 pane.add(imperial_units, c);
258 /* Font size setting */
262 c.fill = GridBagConstraints.NONE;
263 c.anchor = GridBagConstraints.WEST;
264 pane.add(new JLabel("Font size"), c);
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;
273 AltosUIPreferences.set_font_size(size);
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");
284 /* Look & Feel setting */
288 c.fill = GridBagConstraints.NONE;
289 c.anchor = GridBagConstraints.WEST;
290 pane.add(new JLabel("Look & feel"), c);
292 class LookAndFeelRenderer extends BasicComboBoxRenderer implements ListCellRenderer {
294 public LookAndFeelRenderer() {
298 public Component getListCellRendererComponent(
303 boolean cellHasFocus)
305 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
306 setText(((UIManager.LookAndFeelInfo) value).getName());
311 final UIManager.LookAndFeelInfo[] look_and_feels = UIManager.getInstalledLookAndFeels();
313 look_and_feel_value = new JComboBox(look_and_feels);
315 DelegatingRenderer.install(look_and_feel_value);
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);
322 look_and_feel_value.addActionListener(new ActionListener() {
323 public void actionPerformed(ActionEvent e) {
324 int id = look_and_feel_value.getSelectedIndex();
326 AltosUIPreferences.set_look_and_feel(look_and_feels[id].getClassName());
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");
337 /* Serial debug setting */
341 c.fill = GridBagConstraints.NONE;
342 c.anchor = GridBagConstraints.WEST;
343 pane.add(new JLabel("Serial Debug"), c);
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);
353 serial_debug.setToolTipText("Enable/Disable USB I/O getting sent to the console");
358 c.fill = GridBagConstraints.NONE;
359 c.anchor = GridBagConstraints.WEST;
360 pane.add(serial_debug, c);
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());
371 c.fill = GridBagConstraints.NONE;
372 c.anchor = GridBagConstraints.WEST;
373 pane.add(manage_bluetooth, c);
375 manage_frequencies = new JButton("Manage Frequencies");
376 manage_frequencies.addActionListener(new ActionListener() {
377 public void actionPerformed(ActionEvent e) {
378 AltosConfigFreqUI.show(owner);
381 manage_frequencies.setToolTipText("Configure which values are shown in frequency menus");
386 c.fill = GridBagConstraints.NONE;
387 c.anchor = GridBagConstraints.WEST;
388 pane.add(manage_frequencies, c);
390 /* And a close button at the bottom */
391 close = new JButton("Close");
392 close.addActionListener(new ActionListener() {
393 public void actionPerformed(ActionEvent e) {
400 c.fill = GridBagConstraints.NONE;
401 c.anchor = GridBagConstraints.CENTER;
405 setLocationRelativeTo(owner);