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