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.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import javax.swing.event.*;
29 import java.util.prefs.*;
30 import java.util.concurrent.LinkedBlockingQueue;
34 public class AltosConfigUI
36 implements ActionListener, ItemListener, DocumentListener
44 JLabel main_deploy_label;
45 JLabel apogee_delay_label;
46 JLabel radio_channel_label;
47 JLabel radio_calibration_label;
48 JLabel callsign_label;
56 JComboBox main_deploy_value;
57 JComboBox apogee_delay_value;
58 JComboBox radio_channel_value;
59 JTextField radio_calibration_value;
60 JTextField callsign_value;
67 ActionListener listener;
69 static String[] main_deploy_values = {
70 "100", "150", "200", "250", "300", "350",
74 static String[] apogee_delay_values = {
75 "0", "1", "2", "3", "4", "5"
78 static String[] radio_channel_values = new String[10];
80 for (int i = 0; i <= 9; i++)
81 radio_channel_values[i] = String.format("Channel %1d (%7.3fMHz)",
82 i, 434.550 + i * 0.1);
85 /* A window listener to catch closing events and tell the config code */
86 class ConfigListener extends WindowAdapter {
89 public ConfigListener(AltosConfigUI this_ui) {
93 public void windowClosing(WindowEvent e) {
94 ui.actionPerformed(new ActionEvent(e.getSource(),
95 ActionEvent.ACTION_PERFORMED,
100 /* Build the UI using a grid bag */
101 public AltosConfigUI(JFrame in_owner) {
102 super (in_owner, "Configure TeleMetrum", false);
105 GridBagConstraints c;
107 Insets il = new Insets(4,4,4,4);
108 Insets ir = new Insets(4,4,4,4);
110 pane = getContentPane();
111 pane.setLayout(new GridBagLayout());
114 c = new GridBagConstraints();
115 c.gridx = 0; c.gridy = 0;
117 c.fill = GridBagConstraints.NONE;
118 c.anchor = GridBagConstraints.LINE_START;
120 product_label = new JLabel("Product:");
121 pane.add(product_label, c);
123 c = new GridBagConstraints();
124 c.gridx = 4; c.gridy = 0;
126 c.fill = GridBagConstraints.HORIZONTAL;
128 c.anchor = GridBagConstraints.LINE_START;
130 product_value = new JLabel("");
131 pane.add(product_value, c);
134 c = new GridBagConstraints();
135 c.gridx = 0; c.gridy = 1;
137 c.fill = GridBagConstraints.NONE;
138 c.anchor = GridBagConstraints.LINE_START;
141 version_label = new JLabel("Software version:");
142 pane.add(version_label, c);
144 c = new GridBagConstraints();
145 c.gridx = 4; c.gridy = 1;
147 c.fill = GridBagConstraints.HORIZONTAL;
149 c.anchor = GridBagConstraints.LINE_START;
152 version_value = new JLabel("");
153 pane.add(version_value, c);
156 c = new GridBagConstraints();
157 c.gridx = 0; c.gridy = 2;
159 c.fill = GridBagConstraints.NONE;
160 c.anchor = GridBagConstraints.LINE_START;
163 serial_label = new JLabel("Serial:");
164 pane.add(serial_label, c);
166 c = new GridBagConstraints();
167 c.gridx = 4; c.gridy = 2;
169 c.fill = GridBagConstraints.HORIZONTAL;
171 c.anchor = GridBagConstraints.LINE_START;
174 serial_value = new JLabel("");
175 pane.add(serial_value, c);
178 c = new GridBagConstraints();
179 c.gridx = 0; c.gridy = 3;
181 c.fill = GridBagConstraints.NONE;
182 c.anchor = GridBagConstraints.LINE_START;
185 main_deploy_label = new JLabel("Main Deploy Altitude(m):");
186 pane.add(main_deploy_label, c);
188 c = new GridBagConstraints();
189 c.gridx = 4; c.gridy = 3;
191 c.fill = GridBagConstraints.HORIZONTAL;
193 c.anchor = GridBagConstraints.LINE_START;
196 main_deploy_value = new JComboBox(main_deploy_values);
197 main_deploy_value.setEditable(true);
198 main_deploy_value.addItemListener(this);
199 pane.add(main_deploy_value, c);
202 c = new GridBagConstraints();
203 c.gridx = 0; c.gridy = 4;
205 c.fill = GridBagConstraints.NONE;
206 c.anchor = GridBagConstraints.LINE_START;
209 apogee_delay_label = new JLabel("Apogee Delay(s):");
210 pane.add(apogee_delay_label, c);
212 c = new GridBagConstraints();
213 c.gridx = 4; c.gridy = 4;
215 c.fill = GridBagConstraints.HORIZONTAL;
217 c.anchor = GridBagConstraints.LINE_START;
220 apogee_delay_value = new JComboBox(apogee_delay_values);
221 apogee_delay_value.setEditable(true);
222 apogee_delay_value.addItemListener(this);
223 pane.add(apogee_delay_value, c);
226 c = new GridBagConstraints();
227 c.gridx = 0; c.gridy = 5;
229 c.fill = GridBagConstraints.NONE;
230 c.anchor = GridBagConstraints.LINE_START;
233 radio_channel_label = new JLabel("Radio Channel:");
234 pane.add(radio_channel_label, c);
236 c = new GridBagConstraints();
237 c.gridx = 4; c.gridy = 5;
239 c.fill = GridBagConstraints.HORIZONTAL;
241 c.anchor = GridBagConstraints.LINE_START;
244 radio_channel_value = new JComboBox(radio_channel_values);
245 radio_channel_value.setEditable(false);
246 radio_channel_value.addItemListener(this);
247 pane.add(radio_channel_value, c);
249 /* Radio Calibration */
250 c = new GridBagConstraints();
251 c.gridx = 0; c.gridy = 6;
253 c.fill = GridBagConstraints.NONE;
254 c.anchor = GridBagConstraints.LINE_START;
257 radio_calibration_label = new JLabel("RF Calibration:");
258 pane.add(radio_calibration_label, c);
260 c = new GridBagConstraints();
261 c.gridx = 4; c.gridy = 6;
263 c.fill = GridBagConstraints.HORIZONTAL;
265 c.anchor = GridBagConstraints.LINE_START;
268 radio_calibration_value = new JTextField(String.format("%d", 1186611));
269 radio_calibration_value.getDocument().addDocumentListener(this);
270 pane.add(radio_calibration_value, c);
273 c = new GridBagConstraints();
274 c.gridx = 0; c.gridy = 7;
276 c.fill = GridBagConstraints.NONE;
277 c.anchor = GridBagConstraints.LINE_START;
280 callsign_label = new JLabel("Callsign:");
281 pane.add(callsign_label, c);
283 c = new GridBagConstraints();
284 c.gridx = 4; c.gridy = 7;
286 c.fill = GridBagConstraints.HORIZONTAL;
288 c.anchor = GridBagConstraints.LINE_START;
291 callsign_value = new JTextField(AltosPreferences.callsign());
292 callsign_value.getDocument().addDocumentListener(this);
293 pane.add(callsign_value, c);
296 c = new GridBagConstraints();
297 c.gridx = 0; c.gridy = 8;
299 c.fill = GridBagConstraints.NONE;
300 c.anchor = GridBagConstraints.LINE_START;
302 save = new JButton("Save");
304 save.addActionListener(this);
305 save.setActionCommand("Save");
307 c = new GridBagConstraints();
308 c.gridx = 2; c.gridy = 8;
310 c.fill = GridBagConstraints.NONE;
311 c.anchor = GridBagConstraints.CENTER;
313 reset = new JButton("Reset");
315 reset.addActionListener(this);
316 reset.setActionCommand("Reset");
318 c = new GridBagConstraints();
319 c.gridx = 4; c.gridy = 8;
321 c.fill = GridBagConstraints.NONE;
322 c.anchor = GridBagConstraints.CENTER;
324 reboot = new JButton("Reboot");
326 reboot.addActionListener(this);
327 reboot.setActionCommand("Reboot");
329 c = new GridBagConstraints();
330 c.gridx = 6; c.gridy = 8;
332 c.fill = GridBagConstraints.NONE;
333 c.anchor = GridBagConstraints.LINE_END;
335 close = new JButton("Close");
337 close.addActionListener(this);
338 close.setActionCommand("Close");
340 addWindowListener(new ConfigListener(this));
343 /* Once the initial values are set, the config code will show the dialog */
344 public void make_visible() {
346 setLocationRelativeTo(owner);
350 /* If any values have been changed, confirm before closing */
351 public boolean check_dirty(String operation) {
353 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
355 i = JOptionPane.showOptionDialog(this,
356 String.format("Configuration modified. %s anyway?", operation),
357 "Configuration Modified",
358 JOptionPane.DEFAULT_OPTION,
359 JOptionPane.WARNING_MESSAGE,
360 null, options, options[1]);
367 /* Listen for events from our buttons */
368 public void actionPerformed(ActionEvent e) {
369 String cmd = e.getActionCommand();
371 if (cmd.equals("Close") || cmd.equals("Reboot"))
372 if (!check_dirty(cmd))
374 listener.actionPerformed(e);
375 if (cmd.equals("Close") || cmd.equals("Reboot")) {
382 /* ItemListener interface method */
383 public void itemStateChanged(ItemEvent e) {
387 /* DocumentListener interface methods */
388 public void changedUpdate(DocumentEvent e) {
392 public void insertUpdate(DocumentEvent e) {
396 public void removeUpdate(DocumentEvent e) {
400 /* Let the config code hook on a listener */
401 public void addActionListener(ActionListener l) {
405 /* set and get all of the dialog values */
406 public void set_product(String product) {
407 product_value.setText(product);
410 public void set_version(String version) {
411 version_value.setText(version);
414 public void set_serial(int serial) {
415 serial_value.setText(String.format("%d", serial));
418 public void set_main_deploy(int new_main_deploy) {
419 main_deploy_value.setSelectedItem(Integer.toString(new_main_deploy));
422 public int main_deploy() {
423 return Integer.parseInt(main_deploy_value.getSelectedItem().toString());
426 public void set_apogee_delay(int new_apogee_delay) {
427 apogee_delay_value.setSelectedItem(Integer.toString(new_apogee_delay));
430 public int apogee_delay() {
431 return Integer.parseInt(apogee_delay_value.getSelectedItem().toString());
434 public void set_radio_channel(int new_radio_channel) {
435 radio_channel_value.setSelectedIndex(new_radio_channel);
438 public int radio_channel() {
439 return radio_channel_value.getSelectedIndex();
442 public void set_radio_calibration(int new_radio_calibration) {
443 radio_calibration_value.setText(String.format("%d", new_radio_calibration));
446 public int radio_calibration() {
447 return Integer.parseInt(radio_calibration_value.getText());
450 public void set_callsign(String new_callsign) {
451 callsign_value.setText(new_callsign);
454 public String callsign() {
455 return callsign_value.getText();
458 public void set_clean() {