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.
18 package org.altusmetrum.telegps;
21 import java.awt.event.*;
23 import javax.swing.event.*;
24 import org.altusmetrum.altoslib_4.*;
25 import org.altusmetrum.altosuilib_2.*;
27 public class TeleGPSConfigUI
29 implements ActionListener, ItemListener, DocumentListener, AltosConfigValues
36 JLabel frequency_label;
37 JLabel radio_calibration_label;
38 JLabel radio_frequency_label;
39 JLabel radio_enable_label;
40 JLabel aprs_interval_label;
41 JLabel flight_log_max_label;
42 JLabel callsign_label;
50 AltosFreqList radio_frequency_value;
51 JTextField radio_calibration_value;
52 JRadioButton radio_enable_value;
53 JComboBox<String> aprs_interval_value;
54 JComboBox<String> flight_log_max_value;
55 JTextField callsign_value;
62 ActionListener listener;
64 static String[] flight_log_max_values = {
65 "64", "128", "192", "256", "320",
66 "384", "448", "512", "576", "640",
67 "704", "768", "832", "896", "960",
70 static String[] aprs_interval_values = {
77 /* A window listener to catch closing events and tell the config code */
78 class ConfigListener extends WindowAdapter {
81 public ConfigListener(TeleGPSConfigUI this_ui) {
85 public void windowClosing(WindowEvent e) {
86 ui.actionPerformed(new ActionEvent(e.getSource(),
87 ActionEvent.ACTION_PERFORMED,
92 public void set_pyros(AltosPyro[] new_pyros) {
95 public AltosPyro[] pyros() {
99 boolean is_telemetrum() {
100 String product = product_value.getText();
101 return product != null && product.startsWith("TeleGPS");
104 void set_radio_calibration_tool_tip() {
105 if (radio_calibration_value.isEnabled())
106 radio_calibration_value.setToolTipText("Tune radio output to match desired frequency");
108 radio_calibration_value.setToolTipText("Cannot tune radio while connected over packet mode");
111 void set_radio_enable_tool_tip() {
112 if (radio_enable_value.isEnabled())
113 radio_enable_value.setToolTipText("Enable/Disable telemetry and RDF transmissions");
115 radio_enable_value.setToolTipText("Firmware version does not support disabling radio");
118 void set_aprs_interval_tool_tip() {
119 if (aprs_interval_value.isEnabled())
120 aprs_interval_value.setToolTipText("Enable APRS and set the interval between APRS reports");
122 aprs_interval_value.setToolTipText("Hardware doesn't support APRS");
125 void set_flight_log_max_tool_tip() {
126 if (flight_log_max_value.isEnabled())
127 flight_log_max_value.setToolTipText("Size reserved for each flight log (in kB)");
129 flight_log_max_value.setToolTipText("Cannot set max value with flight logs in memory");
132 /* Build the UI using a grid bag */
133 public TeleGPSConfigUI(JFrame in_owner) {
134 super (in_owner, "Configure Flight Computer", false);
137 GridBagConstraints c;
140 Insets il = new Insets(4,4,4,4);
141 Insets ir = new Insets(4,4,4,4);
143 pane = getContentPane();
144 pane.setLayout(new GridBagLayout());
147 c = new GridBagConstraints();
148 c.gridx = 0; c.gridy = row;
150 c.fill = GridBagConstraints.NONE;
151 c.anchor = GridBagConstraints.LINE_START;
153 product_label = new JLabel("Product:");
154 pane.add(product_label, c);
156 c = new GridBagConstraints();
157 c.gridx = 4; c.gridy = row;
159 c.fill = GridBagConstraints.HORIZONTAL;
161 c.anchor = GridBagConstraints.LINE_START;
163 product_value = new JLabel("");
164 pane.add(product_value, c);
168 c = new GridBagConstraints();
169 c.gridx = 0; c.gridy = row;
171 c.fill = GridBagConstraints.NONE;
172 c.anchor = GridBagConstraints.LINE_START;
175 version_label = new JLabel("Software version:");
176 pane.add(version_label, c);
178 c = new GridBagConstraints();
179 c.gridx = 4; c.gridy = row;
181 c.fill = GridBagConstraints.HORIZONTAL;
183 c.anchor = GridBagConstraints.LINE_START;
186 version_value = new JLabel("");
187 pane.add(version_value, c);
191 c = new GridBagConstraints();
192 c.gridx = 0; c.gridy = row;
194 c.fill = GridBagConstraints.NONE;
195 c.anchor = GridBagConstraints.LINE_START;
198 serial_label = new JLabel("Serial:");
199 pane.add(serial_label, c);
201 c = new GridBagConstraints();
202 c.gridx = 4; c.gridy = row;
204 c.fill = GridBagConstraints.HORIZONTAL;
206 c.anchor = GridBagConstraints.LINE_START;
209 serial_value = new JLabel("");
210 pane.add(serial_value, c);
214 c = new GridBagConstraints();
215 c.gridx = 0; c.gridy = row;
217 c.fill = GridBagConstraints.NONE;
218 c.anchor = GridBagConstraints.LINE_START;
221 radio_frequency_label = new JLabel("Frequency:");
222 pane.add(radio_frequency_label, c);
224 c = new GridBagConstraints();
225 c.gridx = 4; c.gridy = row;
227 c.fill = GridBagConstraints.HORIZONTAL;
229 c.anchor = GridBagConstraints.LINE_START;
232 radio_frequency_value = new AltosFreqList();
233 radio_frequency_value.addItemListener(this);
234 pane.add(radio_frequency_value, c);
235 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
238 /* Radio Calibration */
239 c = new GridBagConstraints();
240 c.gridx = 0; c.gridy = row;
242 c.fill = GridBagConstraints.NONE;
243 c.anchor = GridBagConstraints.LINE_START;
246 radio_calibration_label = new JLabel("RF Calibration:");
247 pane.add(radio_calibration_label, c);
249 c = new GridBagConstraints();
250 c.gridx = 4; c.gridy = row;
252 c.fill = GridBagConstraints.HORIZONTAL;
254 c.anchor = GridBagConstraints.LINE_START;
257 radio_calibration_value = new JTextField(String.format("%d", 1186611));
258 radio_calibration_value.getDocument().addDocumentListener(this);
259 pane.add(radio_calibration_value, c);
260 set_radio_calibration_tool_tip();
264 c = new GridBagConstraints();
265 c.gridx = 0; c.gridy = row;
267 c.fill = GridBagConstraints.NONE;
268 c.anchor = GridBagConstraints.LINE_START;
271 radio_enable_label = new JLabel("Telemetry/RDF/APRS Enable:");
272 pane.add(radio_enable_label, c);
274 c = new GridBagConstraints();
275 c.gridx = 4; c.gridy = row;
277 c.fill = GridBagConstraints.HORIZONTAL;
279 c.anchor = GridBagConstraints.LINE_START;
282 radio_enable_value = new JRadioButton("Enabled");
283 radio_enable_value.addItemListener(this);
284 pane.add(radio_enable_value, c);
285 set_radio_enable_tool_tip();
289 c = new GridBagConstraints();
290 c.gridx = 0; c.gridy = row;
292 c.fill = GridBagConstraints.NONE;
293 c.anchor = GridBagConstraints.LINE_START;
296 aprs_interval_label = new JLabel("APRS Interval(s):");
297 pane.add(aprs_interval_label, c);
299 c = new GridBagConstraints();
300 c.gridx = 4; c.gridy = row;
302 c.fill = GridBagConstraints.HORIZONTAL;
304 c.anchor = GridBagConstraints.LINE_START;
307 aprs_interval_value = new JComboBox<String>(aprs_interval_values);
308 aprs_interval_value.setEditable(true);
309 aprs_interval_value.addItemListener(this);
310 pane.add(aprs_interval_value, c);
311 set_aprs_interval_tool_tip();
315 c = new GridBagConstraints();
316 c.gridx = 0; c.gridy = row;
318 c.fill = GridBagConstraints.NONE;
319 c.anchor = GridBagConstraints.LINE_START;
322 callsign_label = new JLabel("Callsign:");
323 pane.add(callsign_label, c);
325 c = new GridBagConstraints();
326 c.gridx = 4; c.gridy = row;
328 c.fill = GridBagConstraints.HORIZONTAL;
330 c.anchor = GridBagConstraints.LINE_START;
333 callsign_value = new JTextField(AltosUIPreferences.callsign());
334 callsign_value.getDocument().addDocumentListener(this);
335 pane.add(callsign_value, c);
336 callsign_value.setToolTipText("Callsign reported in telemetry data");
340 c = new GridBagConstraints();
341 c.gridx = 0; c.gridy = row;
343 c.fill = GridBagConstraints.NONE;
344 c.anchor = GridBagConstraints.LINE_START;
347 flight_log_max_label = new JLabel("Maximum Flight Log Size:");
348 pane.add(flight_log_max_label, c);
350 c = new GridBagConstraints();
351 c.gridx = 4; c.gridy = row;
353 c.fill = GridBagConstraints.HORIZONTAL;
355 c.anchor = GridBagConstraints.LINE_START;
358 flight_log_max_value = new JComboBox<String>(flight_log_max_values);
359 flight_log_max_value.setEditable(true);
360 flight_log_max_value.addItemListener(this);
361 pane.add(flight_log_max_value, c);
362 set_flight_log_max_tool_tip();
366 c = new GridBagConstraints();
367 c.gridx = 0; c.gridy = row;
369 c.fill = GridBagConstraints.NONE;
370 c.anchor = GridBagConstraints.LINE_START;
372 save = new JButton("Save");
374 save.addActionListener(this);
375 save.setActionCommand("Save");
377 c = new GridBagConstraints();
378 c.gridx = 2; c.gridy = row;
380 c.fill = GridBagConstraints.NONE;
381 c.anchor = GridBagConstraints.CENTER;
383 reset = new JButton("Reset");
385 reset.addActionListener(this);
386 reset.setActionCommand("Reset");
388 c = new GridBagConstraints();
389 c.gridx = 4; c.gridy = row;
391 c.fill = GridBagConstraints.NONE;
392 c.anchor = GridBagConstraints.CENTER;
394 reboot = new JButton("Reboot");
396 reboot.addActionListener(this);
397 reboot.setActionCommand("Reboot");
399 c = new GridBagConstraints();
400 c.gridx = 6; c.gridy = row;
402 c.fill = GridBagConstraints.NONE;
403 c.anchor = GridBagConstraints.LINE_END;
405 close = new JButton("Close");
407 close.addActionListener(this);
408 close.setActionCommand("Close");
410 addWindowListener(new ConfigListener(this));
413 /* Once the initial values are set, the config code will show the dialog */
414 public void make_visible() {
416 setLocationRelativeTo(owner);
420 /* If any values have been changed, confirm before closing */
421 public boolean check_dirty(String operation) {
423 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
425 i = JOptionPane.showOptionDialog(this,
426 String.format("Configuration modified. %s anyway?", operation),
427 "Configuration Modified",
428 JOptionPane.DEFAULT_OPTION,
429 JOptionPane.WARNING_MESSAGE,
430 null, options, options[1]);
439 save.setEnabled(true);
442 public void set_clean() {
444 save.setEnabled(false);
447 public void dispose() {
451 /* Listen for events from our buttons */
452 public void actionPerformed(ActionEvent e) {
453 String cmd = e.getActionCommand();
455 if (cmd.equals("Close") || cmd.equals("Reboot"))
456 if (!check_dirty(cmd))
458 listener.actionPerformed(e);
459 if (cmd.equals("Close") || cmd.equals("Reboot")) {
466 /* ItemListener interface method */
467 public void itemStateChanged(ItemEvent e) {
471 /* DocumentListener interface methods */
472 public void changedUpdate(DocumentEvent e) {
476 public void insertUpdate(DocumentEvent e) {
480 public void removeUpdate(DocumentEvent e) {
484 /* Let the config code hook on a listener */
485 public void addActionListener(ActionListener l) {
489 /* set and get all of the dialog values */
490 public void set_product(String product) {
491 radio_frequency_value.set_product(product);
492 product_value.setText(product);
493 set_flight_log_max_tool_tip();
496 public void set_version(String version) {
497 version_value.setText(version);
500 public void set_serial(int serial) {
501 radio_frequency_value.set_serial(serial);
502 serial_value.setText(String.format("%d", serial));
505 public void set_main_deploy(int new_main_deploy) {
508 public int main_deploy() {
513 String get_main_deploy_label() {
514 return String.format("Main Deploy Altitude(%s):", AltosConvert.height.show_units());
517 String[] main_deploy_values() {
518 if (AltosConvert.imperial_units)
519 return main_deploy_values_ft;
521 return main_deploy_values_m;
524 void set_main_deploy_values() {
525 String[] v = main_deploy_values();
526 while (main_deploy_value.getItemCount() > 0)
527 main_deploy_value.removeItemAt(0);
528 for (int i = 0; i < v.length; i++)
529 main_deploy_value.addItem(v[i]);
530 main_deploy_value.setMaximumRowCount(v.length);
534 public void set_apogee_delay(int new_apogee_delay) { }
536 public int apogee_delay() {
540 public void set_apogee_lockout(int new_apogee_lockout) { }
542 public int apogee_lockout() { return -1; }
544 public void set_radio_frequency(double new_radio_frequency) {
545 radio_frequency_value.set_frequency(new_radio_frequency);
548 public double radio_frequency() {
549 return radio_frequency_value.frequency();
552 public void set_radio_calibration(int new_radio_calibration) {
553 radio_calibration_value.setVisible(new_radio_calibration >= 0);
554 if (new_radio_calibration < 0)
555 radio_calibration_value.setText("Disabled");
557 radio_calibration_value.setText(String.format("%d", new_radio_calibration));
560 public int radio_calibration() {
561 return Integer.parseInt(radio_calibration_value.getText());
564 public void set_radio_enable(int new_radio_enable) {
565 if (new_radio_enable >= 0) {
566 radio_enable_value.setSelected(new_radio_enable > 0);
567 radio_enable_value.setEnabled(true);
569 radio_enable_value.setSelected(true);
570 radio_enable_value.setVisible(radio_frequency() > 0);
571 radio_enable_value.setEnabled(false);
573 set_radio_enable_tool_tip();
576 public int radio_enable() {
577 if (radio_enable_value.isEnabled())
578 return radio_enable_value.isSelected() ? 1 : 0;
583 public void set_callsign(String new_callsign) {
584 callsign_value.setVisible(new_callsign != null);
585 callsign_value.setText(new_callsign);
588 public String callsign() {
589 return callsign_value.getText();
592 public void set_flight_log_max(int new_flight_log_max) {
593 flight_log_max_value.setSelectedItem(Integer.toString(new_flight_log_max));
594 set_flight_log_max_tool_tip();
597 public void set_flight_log_max_enabled(boolean enable) {
598 flight_log_max_value.setEnabled(enable);
599 set_flight_log_max_tool_tip();
602 public int flight_log_max() {
603 return Integer.parseInt(flight_log_max_value.getSelectedItem().toString());
606 public void set_flight_log_max_limit(int flight_log_max_limit) {
607 //boolean any_added = false;
608 flight_log_max_value.removeAllItems();
609 for (int i = 0; i < flight_log_max_values.length; i++) {
610 if (Integer.parseInt(flight_log_max_values[i]) < flight_log_max_limit){
611 flight_log_max_value.addItem(flight_log_max_values[i]);
615 flight_log_max_value.addItem(String.format("%d", flight_log_max_limit));
618 public void set_ignite_mode(int new_ignite_mode) { }
619 public int ignite_mode() { return -1; }
622 public void set_pad_orientation(int new_pad_orientation) { }
623 public int pad_orientation() { return -1; }
625 public void set_beep(int new_beep) { }
627 public int beep() { return -1; }
629 public void set_aprs_interval(int new_aprs_interval) {
632 if (new_aprs_interval <= 0)
635 s = Integer.toString(new_aprs_interval);
636 aprs_interval_value.setSelectedItem(s);
637 aprs_interval_value.setVisible(new_aprs_interval >= 0);
638 set_aprs_interval_tool_tip();
641 public int aprs_interval() {
642 String s = aprs_interval_value.getSelectedItem().toString();
644 if (s.equals("Disabled"))
646 return Integer.parseInt(s);