altos/stm32f4-disco: Start hooking up stm32f413 USB for the disco board
[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_13.*;
27 import org.altusmetrum.altosuilib_13.*;
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 AltosLib.MISSING;
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.isVisible())
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.isVisible())
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.isVisible())
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.isVisible())
165                         aprs_ssid_value.setToolTipText("Set the APRS SSID (secondary station identifier)");
166                 else if (aprs_ssid_value.isVisible())
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.isVisible())
174                         aprs_format_value.setToolTipText("Set the APRS format (compressed/uncompressed)");
175                 else if (aprs_format_value.isVisible())
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.isVisible())
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         public 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         public int accel_cal_plus() {
638                 return AltosLib.MISSING;
639         }
640
641         public int accel_cal_minus() {
642                 return AltosLib.MISSING;
643         }
644
645         public void set_accel_cal(int accel_plus, int accel_minus) {
646         }
647
648         /* Listen for events from our buttons */
649         public void actionPerformed(ActionEvent e) {
650                 String  cmd = e.getActionCommand();
651
652                 if (cmd.equals("Close") || cmd.equals("Reboot"))
653                         if (!check_dirty(cmd))
654                                 return;
655                 listener.actionPerformed(e);
656                 if (cmd.equals("Close") || cmd.equals("Reboot")) {
657                         setVisible(false);
658                         dispose();
659                 }
660                 set_clean();
661         }
662
663         /* ItemListener interface method */
664         public void itemStateChanged(ItemEvent e) {
665                 set_dirty();
666         }
667
668         /* DocumentListener interface methods */
669         public void changedUpdate(DocumentEvent e) {
670                 set_dirty();
671         }
672
673         public void insertUpdate(DocumentEvent e) {
674                 set_dirty();
675         }
676
677         public void removeUpdate(DocumentEvent e) {
678                 set_dirty();
679         }
680
681         /* Let the config code hook on a listener */
682         public void addActionListener(ActionListener l) {
683                 listener = l;
684         }
685
686         public void units_changed(boolean imperial_units) {
687                 boolean was_dirty = dirty;
688
689                 if (tracker_motion_value.isVisible()) {
690                         String motion = tracker_motion_value.getSelectedItem().toString();
691                         tracker_motion_label.setText(get_tracker_motion_label());
692                         set_tracker_motion_values();
693                         try {
694                                 int m = (int) (AltosConvert.height.parse_locale(motion, !imperial_units) + 0.5);
695                                 set_tracker_motion(m);
696                         } catch (ParseException pe) {
697                         }
698                 }
699                 if (!was_dirty)
700                         set_clean();
701         }
702
703         /* set and get all of the dialog values */
704         public void set_product(String product) {
705                 radio_frequency_value.set_product(product);
706                 product_value.setText(product);
707                 set_flight_log_max_tool_tip();
708         }
709
710         public void set_version(String version) {
711                 version_value.setText(version);
712         }
713
714         public void set_serial(int serial) {
715                 radio_frequency_value.set_serial(serial);
716                 serial_value.setText(String.format("%d", serial));
717         }
718
719         public void set_altitude_32(int altitude_32) {
720         }
721
722         public void set_main_deploy(int new_main_deploy) {
723         }
724
725         public int main_deploy() {
726                 return AltosLib.MISSING;
727         }
728
729         public void set_apogee_delay(int new_apogee_delay) { }
730
731         public int apogee_delay() {
732                 return AltosLib.MISSING;
733         }
734
735         public void set_apogee_lockout(int new_apogee_lockout) { }
736
737         public int apogee_lockout() { return AltosLib.MISSING; }
738
739         public void set_radio_frequency(double new_radio_frequency) {
740                 if (new_radio_frequency != AltosLib.MISSING)
741                         radio_frequency_value.set_frequency(new_radio_frequency);
742                 radio_frequency_label.setVisible(new_radio_frequency != AltosLib.MISSING);
743                 radio_frequency_value.setVisible(new_radio_frequency != AltosLib.MISSING);
744         }
745
746         public double radio_frequency() {
747                 return radio_frequency_value.frequency();
748         }
749
750         public void set_radio_calibration(int new_radio_calibration) {
751                 if (new_radio_calibration != AltosLib.MISSING)
752                         radio_calibration_value.setText(String.format("%d", new_radio_calibration));
753                 radio_calibration_value.setVisible(new_radio_calibration == AltosLib.MISSING);
754                 radio_calibration_label.setVisible(new_radio_calibration == AltosLib.MISSING);
755         }
756
757         public void set_radio_enable(int new_radio_enable) {
758                 if (new_radio_enable != AltosLib.MISSING)
759                         radio_enable_value.setSelected(new_radio_enable != 0);
760                 radio_enable_label.setVisible(new_radio_enable != AltosLib.MISSING);
761                 radio_enable_value.setVisible(new_radio_enable != AltosLib.MISSING);
762                 set_radio_enable_tool_tip();
763         }
764
765         public int radio_enable() {
766                 if (radio_enable_value.isVisible())
767                         return radio_enable_value.isSelected() ? 1 : 0;
768                 else
769                         return AltosLib.MISSING;
770         }
771
772         public void set_telemetry_rate(int new_rate) {
773                 if (new_rate != AltosLib.MISSING)
774                         rate_value.set_rate(new_rate);
775                 rate_label.setVisible(new_rate != AltosLib.MISSING);
776                 rate_value.setVisible(new_rate != AltosLib.MISSING);
777         }
778
779         public int telemetry_rate() {
780                 return rate_value.rate();
781         }
782
783         public void set_callsign(String new_callsign) {
784                 if (new_callsign != null)
785                         callsign_value.setText(new_callsign);
786                 callsign_value.setVisible(new_callsign != null);
787                 callsign_label.setVisible(new_callsign != null);
788         }
789
790         public String callsign() {
791                 if (callsign_value.isVisible())
792                         return callsign_value.getText();
793                 return null;
794         }
795
796         private int parse_int(String name, String s, boolean split) throws AltosConfigDataException {
797                 String v = s;
798                 if (split)
799                         v = s.split("\\s+")[0];
800                 try {
801                         return Integer.parseInt(v);
802                 } catch (NumberFormatException ne) {
803                         throw new AltosConfigDataException("Invalid %s \"%s\"", name, s);
804                 }
805         }
806
807         int     flight_log_max_limit;
808         int     flight_log_max;
809
810         public String flight_log_max_label(int flight_log_max) {
811                 if (flight_log_max_limit != 0) {
812                         int     nflight = flight_log_max_limit / flight_log_max;
813                         String  plural = nflight > 1 ? "s" : "";
814
815                         return String.format("%d (%d flight%s)", flight_log_max, nflight, plural);
816                 }
817                 return String.format("%d", flight_log_max);
818         }
819
820         public void set_flight_log_max(int new_flight_log_max) {
821                 flight_log_max_value.setSelectedItem(flight_log_max_label(new_flight_log_max));
822                 flight_log_max = new_flight_log_max;
823                 set_flight_log_max_tool_tip();
824         }
825
826         public void set_flight_log_max_enabled(boolean enable) {
827                 flight_log_max_value.setEnabled(enable);
828                 set_flight_log_max_tool_tip();
829         }
830
831         public int flight_log_max() throws AltosConfigDataException {
832                 return parse_int("flight log max", flight_log_max_value.getSelectedItem().toString(), true);
833         }
834
835         public void set_flight_log_max_limit(int new_flight_log_max_limit) {
836                 flight_log_max_limit = new_flight_log_max_limit;
837                 flight_log_max_value.removeAllItems();
838                 for (int i = 8; i >= 1; i--) {
839                         int     size = flight_log_max_limit / i;
840                         flight_log_max_value.addItem(String.format("%d (%d flights)", size, i));
841                 }
842                 if (flight_log_max != 0)
843                         set_flight_log_max(flight_log_max);
844         }
845
846         public void set_ignite_mode(int new_ignite_mode) { }
847         public int ignite_mode() { return AltosLib.MISSING; }
848
849
850         public void set_pad_orientation(int new_pad_orientation) { }
851         public int pad_orientation() { return AltosLib.MISSING; }
852
853         public void set_beep(int new_beep) { }
854
855         public int beep() { return AltosLib.MISSING; }
856
857         String[] tracker_motion_values() {
858                 if (AltosConvert.imperial_units)
859                         return tracker_motion_values_ft;
860                 else
861                         return tracker_motion_values_m;
862         }
863
864         void set_tracker_motion_values() {
865                 String[]        v = tracker_motion_values();
866                 while (tracker_motion_value.getItemCount() > 0)
867                         tracker_motion_value.removeItemAt(0);
868                 for (int i = 0; i < v.length; i++)
869                         tracker_motion_value.addItem(v[i]);
870                 tracker_motion_value.setMaximumRowCount(v.length);
871         }
872
873         String get_tracker_motion_label() {
874                 return String.format("Logging Trigger Motion (%s):", AltosConvert.height.parse_units());
875         }
876
877         void set_tracker_tool_tip() {
878                 if (tracker_motion_value.isVisible())
879                         tracker_motion_value.setToolTipText("How far the device must move before logging");
880                 else
881                         tracker_motion_value.setToolTipText("This device doesn't disable logging when stationary");
882                 if (tracker_interval_value.isVisible())
883                         tracker_interval_value.setToolTipText("How often to report GPS position");
884                 else
885                         tracker_interval_value.setToolTipText("This device can't configure interval");
886         }
887
888         public void set_tracker_motion(int tracker_motion) {
889                 if (tracker_motion != AltosLib.MISSING)
890                         tracker_motion_value.setSelectedItem(AltosConvert.height.say(tracker_motion));
891                 tracker_motion_label.setVisible(tracker_motion != AltosLib.MISSING);
892                 tracker_motion_value.setVisible(tracker_motion != AltosLib.MISSING);
893         }
894
895         public int tracker_motion() throws AltosConfigDataException {
896                 if (tracker_motion_value.isVisible()) {
897                         String str = tracker_motion_value.getSelectedItem().toString();
898                         try {
899                                 return (int) (AltosConvert.height.parse_locale(str) + 0.5);
900                         } catch (ParseException pe) {
901                                 throw new AltosConfigDataException("invalid tracker motion %s", str);
902                         }
903                 }
904                 return AltosLib.MISSING;
905         }
906
907         public void set_tracker_interval(int tracker_interval) {
908                 if (tracker_interval != AltosLib.MISSING)
909                         tracker_interval_value.setSelectedItem(String.format("%d", tracker_interval));
910                 tracker_interval_label.setVisible(tracker_interval != AltosLib.MISSING);
911                 tracker_interval_value.setVisible(tracker_interval != AltosLib.MISSING);
912         }
913
914         public int tracker_interval() throws AltosConfigDataException {
915                 if (tracker_interval_value.isVisible())
916                         return parse_int ("tracker interval", tracker_interval_value.getSelectedItem().toString(), false);
917                 return AltosLib.MISSING;
918         }
919
920         public void set_aprs_interval(int new_aprs_interval) {
921                 if (new_aprs_interval != AltosLib.MISSING)
922                         aprs_interval_value.setSelectedItem(Integer.toString(new_aprs_interval));
923                 aprs_interval_value.setVisible(new_aprs_interval != AltosLib.MISSING);
924                 aprs_interval_label.setVisible(new_aprs_interval != AltosLib.MISSING);
925                 set_aprs_interval_tool_tip();
926         }
927
928         public int aprs_interval() throws AltosConfigDataException {
929                 if (aprs_interval_value.isVisible()) {
930                         String  s = aprs_interval_value.getSelectedItem().toString();
931
932                         return parse_int("aprs interval", s, false);
933                 }
934                 return AltosLib.MISSING;
935         }
936
937         public void set_aprs_ssid(int new_aprs_ssid) {
938                 if (new_aprs_ssid != AltosLib.MISSING)
939                         aprs_ssid_value.setSelectedItem(new_aprs_ssid);
940                 aprs_ssid_value.setVisible(new_aprs_ssid != AltosLib.MISSING);
941                 aprs_ssid_label.setVisible(new_aprs_ssid != AltosLib.MISSING);
942                 set_aprs_ssid_tool_tip();
943         }
944
945         public int aprs_ssid() throws AltosConfigDataException {
946                 if (aprs_ssid_value.isVisible()) {
947                         Integer i = (Integer) aprs_ssid_value.getSelectedItem();
948                         return i;
949                 }
950                 return AltosLib.MISSING;
951         }
952
953         public void set_aprs_format(int new_aprs_format) {
954                 if (new_aprs_format != AltosLib.MISSING)
955                         aprs_format_value.setSelectedIndex(new_aprs_format);
956                 aprs_format_value.setVisible(new_aprs_format != AltosLib.MISSING);
957                 aprs_format_label.setVisible(new_aprs_format != AltosLib.MISSING);
958                 set_aprs_format_tool_tip();
959         }
960
961         public int aprs_format() throws AltosConfigDataException {
962                 if (aprs_format_value.isVisible())
963                         return aprs_format_value.getSelectedIndex();
964                 return AltosLib.MISSING;
965         }
966 }