altosui: Add preference for serial debugging.
[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 javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import javax.swing.event.*;
26 import java.io.*;
27 import java.util.*;
28 import java.text.*;
29 import java.util.prefs.*;
30 import java.util.concurrent.LinkedBlockingQueue;
31
32 public class AltosConfigureUI
33         extends JDialog
34         implements DocumentListener
35 {
36         JFrame          owner;
37         AltosVoice      voice;
38         Container       pane;
39
40         JRadioButton    enable_voice;
41         JButton         test_voice;
42         JButton         close;
43
44         JButton         configure_log;
45         JTextField      log_directory;
46
47         JLabel          callsign_label;
48         JTextField      callsign_value;
49
50         JRadioButton    serial_debug;
51
52         /* DocumentListener interface methods */
53         public void changedUpdate(DocumentEvent e) {
54                 AltosPreferences.set_callsign(callsign_value.getText());
55         }
56
57         public void insertUpdate(DocumentEvent e) {
58                 changedUpdate(e);
59         }
60
61         public void removeUpdate(DocumentEvent e) {
62                 changedUpdate(e);
63         }
64
65         public AltosConfigureUI(JFrame in_owner, AltosVoice in_voice) {
66                 super(in_owner, "Configure AltosUI", false);
67
68                 GridBagConstraints      c;
69
70                 Insets insets = new Insets(4, 4, 4, 4);
71
72                 owner = in_owner;
73                 voice = in_voice;
74                 pane = getContentPane();
75                 pane.setLayout(new GridBagLayout());
76
77                 c = new GridBagConstraints();
78                 c.insets = insets;
79                 c.fill = GridBagConstraints.NONE;
80                 c.anchor = GridBagConstraints.WEST;
81
82                 /* Nice label at the top */
83                 c.gridx = 0;
84                 c.gridy = 0;
85                 c.gridwidth = 3;
86                 c.fill = GridBagConstraints.NONE;
87                 c.anchor = GridBagConstraints.CENTER;
88                 pane.add(new JLabel ("Configure AltOS UI"), c);
89
90                 /* Voice settings */
91                 c.gridx = 0;
92                 c.gridy = 1;
93                 c.gridwidth = 1;
94                 c.fill = GridBagConstraints.NONE;
95                 c.anchor = GridBagConstraints.WEST;
96                 pane.add(new JLabel("Voice"), c);
97
98                 enable_voice = new JRadioButton("Enable", AltosPreferences.voice());
99                 enable_voice.addActionListener(new ActionListener() {
100                                 public void actionPerformed(ActionEvent e) {
101                                         JRadioButton item = (JRadioButton) e.getSource();
102                                         boolean enabled = item.isSelected();
103                                         AltosPreferences.set_voice(enabled);
104                                         if (enabled)
105                                                 voice.speak_always("Enable voice.");
106                                         else
107                                                 voice.speak_always("Disable voice.");
108                                 }
109                         });
110                 c.gridx = 1;
111                 c.gridy = 1;
112                 c.gridwidth = 1;
113                 c.weightx = 1;
114                 c.fill = GridBagConstraints.NONE;
115                 c.anchor = GridBagConstraints.WEST;
116                 pane.add(enable_voice, c);
117
118                 c.gridx = 2;
119                 c.gridy = 1;
120                 c.gridwidth = 1;
121                 c.weightx = 1;
122                 c.fill = GridBagConstraints.NONE;
123                 c.anchor = GridBagConstraints.EAST;
124                 test_voice = new JButton("Test Voice");
125                 test_voice.addActionListener(new ActionListener() {
126                                 public void actionPerformed(ActionEvent e) {
127                                         voice.speak("That's one small step for man; one giant leap for mankind.");
128                                 }
129                         });
130                 pane.add(test_voice, c);
131
132                 /* Log directory settings */
133                 c.gridx = 0;
134                 c.gridy = 2;
135                 c.gridwidth = 1;
136                 c.fill = GridBagConstraints.NONE;
137                 c.anchor = GridBagConstraints.WEST;
138                 pane.add(new JLabel("Log Directory"), c);
139
140                 configure_log = new JButton(AltosPreferences.logdir().getPath());
141                 configure_log.addActionListener(new ActionListener() {
142                                 public void actionPerformed(ActionEvent e) {
143                                         AltosPreferences.ConfigureLog();
144                                         configure_log.setText(AltosPreferences.logdir().getPath());
145                                 }
146                         });
147                 c.gridx = 1;
148                 c.gridy = 2;
149                 c.gridwidth = 2;
150                 c.fill = GridBagConstraints.BOTH;
151                 c.anchor = GridBagConstraints.WEST;
152                 pane.add(configure_log, c);
153
154                 /* Callsign setting */
155                 c.gridx = 0;
156                 c.gridy = 3;
157                 c.gridwidth = 1;
158                 c.fill = GridBagConstraints.NONE;
159                 c.anchor = GridBagConstraints.WEST;
160                 pane.add(new JLabel("Callsign"), c);
161
162                 callsign_value = new JTextField(AltosPreferences.callsign());
163                 callsign_value.getDocument().addDocumentListener(this);
164                 c.gridx = 1;
165                 c.gridy = 3;
166                 c.gridwidth = 2;
167                 c.fill = GridBagConstraints.BOTH;
168                 c.anchor = GridBagConstraints.WEST;
169                 pane.add(callsign_value, c);
170
171                 /* Serial debug setting */
172                 c.gridx = 0;
173                 c.gridy = 4;
174                 c.gridwidth = 1;
175                 c.fill = GridBagConstraints.NONE;
176                 c.anchor = GridBagConstraints.WEST;
177                 pane.add(new JLabel("Serial Debug"), c);
178
179                 serial_debug = new JRadioButton("Enable", AltosPreferences.serial_debug());
180                 serial_debug.addActionListener(new ActionListener() {
181                                 public void actionPerformed(ActionEvent e) {
182                                         JRadioButton item = (JRadioButton) e.getSource();
183                                         boolean enabled = item.isSelected();
184                                         AltosPreferences.set_serial_debug(enabled);
185                                 }
186                         });
187
188                 c.gridx = 1;
189                 c.gridy = 4;
190                 c.gridwidth = 3;
191                 c.fill = GridBagConstraints.NONE;
192                 c.anchor = GridBagConstraints.WEST;
193                 pane.add(serial_debug, c);
194
195                 /* And a close button at the bottom */
196                 close = new JButton("Close");
197                 close.addActionListener(new ActionListener() {
198                                 public void actionPerformed(ActionEvent e) {
199                                         setVisible(false);
200                                 }
201                         });
202                 c.gridx = 0;
203                 c.gridy = 5;
204                 c.gridwidth = 3;
205                 c.fill = GridBagConstraints.NONE;
206                 c.anchor = GridBagConstraints.CENTER;
207                 pane.add(close, c);
208
209                 pack();
210                 setLocationRelativeTo(owner);
211                 setVisible(true);
212         }
213 }