altoslib: Remove debug printfs
[fw/altos] / telegps / TeleGPSConfigUI.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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.telegps;
20
21 import java.text.*;
22 import java.awt.*;
23 import java.awt.event.*;
24 import javax.swing.*;
25 import javax.swing.event.*;
26 import org.altusmetrum.altoslib_11.*;
27 import org.altusmetrum.altosuilib_11.*;
28
29 public class TeleGPSConfigUI
30         extends AltosUIDialog
31         implements ActionListener, ItemListener, DocumentListener, AltosConfigValues, AltosUnitsListener
32 {
33
34         Container               pane;
35         JLabel                  product_label;
36         JLabel                  version_label;
37         JLabel                  serial_label;
38         JLabel                  frequency_label;
39         JLabel                  radio_calibration_label;
40         JLabel                  radio_frequency_label;
41         JLabel                  radio_enable_label;
42         JLabel                  rate_label;
43         JLabel                  aprs_interval_label;
44         JLabel                  aprs_ssid_label;
45         JLabel                  aprs_format_label;
46         JLabel                  flight_log_max_label;
47         JLabel                  callsign_label;
48         JLabel                  tracker_motion_label;
49         JLabel                  tracker_interval_label;
50
51         public boolean          dirty;
52
53         JFrame                  owner;
54         JLabel                  product_value;
55         JLabel                  version_value;
56         JLabel                  serial_value;
57         AltosUIFreqList         radio_frequency_value;
58         JLabel                  radio_calibration_value;
59         JRadioButton            radio_enable_value;
60         AltosUIRateList         rate_value;
61         JComboBox<String>       aprs_interval_value;
62         JComboBox<Integer>      aprs_ssid_value;
63         JComboBox<String>       aprs_format_value;
64         JComboBox<String>       flight_log_max_value;
65         JTextField              callsign_value;
66         JComboBox<String>       tracker_motion_value;
67         JComboBox<String>       tracker_interval_value;
68
69         JButton                 save;
70         JButton                 reset;
71         JButton                 reboot;
72         JButton                 close;
73
74         ActionListener          listener;
75
76         static String[]         aprs_interval_values = {
77                 "Disabled",
78                 "2",
79                 "5",
80                 "10"
81         };
82
83         static Integer[]        aprs_ssid_values = {
84                 0, 1, 2 ,3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
85         };
86
87         static String[]         tracker_motion_values_m = {
88                 "2",
89                 "5",
90                 "10",
91                 "25",
92         };
93
94         static String[]         tracker_motion_values_ft = {
95                 "5",
96                 "20",
97                 "50",
98                 "100"
99         };
100
101         static String[]         tracker_interval_values = {
102                 "1",
103                 "2",
104                 "5",
105                 "10"
106         };
107
108         /* A window listener to catch closing events and tell the config code */
109         class ConfigListener extends WindowAdapter {
110                 TeleGPSConfigUI ui;
111
112                 public ConfigListener(TeleGPSConfigUI this_ui) {
113                         ui = this_ui;
114                 }
115
116                 public void windowClosing(WindowEvent e) {
117                         ui.actionPerformed(new ActionEvent(e.getSource(),
118                                                            ActionEvent.ACTION_PERFORMED,
119                                                            "Close"));
120                 }
121         }
122
123         public void set_pyros(AltosPyro[] new_pyros) {
124         }
125
126         public AltosPyro[] pyros() {
127                 return null;
128         }
129
130         public void set_pyro_firing_time(double new_pyro_firing_time) {
131         }
132
133         public double pyro_firing_time() {
134                 return -1;
135         }
136
137         boolean is_telemetrum() {
138                 String  product = product_value.getText();
139                 return product != null && product.startsWith("TeleGPS");
140         }
141
142         void set_radio_enable_tool_tip() {
143                 if (radio_enable_value.isEnabled())
144                         radio_enable_value.setToolTipText("Enable/Disable telemetry and RDF transmissions");
145                 else
146                         radio_enable_value.setToolTipText("Firmware version does not support disabling radio");
147         }
148
149         void set_rate_tool_tip() {
150                 if (rate_value.isEnabled())
151                         rate_value.setToolTipText("Select telemetry baud rate");
152                 else
153                         rate_value.setToolTipText("Firmware version does not support variable telemetry rates");
154         }
155
156         void set_aprs_interval_tool_tip() {
157                 if (aprs_interval_value.isEnabled())
158                         aprs_interval_value.setToolTipText("Enable APRS and set the interval between APRS reports");
159                 else
160                         aprs_interval_value.setToolTipText("Hardware doesn't support APRS");
161         }
162
163         void set_aprs_ssid_tool_tip() {
164                 if (aprs_ssid_value.isEnabled())
165                         aprs_ssid_value.setToolTipText("Set the APRS SSID (secondary station identifier)");
166                 else if (aprs_ssid_value.isEnabled())
167                         aprs_ssid_value.setToolTipText("Software version doesn't support setting the APRS SSID");
168                 else
169                         aprs_ssid_value.setToolTipText("Hardware doesn't support APRS");
170         }
171
172         void set_aprs_format_tool_tip() {
173                 if (aprs_format_value.isEnabled())
174                         aprs_format_value.setToolTipText("Set the APRS format (compressed/uncompressed)");
175                 else if (aprs_format_value.isEnabled())
176                         aprs_format_value.setToolTipText("Software version doesn't support setting the APRS format");
177                 else
178                         aprs_format_value.setToolTipText("Hardware doesn't support APRS");
179         }
180
181         void set_flight_log_max_tool_tip() {
182                 if (flight_log_max_value.isEnabled())
183                         flight_log_max_value.setToolTipText("Size reserved for each flight log (in kB)");
184                 else
185                         flight_log_max_value.setToolTipText("Cannot set max value with flight logs in memory");
186         }
187
188         /* Build the UI using a grid bag */
189         public TeleGPSConfigUI(JFrame in_owner) {
190                 super (in_owner, "Configure Device", false);
191
192                 owner = in_owner;
193                 GridBagConstraints c;
194                 int row = 0;
195
196                 Insets il = new Insets(4,4,4,4);
197                 Insets ir = new Insets(4,4,4,4);
198
199                 pane = getContentPane();
200                 pane.setLayout(new GridBagLayout());
201
202                 /* Product */
203                 c = new GridBagConstraints();
204                 c.gridx = 0; c.gridy = row;
205                 c.gridwidth = 4;
206                 c.fill = GridBagConstraints.NONE;
207                 c.anchor = GridBagConstraints.LINE_START;
208                 c.insets = il;
209                 product_label = new JLabel("Product:");
210                 pane.add(product_label, c);
211
212                 c = new GridBagConstraints();
213                 c.gridx = 4; c.gridy = row;
214                 c.gridwidth = 4;
215                 c.fill = GridBagConstraints.HORIZONTAL;
216                 c.weightx = 1;
217                 c.anchor = GridBagConstraints.LINE_START;
218                 c.insets = ir;
219                 product_value = new JLabel("");
220                 pane.add(product_value, c);
221                 row++;
222
223                 /* Version */
224                 c = new GridBagConstraints();
225                 c.gridx = 0; c.gridy = row;
226                 c.gridwidth = 4;
227                 c.fill = GridBagConstraints.NONE;
228                 c.anchor = GridBagConstraints.LINE_START;
229                 c.insets = il;
230                 c.ipady = 5;
231                 version_label = new JLabel("Software version:");
232                 pane.add(version_label, c);
233
234                 c = new GridBagConstraints();
235                 c.gridx = 4; c.gridy = row;
236                 c.gridwidth = 4;
237                 c.fill = GridBagConstraints.HORIZONTAL;
238                 c.weightx = 1;
239                 c.anchor = GridBagConstraints.LINE_START;
240                 c.insets = ir;
241                 c.ipady = 5;
242                 version_value = new JLabel("");
243                 pane.add(version_value, c);
244                 row++;
245
246                 /* Serial */
247                 c = new GridBagConstraints();
248                 c.gridx = 0; c.gridy = row;
249                 c.gridwidth = 4;
250                 c.fill = GridBagConstraints.NONE;
251                 c.anchor = GridBagConstraints.LINE_START;
252                 c.insets = il;
253                 c.ipady = 5;
254                 serial_label = new JLabel("Serial:");
255                 pane.add(serial_label, c);
256
257                 c = new GridBagConstraints();
258                 c.gridx = 4; c.gridy = row;
259                 c.gridwidth = 4;
260                 c.fill = GridBagConstraints.HORIZONTAL;
261                 c.weightx = 1;
262                 c.anchor = GridBagConstraints.LINE_START;
263                 c.insets = ir;
264                 c.ipady = 5;
265                 serial_value = new JLabel("");
266                 pane.add(serial_value, c);
267                 row++;
268
269                 /* Frequency */
270                 c = new GridBagConstraints();
271                 c.gridx = 0; c.gridy = row;
272                 c.gridwidth = 4;
273                 c.fill = GridBagConstraints.NONE;
274                 c.anchor = GridBagConstraints.LINE_START;
275                 c.insets = il;
276                 c.ipady = 5;
277                 radio_frequency_label = new JLabel("Frequency:");
278                 pane.add(radio_frequency_label, c);
279
280                 c = new GridBagConstraints();
281                 c.gridx = 4; c.gridy = row;
282                 c.gridwidth = 4;
283                 c.fill = GridBagConstraints.HORIZONTAL;
284                 c.weightx = 1;
285                 c.anchor = GridBagConstraints.LINE_START;
286                 c.insets = ir;
287                 c.ipady = 5;
288                 radio_frequency_value = new AltosUIFreqList();
289                 radio_frequency_value.addItemListener(this);
290                 pane.add(radio_frequency_value, c);
291                 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
292                 row++;
293
294                 /* Radio Calibration */
295                 c = new GridBagConstraints();
296                 c.gridx = 0; c.gridy = row;
297                 c.gridwidth = 4;
298                 c.fill = GridBagConstraints.NONE;
299                 c.anchor = GridBagConstraints.LINE_START;
300                 c.insets = il;
301                 c.ipady = 5;
302                 radio_calibration_label = new JLabel("RF Calibration:");
303                 pane.add(radio_calibration_label, c);
304
305                 c = new GridBagConstraints();
306                 c.gridx = 4; c.gridy = row;
307                 c.gridwidth = 4;
308                 c.fill = GridBagConstraints.HORIZONTAL;
309                 c.weightx = 1;
310                 c.anchor = GridBagConstraints.LINE_START;
311                 c.insets = ir;
312                 c.ipady = 5;
313                 radio_calibration_value = new JLabel(String.format("%d", 1186611));
314                 pane.add(radio_calibration_value, c);
315                 row++;
316
317                 /* Radio Enable */
318                 c = new GridBagConstraints();
319                 c.gridx = 0; c.gridy = row;
320                 c.gridwidth = 4;
321                 c.fill = GridBagConstraints.NONE;
322                 c.anchor = GridBagConstraints.LINE_START;
323                 c.insets = il;
324                 c.ipady = 5;
325                 radio_enable_label = new JLabel("Telemetry/RDF/APRS Enable:");
326                 pane.add(radio_enable_label, c);
327
328                 c = new GridBagConstraints();
329                 c.gridx = 4; c.gridy = row;
330                 c.gridwidth = 4;
331                 c.fill = GridBagConstraints.HORIZONTAL;
332                 c.weightx = 1;
333                 c.anchor = GridBagConstraints.LINE_START;
334                 c.insets = ir;
335                 c.ipady = 5;
336                 radio_enable_value = new JRadioButton("Enabled");
337                 radio_enable_value.addItemListener(this);
338                 pane.add(radio_enable_value, c);
339                 set_radio_enable_tool_tip();
340                 row++;
341
342                 /* Telemetry Rate */
343                 c = new GridBagConstraints();
344                 c.gridx = 0; c.gridy = row;
345                 c.gridwidth = 4;
346                 c.fill = GridBagConstraints.NONE;
347                 c.anchor = GridBagConstraints.LINE_START;
348                 c.insets = il;
349                 c.ipady = 5;
350                 rate_label = new JLabel("Telemetry baud rate:");
351                 pane.add(rate_label, c);
352
353                 c = new GridBagConstraints();
354                 c.gridx = 4; c.gridy = row;
355                 c.gridwidth = 4;
356                 c.fill = GridBagConstraints.HORIZONTAL;
357                 c.weightx = 1;
358                 c.anchor = GridBagConstraints.LINE_START;
359                 c.insets = ir;
360                 c.ipady = 5;
361                 rate_value = new AltosUIRateList();
362                 rate_value.addItemListener(this);
363                 pane.add(rate_value, c);
364                 set_rate_tool_tip();
365                 row++;
366
367                 /* APRS interval */
368                 c = new GridBagConstraints();
369                 c.gridx = 0; c.gridy = row;
370                 c.gridwidth = 4;
371                 c.fill = GridBagConstraints.NONE;
372                 c.anchor = GridBagConstraints.LINE_START;
373                 c.insets = il;
374                 c.ipady = 5;
375                 aprs_interval_label = new JLabel("APRS Interval(s):");
376                 pane.add(aprs_interval_label, c);
377
378                 c = new GridBagConstraints();
379                 c.gridx = 4; c.gridy = row;
380                 c.gridwidth = 4;
381                 c.fill = GridBagConstraints.HORIZONTAL;
382                 c.weightx = 1;
383                 c.anchor = GridBagConstraints.LINE_START;
384                 c.insets = ir;
385                 c.ipady = 5;
386                 aprs_interval_value = new JComboBox<String>(aprs_interval_values);
387                 aprs_interval_value.setEditable(true);
388                 aprs_interval_value.addItemListener(this);
389                 pane.add(aprs_interval_value, c);
390                 set_aprs_interval_tool_tip();
391                 row++;
392
393                 /* APRS SSID */
394                 c = new GridBagConstraints();
395                 c.gridx = 0; c.gridy = row;
396                 c.gridwidth = 4;
397                 c.fill = GridBagConstraints.NONE;
398                 c.anchor = GridBagConstraints.LINE_START;
399                 c.insets = il;
400                 c.ipady = 5;
401                 aprs_ssid_label = new JLabel("APRS SSID:");
402                 pane.add(aprs_ssid_label, c);
403
404                 c = new GridBagConstraints();
405                 c.gridx = 4; c.gridy = row;
406                 c.gridwidth = 4;
407                 c.fill = GridBagConstraints.HORIZONTAL;
408                 c.weightx = 1;
409                 c.anchor = GridBagConstraints.LINE_START;
410                 c.insets = ir;
411                 c.ipady = 5;
412                 aprs_ssid_value = new JComboBox<Integer>(aprs_ssid_values);
413                 aprs_ssid_value.setEditable(false);
414                 aprs_ssid_value.addItemListener(this);
415                 aprs_ssid_value.setMaximumRowCount(aprs_ssid_values.length);
416                 pane.add(aprs_ssid_value, c);
417                 set_aprs_ssid_tool_tip();
418                 row++;
419
420                 /* APRS format */
421                 c = new GridBagConstraints();
422                 c.gridx = 0; c.gridy = row;
423                 c.gridwidth = 4;
424                 c.fill = GridBagConstraints.NONE;
425                 c.anchor = GridBagConstraints.LINE_START;
426                 c.insets = il;
427                 c.ipady = 5;
428                 aprs_format_label = new JLabel("APRS format:");
429                 pane.add(aprs_format_label, c);
430
431                 c = new GridBagConstraints();
432                 c.gridx = 4; c.gridy = row;
433                 c.gridwidth = 4;
434                 c.fill = GridBagConstraints.HORIZONTAL;
435                 c.weightx = 1;
436                 c.anchor = GridBagConstraints.LINE_START;
437                 c.insets = ir;
438                 c.ipady = 5;
439                 aprs_format_value = new JComboBox<String>(AltosLib.ao_aprs_format_name);
440                 aprs_format_value.setEditable(false);
441                 aprs_format_value.addItemListener(this);
442                 aprs_format_value.setMaximumRowCount(AltosLib.ao_aprs_format_name.length);
443                 pane.add(aprs_format_value, c);
444                 set_aprs_format_tool_tip();
445                 row++;
446
447                 /* Callsign */
448                 c = new GridBagConstraints();
449                 c.gridx = 0; c.gridy = row;
450                 c.gridwidth = 4;
451                 c.fill = GridBagConstraints.NONE;
452                 c.anchor = GridBagConstraints.LINE_START;
453                 c.insets = il;
454                 c.ipady = 5;
455                 callsign_label = new JLabel("Callsign:");
456                 pane.add(callsign_label, c);
457
458                 c = new GridBagConstraints();
459                 c.gridx = 4; c.gridy = row;
460                 c.gridwidth = 4;
461                 c.fill = GridBagConstraints.HORIZONTAL;
462                 c.weightx = 1;
463                 c.anchor = GridBagConstraints.LINE_START;
464                 c.insets = ir;
465                 c.ipady = 5;
466                 callsign_value = new JTextField(AltosUIPreferences.callsign());
467                 callsign_value.getDocument().addDocumentListener(this);
468                 pane.add(callsign_value, c);
469                 callsign_value.setToolTipText("Callsign reported in telemetry data");
470                 row++;
471
472                 /* Flight log max */
473                 c = new GridBagConstraints();
474                 c.gridx = 0; c.gridy = row;
475                 c.gridwidth = 4;
476                 c.fill = GridBagConstraints.NONE;
477                 c.anchor = GridBagConstraints.LINE_START;
478                 c.insets = il;
479                 c.ipady = 5;
480                 flight_log_max_label = new JLabel("Maximum Log Size (kB):");
481                 pane.add(flight_log_max_label, c);
482
483                 c = new GridBagConstraints();
484                 c.gridx = 4; c.gridy = row;
485                 c.gridwidth = 4;
486                 c.fill = GridBagConstraints.HORIZONTAL;
487                 c.weightx = 1;
488                 c.anchor = GridBagConstraints.LINE_START;
489                 c.insets = ir;
490                 c.ipady = 5;
491                 flight_log_max_value = new JComboBox<String>();
492                 flight_log_max_value.setEditable(true);
493                 flight_log_max_value.addItemListener(this);
494                 pane.add(flight_log_max_value, c);
495                 set_flight_log_max_tool_tip();
496                 row++;
497
498                 /* Tracker triger horiz distances */
499                 c = new GridBagConstraints();
500                 c.gridx = 0; c.gridy = row;
501                 c.gridwidth = 4;
502                 c.fill = GridBagConstraints.NONE;
503                 c.anchor = GridBagConstraints.LINE_START;
504                 c.insets = il;
505                 c.ipady = 5;
506                 tracker_motion_label = new JLabel(get_tracker_motion_label());
507                 pane.add(tracker_motion_label, c);
508
509                 c = new GridBagConstraints();
510                 c.gridx = 4; c.gridy = row;
511                 c.gridwidth = 4;
512                 c.fill = GridBagConstraints.HORIZONTAL;
513                 c.weightx = 1;
514                 c.anchor = GridBagConstraints.LINE_START;
515                 c.insets = ir;
516                 c.ipady = 5;
517                 tracker_motion_value = new JComboBox<String>(tracker_motion_values());
518                 tracker_motion_value.setEditable(true);
519                 tracker_motion_value.addItemListener(this);
520                 pane.add(tracker_motion_value, c);
521                 row++;
522
523                 /* Tracker triger vert distances */
524                 c = new GridBagConstraints();
525                 c.gridx = 0; c.gridy = row;
526                 c.gridwidth = 4;
527                 c.fill = GridBagConstraints.NONE;
528                 c.anchor = GridBagConstraints.LINE_START;
529                 c.insets = il;
530                 c.ipady = 5;
531                 tracker_interval_label = new JLabel("Position Reporting Interval (s):");
532                 pane.add(tracker_interval_label, c);
533
534                 c = new GridBagConstraints();
535                 c.gridx = 4; c.gridy = row;
536                 c.gridwidth = 4;
537                 c.fill = GridBagConstraints.HORIZONTAL;
538                 c.weightx = 1;
539                 c.anchor = GridBagConstraints.LINE_START;
540                 c.insets = ir;
541                 c.ipady = 5;
542                 tracker_interval_value = new JComboBox<String>(tracker_interval_values);
543                 tracker_interval_value.setEditable(true);
544                 tracker_interval_value.addItemListener(this);
545                 pane.add(tracker_interval_value, c);
546                 set_tracker_tool_tip();
547                 row++;
548
549                 /* Buttons */
550                 c = new GridBagConstraints();
551                 c.gridx = 0; c.gridy = row;
552                 c.gridwidth = 2;
553                 c.fill = GridBagConstraints.NONE;
554                 c.anchor = GridBagConstraints.LINE_START;
555                 c.insets = il;
556                 save = new JButton("Save");
557                 pane.add(save, c);
558                 save.addActionListener(this);
559                 save.setActionCommand("Save");
560
561                 c = new GridBagConstraints();
562                 c.gridx = 2; c.gridy = row;
563                 c.gridwidth = 2;
564                 c.fill = GridBagConstraints.NONE;
565                 c.anchor = GridBagConstraints.CENTER;
566                 c.insets = il;
567                 reset = new JButton("Reset");
568                 pane.add(reset, c);
569                 reset.addActionListener(this);
570                 reset.setActionCommand("Reset");
571
572                 c = new GridBagConstraints();
573                 c.gridx = 4; c.gridy = row;
574                 c.gridwidth = 2;
575                 c.fill = GridBagConstraints.NONE;
576                 c.anchor = GridBagConstraints.CENTER;
577                 c.insets = il;
578                 reboot = new JButton("Reboot");
579                 pane.add(reboot, c);
580                 reboot.addActionListener(this);
581                 reboot.setActionCommand("Reboot");
582
583                 c = new GridBagConstraints();
584                 c.gridx = 6; c.gridy = row;
585                 c.gridwidth = 2;
586                 c.fill = GridBagConstraints.NONE;
587                 c.anchor = GridBagConstraints.LINE_END;
588                 c.insets = il;
589                 close = new JButton("Close");
590                 pane.add(close, c);
591                 close.addActionListener(this);
592                 close.setActionCommand("Close");
593
594                 addWindowListener(new ConfigListener(this));
595                 AltosPreferences.register_units_listener(this);
596         }
597
598         /* Once the initial values are set, the config code will show the dialog */
599         public void make_visible() {
600                 pack();
601                 setLocationRelativeTo(owner);
602                 setVisible(true);
603         }
604
605         /* If any values have been changed, confirm before closing */
606         public boolean check_dirty(String operation) {
607                 if (dirty) {
608                         Object[] options = { String.format("%s anyway", operation), "Keep editing" };
609                         int i;
610                         i = JOptionPane.showOptionDialog(this,
611                                                          String.format("Configuration modified. %s anyway?", operation),
612                                                          "Configuration Modified",
613                                                          JOptionPane.DEFAULT_OPTION,
614                                                          JOptionPane.WARNING_MESSAGE,
615                                                          null, options, options[1]);
616                         if (i != 0)
617                                 return false;
618                 }
619                 return true;
620         }
621
622         void set_dirty() {
623                 dirty = true;
624                 save.setEnabled(true);
625         }
626
627         public void set_clean() {
628                 dirty = false;
629                 save.setEnabled(false);
630         }
631
632         public void dispose() {
633                 AltosPreferences.unregister_units_listener(this);
634                 super.dispose();
635         }
636
637         /* Listen for events from our buttons */
638         public void actionPerformed(ActionEvent e) {
639                 String  cmd = e.getActionCommand();
640
641                 if (cmd.equals("Close") || cmd.equals("Reboot"))
642                         if (!check_dirty(cmd))
643                                 return;
644                 listener.actionPerformed(e);
645                 if (cmd.equals("Close") || cmd.equals("Reboot")) {
646                         setVisible(false);
647                         dispose();
648                 }
649                 set_clean();
650         }
651
652         /* ItemListener interface method */
653         public void itemStateChanged(ItemEvent e) {
654                 set_dirty();
655         }
656
657         /* DocumentListener interface methods */
658         public void changedUpdate(DocumentEvent e) {
659                 set_dirty();
660         }
661
662         public void insertUpdate(DocumentEvent e) {
663                 set_dirty();
664         }
665
666         public void removeUpdate(DocumentEvent e) {
667                 set_dirty();
668         }
669
670         /* Let the config code hook on a listener */
671         public void addActionListener(ActionListener l) {
672                 listener = l;
673         }
674
675         public void units_changed(boolean imperial_units) {
676                 boolean was_dirty = dirty;
677
678                 if (tracker_motion_value.isEnabled()) {
679                         String motion = tracker_motion_value.getSelectedItem().toString();
680                         tracker_motion_label.setText(get_tracker_motion_label());
681                         set_tracker_motion_values();
682                         try {
683                                 int m = (int) (AltosConvert.height.parse_locale(motion, !imperial_units) + 0.5);
684                                 set_tracker_motion(m);
685                         } catch (ParseException pe) {
686                         }
687                 }
688                 if (!was_dirty)
689                         set_clean();
690         }
691
692         /* set and get all of the dialog values */
693         public void set_product(String product) {
694                 radio_frequency_value.set_product(product);
695                 product_value.setText(product);
696                 set_flight_log_max_tool_tip();
697         }
698
699         public void set_version(String version) {
700                 version_value.setText(version);
701         }
702
703         public void set_serial(int serial) {
704                 radio_frequency_value.set_serial(serial);
705                 serial_value.setText(String.format("%d", serial));
706         }
707
708         public void set_altitude_32(int altitude_32) {
709         }
710
711         public void set_main_deploy(int new_main_deploy) {
712         }
713
714         public int main_deploy() {
715                 return -1;
716         }
717
718         public void set_apogee_delay(int new_apogee_delay) { }
719
720         public int apogee_delay() {
721                 return -1;
722         }
723
724         public void set_apogee_lockout(int new_apogee_lockout) { }
725
726         public int apogee_lockout() { return -1; }
727
728         public void set_radio_frequency(double new_radio_frequency) {
729                 radio_frequency_value.set_frequency(new_radio_frequency);
730         }
731
732         public double radio_frequency() {
733                 return radio_frequency_value.frequency();
734         }
735
736         public void set_radio_calibration(int new_radio_calibration) {
737                 radio_calibration_value.setVisible(new_radio_calibration >= 0);
738                 if (new_radio_calibration < 0)
739                         radio_calibration_value.setText("Disabled");
740                 else
741                         radio_calibration_value.setText(String.format("%d", new_radio_calibration));
742         }
743
744         private int parse_int(String name, String s, boolean split) throws AltosConfigDataException {
745                 String v = s;
746                 if (split)
747                         v = s.split("\\s+")[0];
748                 try {
749                         return Integer.parseInt(v);
750                 } catch (NumberFormatException ne) {
751                         throw new AltosConfigDataException("Invalid %s \"%s\"", name, s);
752                 }
753         }
754
755         public void set_radio_enable(int new_radio_enable) {
756                 if (new_radio_enable >= 0) {
757                         radio_enable_value.setSelected(new_radio_enable > 0);
758                         radio_enable_value.setEnabled(true);
759                 } else {
760                         radio_enable_value.setSelected(true);
761                         radio_enable_value.setVisible(radio_frequency() > 0);
762                         radio_enable_value.setEnabled(false);
763                 }
764                 set_radio_enable_tool_tip();
765         }
766
767         public int radio_enable() {
768                 if (radio_enable_value.isEnabled())
769                         return radio_enable_value.isSelected() ? 1 : 0;
770                 else
771                         return -1;
772         }
773
774         public void set_telemetry_rate(int new_rate) {
775                 rate_value.set_rate(new_rate);
776         }
777
778         public int telemetry_rate() {
779                 return rate_value.rate();
780         }
781
782         public void set_callsign(String new_callsign) {
783                 callsign_value.setVisible(new_callsign != null);
784                 callsign_value.setText(new_callsign);
785         }
786
787         public String callsign() {
788                 return callsign_value.getText();
789         }
790
791         int     flight_log_max_limit;
792         int     flight_log_max;
793
794         public String flight_log_max_label(int flight_log_max) {
795                 if (flight_log_max_limit != 0) {
796                         int     nflight = flight_log_max_limit / flight_log_max;
797                         String  plural = nflight > 1 ? "s" : "";
798
799                         return String.format("%d (%d flight%s)", flight_log_max, nflight, plural);
800                 }
801                 return String.format("%d", flight_log_max);
802         }
803
804         public void set_flight_log_max(int new_flight_log_max) {
805                 flight_log_max_value.setSelectedItem(flight_log_max_label(new_flight_log_max));
806                 flight_log_max = new_flight_log_max;
807                 set_flight_log_max_tool_tip();
808         }
809
810         public void set_flight_log_max_enabled(boolean enable) {
811                 flight_log_max_value.setEnabled(enable);
812                 set_flight_log_max_tool_tip();
813         }
814
815         public int flight_log_max() throws AltosConfigDataException {
816                 return parse_int("flight log max", flight_log_max_value.getSelectedItem().toString(), true);
817         }
818
819         public void set_flight_log_max_limit(int new_flight_log_max_limit) {
820                 flight_log_max_limit = new_flight_log_max_limit;
821                 flight_log_max_value.removeAllItems();
822                 for (int i = 8; i >= 1; i--) {
823                         int     size = flight_log_max_limit / i;
824                         flight_log_max_value.addItem(String.format("%d (%d flights)", size, i));
825                 }
826                 if (flight_log_max != 0)
827                         set_flight_log_max(flight_log_max);
828         }
829
830         public void set_ignite_mode(int new_ignite_mode) { }
831         public int ignite_mode() { return -1; }
832
833
834         public void set_pad_orientation(int new_pad_orientation) { }
835         public int pad_orientation() { return -1; }
836
837         public void set_beep(int new_beep) { }
838
839         public int beep() { return -1; }
840
841         String[] tracker_motion_values() {
842                 if (AltosConvert.imperial_units)
843                         return tracker_motion_values_ft;
844                 else
845                         return tracker_motion_values_m;
846         }
847
848         void set_tracker_motion_values() {
849                 String[]        v = tracker_motion_values();
850                 while (tracker_motion_value.getItemCount() > 0)
851                         tracker_motion_value.removeItemAt(0);
852                 for (int i = 0; i < v.length; i++)
853                         tracker_motion_value.addItem(v[i]);
854                 tracker_motion_value.setMaximumRowCount(v.length);
855         }
856
857         String get_tracker_motion_label() {
858                 return String.format("Logging Trigger Motion (%s):", AltosConvert.height.parse_units());
859         }
860
861         void set_tracker_tool_tip() {
862                 if (tracker_motion_value.isEnabled())
863                         tracker_motion_value.setToolTipText("How far the device must move before logging");
864                 else
865                         tracker_motion_value.setToolTipText("This device doesn't disable logging when stationary");
866                 if (tracker_interval_value.isEnabled())
867                         tracker_interval_value.setToolTipText("How often to report GPS position");
868                 else
869                         tracker_interval_value.setToolTipText("This device can't configure interval");
870         }
871
872         public void set_tracker_motion(int tracker_motion) {
873                 if (tracker_motion < 0) {
874                         tracker_motion_value.setEnabled(false);
875                 } else {
876                         tracker_motion_value.setEnabled(true);
877                         tracker_motion_value.setSelectedItem(AltosConvert.height.say(tracker_motion));
878                 }
879         }
880
881         public int tracker_motion() throws AltosConfigDataException {
882                 String str = tracker_motion_value.getSelectedItem().toString();
883                 try {
884                         return (int) (AltosConvert.height.parse_locale(str) + 0.5);
885                 } catch (ParseException pe) {
886                         throw new AltosConfigDataException("invalid tracker motion %s", str);
887                 }
888         }
889
890         public void set_tracker_interval(int tracker_interval) {
891                 if (tracker_interval< 0) {
892                         tracker_interval_value.setEnabled(false);
893                 } else {
894                         tracker_interval_value.setEnabled(true);
895                         tracker_interval_value.setSelectedItem(String.format("%d", tracker_interval));
896                 }
897         }
898
899         public int tracker_interval() throws AltosConfigDataException {
900                 return parse_int ("tracker interval", tracker_interval_value.getSelectedItem().toString(), false);
901         }
902
903         public void set_aprs_interval(int new_aprs_interval) {
904                 String  s;
905
906                 if (new_aprs_interval <= 0)
907                         s = "Disabled";
908                 else
909                         s = Integer.toString(new_aprs_interval);
910                 aprs_interval_value.setSelectedItem(s);
911                 aprs_interval_value.setVisible(new_aprs_interval >= 0);
912                 set_aprs_interval_tool_tip();
913         }
914
915         public int aprs_interval() throws AltosConfigDataException {
916                 String  s = aprs_interval_value.getSelectedItem().toString();
917
918                 if (s.equals("Disabled"))
919                         return 0;
920                 return parse_int("aprs interval", s, false);
921         }
922
923         public void set_aprs_ssid(int new_aprs_ssid) {
924                 aprs_ssid_value.setSelectedItem(Math.max(0,new_aprs_ssid));
925                 aprs_ssid_value.setVisible(new_aprs_ssid >= 0);
926                 set_aprs_ssid_tool_tip();
927         }
928
929         public int aprs_ssid() throws AltosConfigDataException {
930                 Integer i = (Integer) aprs_ssid_value.getSelectedItem();
931                 return i;
932         }
933
934         public void set_aprs_format(int new_aprs_format) {
935                 aprs_format_value.setVisible(new_aprs_format >= 0);
936                 aprs_format_label.setVisible(new_aprs_format >= 0);
937
938                 aprs_format_value.setSelectedIndex(Math.max(0,new_aprs_format));
939                 set_aprs_format_tool_tip();
940         }
941
942         public int aprs_format() throws AltosConfigDataException {
943                 return aprs_format_value.getSelectedIndex();
944         }
945 }