9a292c915b760fd51fda9b2a392665492b0363ea
[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                 c.gridx = 0;
91                 c.gridy = 1;
92                 c.gridwidth = 3;
93                 c.fill = GridBagConstraints.NONE;
94                 c.anchor = GridBagConstraints.CENTER;
95                 pane.add(new JLabel (String.format("AltOS version %s", AltosVersion.version)), c);
96
97                 /* Voice settings */
98                 c.gridx = 0;
99                 c.gridy = 2;
100                 c.gridwidth = 1;
101                 c.fill = GridBagConstraints.NONE;
102                 c.anchor = GridBagConstraints.WEST;
103                 pane.add(new JLabel("Voice"), c);
104
105                 enable_voice = new JRadioButton("Enable", AltosPreferences.voice());
106                 enable_voice.addActionListener(new ActionListener() {
107                                 public void actionPerformed(ActionEvent e) {
108                                         JRadioButton item = (JRadioButton) e.getSource();
109                                         boolean enabled = item.isSelected();
110                                         AltosPreferences.set_voice(enabled);
111                                         if (enabled)
112                                                 voice.speak_always("Enable voice.");
113                                         else
114                                                 voice.speak_always("Disable voice.");
115                                 }
116                         });
117                 c.gridx = 1;
118                 c.gridy = 2;
119                 c.gridwidth = 1;
120                 c.weightx = 1;
121                 c.fill = GridBagConstraints.NONE;
122                 c.anchor = GridBagConstraints.WEST;
123                 pane.add(enable_voice, c);
124
125                 c.gridx = 2;
126                 c.gridy = 2;
127                 c.gridwidth = 1;
128                 c.weightx = 1;
129                 c.fill = GridBagConstraints.NONE;
130                 c.anchor = GridBagConstraints.EAST;
131                 test_voice = new JButton("Test Voice");
132                 test_voice.addActionListener(new ActionListener() {
133                                 public void actionPerformed(ActionEvent e) {
134                                         voice.speak("That's one small step for man; one giant leap for mankind.");
135                                 }
136                         });
137                 pane.add(test_voice, c);
138
139                 /* Log directory settings */
140                 c.gridx = 0;
141                 c.gridy = 3;
142                 c.gridwidth = 1;
143                 c.fill = GridBagConstraints.NONE;
144                 c.anchor = GridBagConstraints.WEST;
145                 pane.add(new JLabel("Log Directory"), c);
146
147                 configure_log = new JButton(AltosPreferences.logdir().getPath());
148                 configure_log.addActionListener(new ActionListener() {
149                                 public void actionPerformed(ActionEvent e) {
150                                         AltosPreferences.ConfigureLog();
151                                         configure_log.setText(AltosPreferences.logdir().getPath());
152                                 }
153                         });
154                 c.gridx = 1;
155                 c.gridy = 3;
156                 c.gridwidth = 2;
157                 c.fill = GridBagConstraints.BOTH;
158                 c.anchor = GridBagConstraints.WEST;
159                 pane.add(configure_log, c);
160
161                 /* Callsign setting */
162                 c.gridx = 0;
163                 c.gridy = 4;
164                 c.gridwidth = 1;
165                 c.fill = GridBagConstraints.NONE;
166                 c.anchor = GridBagConstraints.WEST;
167                 pane.add(new JLabel("Callsign"), c);
168
169                 callsign_value = new JTextField(AltosPreferences.callsign());
170                 callsign_value.getDocument().addDocumentListener(this);
171                 c.gridx = 1;
172                 c.gridy = 4;
173                 c.gridwidth = 2;
174                 c.fill = GridBagConstraints.BOTH;
175                 c.anchor = GridBagConstraints.WEST;
176                 pane.add(callsign_value, c);
177
178                 /* Serial debug setting */
179                 c.gridx = 0;
180                 c.gridy = 5;
181                 c.gridwidth = 1;
182                 c.fill = GridBagConstraints.NONE;
183                 c.anchor = GridBagConstraints.WEST;
184                 pane.add(new JLabel("Serial Debug"), c);
185
186                 serial_debug = new JRadioButton("Enable", AltosPreferences.serial_debug());
187                 serial_debug.addActionListener(new ActionListener() {
188                                 public void actionPerformed(ActionEvent e) {
189                                         JRadioButton item = (JRadioButton) e.getSource();
190                                         boolean enabled = item.isSelected();
191                                         AltosPreferences.set_serial_debug(enabled);
192                                 }
193                         });
194
195                 c.gridx = 1;
196                 c.gridy = 5;
197                 c.gridwidth = 3;
198                 c.fill = GridBagConstraints.NONE;
199                 c.anchor = GridBagConstraints.WEST;
200                 pane.add(serial_debug, c);
201
202                 /* And a close button at the bottom */
203                 close = new JButton("Close");
204                 close.addActionListener(new ActionListener() {
205                                 public void actionPerformed(ActionEvent e) {
206                                         setVisible(false);
207                                 }
208                         });
209                 c.gridx = 0;
210                 c.gridy = 6;
211                 c.gridwidth = 3;
212                 c.fill = GridBagConstraints.NONE;
213                 c.anchor = GridBagConstraints.CENTER;
214                 pane.add(close, c);
215
216                 pack();
217                 setLocationRelativeTo(owner);
218                 setVisible(true);
219         }
220 }