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