java: Refactor AltosFlightDisplay units and font update handling
[fw/altos] / altosui / AltosConfigUI.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.event.*;
24 import org.altusmetrum.altoslib_4.*;
25 import org.altusmetrum.altosuilib_2.*;
26
27 public class AltosConfigUI
28         extends AltosUIDialog
29         implements ActionListener, ItemListener, DocumentListener, AltosConfigValues, AltosUnitsListener
30 {
31
32         Container               pane;
33         JLabel                  product_label;
34         JLabel                  version_label;
35         JLabel                  serial_label;
36         JLabel                  main_deploy_label;
37         JLabel                  apogee_delay_label;
38         JLabel                  apogee_lockout_label;
39         JLabel                  frequency_label;
40         JLabel                  radio_calibration_label;
41         JLabel                  radio_frequency_label;
42         JLabel                  radio_enable_label;
43         JLabel                  aprs_interval_label;
44         JLabel                  flight_log_max_label;
45         JLabel                  ignite_mode_label;
46         JLabel                  pad_orientation_label;
47         JLabel                  callsign_label;
48         JLabel                  beep_label;
49
50         public boolean          dirty;
51
52         JFrame                  owner;
53         JLabel                  product_value;
54         JLabel                  version_value;
55         JLabel                  serial_value;
56         JComboBox<String>       main_deploy_value;
57         JComboBox<String>       apogee_delay_value;
58         JComboBox<String>       apogee_lockout_value;
59         AltosFreqList           radio_frequency_value;
60         JTextField              radio_calibration_value;
61         JRadioButton            radio_enable_value;
62         JComboBox<String>       aprs_interval_value;
63         JComboBox<String>       flight_log_max_value;
64         JComboBox<String>       ignite_mode_value;
65         JComboBox<String>       pad_orientation_value;
66         JTextField              callsign_value;
67         JComboBox<String>       beep_value;
68
69         JButton                 pyro;
70
71         JButton                 save;
72         JButton                 reset;
73         JButton                 reboot;
74         JButton                 close;
75
76         AltosPyro[]             pyros;
77
78         ActionListener          listener;
79
80         static String[]         main_deploy_values_m = {
81                 "100", "150", "200", "250", "300", "350",
82                 "400", "450", "500"
83         };
84
85         static String[]         main_deploy_values_ft = {
86                 "250", "500", "750", "1000", "1250", "1500",
87                 "1750", "2000"
88         };
89
90         static String[]         apogee_delay_values = {
91                 "0", "1", "2", "3", "4", "5"
92         };
93
94         static String[]         apogee_lockout_values = {
95                 "0", "5", "10", "15", "20"
96         };
97
98         static String[]         flight_log_max_values = {
99                 "64", "128", "192", "256", "320",
100                 "384", "448", "512", "576", "640",
101                 "704", "768", "832", "896", "960",
102         };
103
104         static String[]         ignite_mode_values = {
105                 "Dual Deploy",
106                 "Redundant Apogee",
107                 "Redundant Main",
108         };
109
110         static String[]         aprs_interval_values = {
111                 "Disabled",
112                 "2",
113                 "5",
114                 "10"
115         };
116
117         static String[]         beep_values = {
118                 "3750",
119                 "4000",
120                 "4250",
121         };
122
123         static String[]         pad_orientation_values = {
124                 "Antenna Up",
125                 "Antenna Down",
126         };
127
128         /* A window listener to catch closing events and tell the config code */
129         class ConfigListener extends WindowAdapter {
130                 AltosConfigUI   ui;
131
132                 public ConfigListener(AltosConfigUI this_ui) {
133                         ui = this_ui;
134                 }
135
136                 public void windowClosing(WindowEvent e) {
137                         ui.actionPerformed(new ActionEvent(e.getSource(),
138                                                            ActionEvent.ACTION_PERFORMED,
139                                                            "Close"));
140                 }
141         }
142
143         boolean is_telemini_v1() {
144                 String  product = product_value.getText();
145                 return product != null && product.startsWith("TeleMini-v1");
146         }
147
148         boolean is_telemini() {
149                 String  product = product_value.getText();
150                 return product != null && product.startsWith("TeleMini");
151         }
152
153         boolean is_easymini() {
154                 String  product = product_value.getText();
155                 return product != null && product.startsWith("EasyMini");
156         }
157
158         boolean is_telemetrum() {
159                 String  product = product_value.getText();
160                 return product != null && product.startsWith("TeleMetrum");
161         }
162
163         void set_radio_calibration_tool_tip() {
164                 if (radio_calibration_value.isEnabled())
165                         radio_calibration_value.setToolTipText("Tune radio output to match desired frequency");
166                 else
167                         radio_calibration_value.setToolTipText("Cannot tune radio while connected over packet mode");
168         }
169
170         void set_radio_enable_tool_tip() {
171                 if (radio_enable_value.isEnabled())
172                         radio_enable_value.setToolTipText("Enable/Disable telemetry and RDF transmissions");
173                 else
174                         radio_enable_value.setToolTipText("Firmware version does not support disabling radio");
175         }
176
177         void set_aprs_interval_tool_tip() {
178                 if (aprs_interval_value.isEnabled())
179                         aprs_interval_value.setToolTipText("Enable APRS and set the interval between APRS reports");
180                 else
181                         aprs_interval_value.setToolTipText("Hardware doesn't support APRS");
182         }
183
184         void set_flight_log_max_tool_tip() {
185                 if (flight_log_max_value.isEnabled())
186                         flight_log_max_value.setToolTipText("Size reserved for each flight log (in kB)");
187                 else {
188                         if (is_telemini_v1())
189                                 flight_log_max_value.setToolTipText("TeleMini-v1 stores only one flight");
190                         else
191                                 flight_log_max_value.setToolTipText("Cannot set max value with flight logs in memory");
192                 }
193         }
194
195         void set_ignite_mode_tool_tip() {
196                 if (ignite_mode_value.isEnabled())
197                         ignite_mode_value.setToolTipText("Select when igniters will be fired");
198                 else
199                         ignite_mode_value.setToolTipText("Older firmware could not select ignite mode");
200         }
201
202         void set_pad_orientation_tool_tip() {
203                 if (pad_orientation_value.isEnabled())
204                         pad_orientation_value.setToolTipText("How will the computer be mounted in the airframe");
205                 else {
206                         if (is_telemetrum())
207                                 pad_orientation_value.setToolTipText("Older TeleMetrum firmware must fly antenna forward");
208                         else if (is_telemini() || is_easymini())
209                                 pad_orientation_value.setToolTipText("TeleMini and EasyMini don't care how they are mounted");
210                         else
211                                 pad_orientation_value.setToolTipText("Can't select orientation");
212                 }
213         }
214
215         void set_beep_tool_tip() {
216                 if (beep_value.isEnabled())
217                         beep_value.setToolTipText("What frequency the beeper will sound at");
218                 else
219                         beep_value.setToolTipText("Older firmware could not select beeper frequency");
220         }
221
222         /* Build the UI using a grid bag */
223         public AltosConfigUI(JFrame in_owner, boolean remote) {
224                 super (in_owner, "Configure Flight Computer", false);
225
226                 owner = in_owner;
227                 GridBagConstraints c;
228                 int row = 0;
229
230                 Insets il = new Insets(4,4,4,4);
231                 Insets ir = new Insets(4,4,4,4);
232
233                 pane = getContentPane();
234                 pane.setLayout(new GridBagLayout());
235
236                 /* Product */
237                 c = new GridBagConstraints();
238                 c.gridx = 0; c.gridy = row;
239                 c.gridwidth = 4;
240                 c.fill = GridBagConstraints.NONE;
241                 c.anchor = GridBagConstraints.LINE_START;
242                 c.insets = il;
243                 product_label = new JLabel("Product:");
244                 pane.add(product_label, c);
245
246                 c = new GridBagConstraints();
247                 c.gridx = 4; c.gridy = row;
248                 c.gridwidth = 4;
249                 c.fill = GridBagConstraints.HORIZONTAL;
250                 c.weightx = 1;
251                 c.anchor = GridBagConstraints.LINE_START;
252                 c.insets = ir;
253                 product_value = new JLabel("");
254                 pane.add(product_value, c);
255                 row++;
256
257                 /* Version */
258                 c = new GridBagConstraints();
259                 c.gridx = 0; c.gridy = row;
260                 c.gridwidth = 4;
261                 c.fill = GridBagConstraints.NONE;
262                 c.anchor = GridBagConstraints.LINE_START;
263                 c.insets = il;
264                 c.ipady = 5;
265                 version_label = new JLabel("Software version:");
266                 pane.add(version_label, c);
267
268                 c = new GridBagConstraints();
269                 c.gridx = 4; c.gridy = row;
270                 c.gridwidth = 4;
271                 c.fill = GridBagConstraints.HORIZONTAL;
272                 c.weightx = 1;
273                 c.anchor = GridBagConstraints.LINE_START;
274                 c.insets = ir;
275                 c.ipady = 5;
276                 version_value = new JLabel("");
277                 pane.add(version_value, c);
278                 row++;
279
280                 /* Serial */
281                 c = new GridBagConstraints();
282                 c.gridx = 0; c.gridy = row;
283                 c.gridwidth = 4;
284                 c.fill = GridBagConstraints.NONE;
285                 c.anchor = GridBagConstraints.LINE_START;
286                 c.insets = il;
287                 c.ipady = 5;
288                 serial_label = new JLabel("Serial:");
289                 pane.add(serial_label, c);
290
291                 c = new GridBagConstraints();
292                 c.gridx = 4; c.gridy = row;
293                 c.gridwidth = 4;
294                 c.fill = GridBagConstraints.HORIZONTAL;
295                 c.weightx = 1;
296                 c.anchor = GridBagConstraints.LINE_START;
297                 c.insets = ir;
298                 c.ipady = 5;
299                 serial_value = new JLabel("");
300                 pane.add(serial_value, c);
301                 row++;
302
303                 /* Main deploy */
304                 c = new GridBagConstraints();
305                 c.gridx = 0; c.gridy = row;
306                 c.gridwidth = 4;
307                 c.fill = GridBagConstraints.NONE;
308                 c.anchor = GridBagConstraints.LINE_START;
309                 c.insets = il;
310                 c.ipady = 5;
311                 main_deploy_label = new JLabel(get_main_deploy_label());
312                 pane.add(main_deploy_label, c);
313
314                 c = new GridBagConstraints();
315                 c.gridx = 4; c.gridy = row;
316                 c.gridwidth = 4;
317                 c.fill = GridBagConstraints.HORIZONTAL;
318                 c.weightx = 1;
319                 c.anchor = GridBagConstraints.LINE_START;
320                 c.insets = ir;
321                 c.ipady = 5;
322                 main_deploy_value = new JComboBox<String>(main_deploy_values());
323                 main_deploy_value.setEditable(true);
324                 main_deploy_value.addItemListener(this);
325                 pane.add(main_deploy_value, c);
326                 main_deploy_value.setToolTipText("Height above pad altitude to fire main charge");
327                 row++;
328
329                 /* Apogee delay */
330                 c = new GridBagConstraints();
331                 c.gridx = 0; c.gridy = row;
332                 c.gridwidth = 4;
333                 c.fill = GridBagConstraints.NONE;
334                 c.anchor = GridBagConstraints.LINE_START;
335                 c.insets = il;
336                 c.ipady = 5;
337                 apogee_delay_label = new JLabel("Apogee Delay(s):");
338                 pane.add(apogee_delay_label, c);
339
340                 c = new GridBagConstraints();
341                 c.gridx = 4; c.gridy = row;
342                 c.gridwidth = 4;
343                 c.fill = GridBagConstraints.HORIZONTAL;
344                 c.weightx = 1;
345                 c.anchor = GridBagConstraints.LINE_START;
346                 c.insets = ir;
347                 c.ipady = 5;
348                 apogee_delay_value = new JComboBox<String>(apogee_delay_values);
349                 apogee_delay_value.setEditable(true);
350                 apogee_delay_value.addItemListener(this);
351                 pane.add(apogee_delay_value, c);
352                 apogee_delay_value.setToolTipText("Delay after apogee before charge fires");
353                 row++;
354
355                 /* Apogee lockout */
356                 c = new GridBagConstraints();
357                 c.gridx = 0; c.gridy = row;
358                 c.gridwidth = 4;
359                 c.fill = GridBagConstraints.NONE;
360                 c.anchor = GridBagConstraints.LINE_START;
361                 c.insets = il;
362                 c.ipady = 5;
363                 apogee_lockout_label = new JLabel("Apogee Lockout(s):");
364                 pane.add(apogee_lockout_label, c);
365
366                 c = new GridBagConstraints();
367                 c.gridx = 4; c.gridy = row;
368                 c.gridwidth = 4;
369                 c.fill = GridBagConstraints.HORIZONTAL;
370                 c.weightx = 1;
371                 c.anchor = GridBagConstraints.LINE_START;
372                 c.insets = ir;
373                 c.ipady = 5;
374                 apogee_lockout_value = new JComboBox<String>(apogee_lockout_values);
375                 apogee_lockout_value.setEditable(true);
376                 apogee_lockout_value.addItemListener(this);
377                 pane.add(apogee_lockout_value, c);
378                 apogee_lockout_value.setToolTipText("Time after boost while apogee detection is locked out");
379                 row++;
380
381                 /* Frequency */
382                 c = new GridBagConstraints();
383                 c.gridx = 0; c.gridy = row;
384                 c.gridwidth = 4;
385                 c.fill = GridBagConstraints.NONE;
386                 c.anchor = GridBagConstraints.LINE_START;
387                 c.insets = il;
388                 c.ipady = 5;
389                 radio_frequency_label = new JLabel("Frequency:");
390                 pane.add(radio_frequency_label, c);
391
392                 c = new GridBagConstraints();
393                 c.gridx = 4; c.gridy = row;
394                 c.gridwidth = 4;
395                 c.fill = GridBagConstraints.HORIZONTAL;
396                 c.weightx = 1;
397                 c.anchor = GridBagConstraints.LINE_START;
398                 c.insets = ir;
399                 c.ipady = 5;
400                 radio_frequency_value = new AltosFreqList();
401                 radio_frequency_value.addItemListener(this);
402                 pane.add(radio_frequency_value, c);
403                 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
404                 row++;
405
406                 /* Radio Calibration */
407                 c = new GridBagConstraints();
408                 c.gridx = 0; c.gridy = row;
409                 c.gridwidth = 4;
410                 c.fill = GridBagConstraints.NONE;
411                 c.anchor = GridBagConstraints.LINE_START;
412                 c.insets = il;
413                 c.ipady = 5;
414                 radio_calibration_label = new JLabel("RF Calibration:");
415                 pane.add(radio_calibration_label, c);
416
417                 c = new GridBagConstraints();
418                 c.gridx = 4; c.gridy = row;
419                 c.gridwidth = 4;
420                 c.fill = GridBagConstraints.HORIZONTAL;
421                 c.weightx = 1;
422                 c.anchor = GridBagConstraints.LINE_START;
423                 c.insets = ir;
424                 c.ipady = 5;
425                 radio_calibration_value = new JTextField(String.format("%d", 1186611));
426                 radio_calibration_value.getDocument().addDocumentListener(this);
427                 if (remote)
428                         radio_calibration_value.setEnabled(false);
429                 pane.add(radio_calibration_value, c);
430                 set_radio_calibration_tool_tip();
431                 row++;
432
433                 /* Radio Enable */
434                 c = new GridBagConstraints();
435                 c.gridx = 0; c.gridy = row;
436                 c.gridwidth = 4;
437                 c.fill = GridBagConstraints.NONE;
438                 c.anchor = GridBagConstraints.LINE_START;
439                 c.insets = il;
440                 c.ipady = 5;
441                 radio_enable_label = new JLabel("Telemetry/RDF/APRS Enable:");
442                 pane.add(radio_enable_label, c);
443
444                 c = new GridBagConstraints();
445                 c.gridx = 4; c.gridy = row;
446                 c.gridwidth = 4;
447                 c.fill = GridBagConstraints.HORIZONTAL;
448                 c.weightx = 1;
449                 c.anchor = GridBagConstraints.LINE_START;
450                 c.insets = ir;
451                 c.ipady = 5;
452                 radio_enable_value = new JRadioButton("Enabled");
453                 radio_enable_value.addItemListener(this);
454                 pane.add(radio_enable_value, c);
455                 set_radio_enable_tool_tip();
456                 row++;
457
458                 /* APRS interval */
459                 c = new GridBagConstraints();
460                 c.gridx = 0; c.gridy = row;
461                 c.gridwidth = 4;
462                 c.fill = GridBagConstraints.NONE;
463                 c.anchor = GridBagConstraints.LINE_START;
464                 c.insets = il;
465                 c.ipady = 5;
466                 aprs_interval_label = new JLabel("APRS Interval(s):");
467                 pane.add(aprs_interval_label, c);
468
469                 c = new GridBagConstraints();
470                 c.gridx = 4; c.gridy = row;
471                 c.gridwidth = 4;
472                 c.fill = GridBagConstraints.HORIZONTAL;
473                 c.weightx = 1;
474                 c.anchor = GridBagConstraints.LINE_START;
475                 c.insets = ir;
476                 c.ipady = 5;
477                 aprs_interval_value = new JComboBox<String>(aprs_interval_values);
478                 aprs_interval_value.setEditable(true);
479                 aprs_interval_value.addItemListener(this);
480                 pane.add(aprs_interval_value, c);
481                 set_aprs_interval_tool_tip();
482                 row++;
483
484                 /* Callsign */
485                 c = new GridBagConstraints();
486                 c.gridx = 0; c.gridy = row;
487                 c.gridwidth = 4;
488                 c.fill = GridBagConstraints.NONE;
489                 c.anchor = GridBagConstraints.LINE_START;
490                 c.insets = il;
491                 c.ipady = 5;
492                 callsign_label = new JLabel("Callsign:");
493                 pane.add(callsign_label, c);
494
495                 c = new GridBagConstraints();
496                 c.gridx = 4; c.gridy = row;
497                 c.gridwidth = 4;
498                 c.fill = GridBagConstraints.HORIZONTAL;
499                 c.weightx = 1;
500                 c.anchor = GridBagConstraints.LINE_START;
501                 c.insets = ir;
502                 c.ipady = 5;
503                 callsign_value = new JTextField(AltosUIPreferences.callsign());
504                 callsign_value.getDocument().addDocumentListener(this);
505                 pane.add(callsign_value, c);
506                 callsign_value.setToolTipText("Callsign reported in telemetry data");
507                 row++;
508
509                 /* Flight log max */
510                 c = new GridBagConstraints();
511                 c.gridx = 0; c.gridy = row;
512                 c.gridwidth = 4;
513                 c.fill = GridBagConstraints.NONE;
514                 c.anchor = GridBagConstraints.LINE_START;
515                 c.insets = il;
516                 c.ipady = 5;
517                 flight_log_max_label = new JLabel("Maximum Flight Log Size:");
518                 pane.add(flight_log_max_label, c);
519
520                 c = new GridBagConstraints();
521                 c.gridx = 4; c.gridy = row;
522                 c.gridwidth = 4;
523                 c.fill = GridBagConstraints.HORIZONTAL;
524                 c.weightx = 1;
525                 c.anchor = GridBagConstraints.LINE_START;
526                 c.insets = ir;
527                 c.ipady = 5;
528                 flight_log_max_value = new JComboBox<String>(flight_log_max_values);
529                 flight_log_max_value.setEditable(true);
530                 flight_log_max_value.addItemListener(this);
531                 pane.add(flight_log_max_value, c);
532                 set_flight_log_max_tool_tip();
533                 row++;
534
535                 /* Ignite mode */
536                 c = new GridBagConstraints();
537                 c.gridx = 0; c.gridy = row;
538                 c.gridwidth = 4;
539                 c.fill = GridBagConstraints.NONE;
540                 c.anchor = GridBagConstraints.LINE_START;
541                 c.insets = il;
542                 c.ipady = 5;
543                 ignite_mode_label = new JLabel("Igniter Firing Mode:");
544                 pane.add(ignite_mode_label, c);
545
546                 c = new GridBagConstraints();
547                 c.gridx = 4; c.gridy = row;
548                 c.gridwidth = 4;
549                 c.fill = GridBagConstraints.HORIZONTAL;
550                 c.weightx = 1;
551                 c.anchor = GridBagConstraints.LINE_START;
552                 c.insets = ir;
553                 c.ipady = 5;
554                 ignite_mode_value = new JComboBox<String>(ignite_mode_values);
555                 ignite_mode_value.setEditable(false);
556                 ignite_mode_value.addItemListener(this);
557                 pane.add(ignite_mode_value, c);
558                 set_ignite_mode_tool_tip();
559                 row++;
560
561                 /* Pad orientation */
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                 pad_orientation_label = new JLabel("Pad Orientation:");
570                 pane.add(pad_orientation_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                 pad_orientation_value = new JComboBox<String>(pad_orientation_values);
581                 pad_orientation_value.setEditable(false);
582                 pad_orientation_value.addItemListener(this);
583                 pane.add(pad_orientation_value, c);
584                 set_pad_orientation_tool_tip();
585                 row++;
586
587                 /* Beeper */
588                 c = new GridBagConstraints();
589                 c.gridx = 0; c.gridy = row;
590                 c.gridwidth = 4;
591                 c.fill = GridBagConstraints.NONE;
592                 c.anchor = GridBagConstraints.LINE_START;
593                 c.insets = il;
594                 c.ipady = 5;
595                 beep_label = new JLabel("Beeper Frequency:");
596                 pane.add(beep_label, c);
597
598                 c = new GridBagConstraints();
599                 c.gridx = 4; c.gridy = row;
600                 c.gridwidth = 4;
601                 c.fill = GridBagConstraints.HORIZONTAL;
602                 c.weightx = 1;
603                 c.anchor = GridBagConstraints.LINE_START;
604                 c.insets = ir;
605                 c.ipady = 5;
606                 beep_value = new JComboBox<String>(beep_values);
607                 beep_value.setEditable(true);
608                 beep_value.addItemListener(this);
609                 pane.add(beep_value, c);
610                 set_beep_tool_tip();
611                 row++;
612
613                 /* Pyro channels */
614                 c = new GridBagConstraints();
615                 c.gridx = 4; c.gridy = row;
616                 c.gridwidth = 4;
617                 c.fill = GridBagConstraints.HORIZONTAL;
618                 c.anchor = GridBagConstraints.LINE_START;
619                 c.insets = il;
620                 c.ipady = 5;
621                 pyro = new JButton("Configure Pyro Channels");
622                 pane.add(pyro, c);
623                 pyro.addActionListener(this);
624                 pyro.setActionCommand("Pyro");
625                 row++;
626
627                 /* Buttons */
628                 c = new GridBagConstraints();
629                 c.gridx = 0; c.gridy = row;
630                 c.gridwidth = 2;
631                 c.fill = GridBagConstraints.NONE;
632                 c.anchor = GridBagConstraints.LINE_START;
633                 c.insets = il;
634                 save = new JButton("Save");
635                 pane.add(save, c);
636                 save.addActionListener(this);
637                 save.setActionCommand("Save");
638
639                 c = new GridBagConstraints();
640                 c.gridx = 2; c.gridy = row;
641                 c.gridwidth = 2;
642                 c.fill = GridBagConstraints.NONE;
643                 c.anchor = GridBagConstraints.CENTER;
644                 c.insets = il;
645                 reset = new JButton("Reset");
646                 pane.add(reset, c);
647                 reset.addActionListener(this);
648                 reset.setActionCommand("Reset");
649
650                 c = new GridBagConstraints();
651                 c.gridx = 4; c.gridy = row;
652                 c.gridwidth = 2;
653                 c.fill = GridBagConstraints.NONE;
654                 c.anchor = GridBagConstraints.CENTER;
655                 c.insets = il;
656                 reboot = new JButton("Reboot");
657                 pane.add(reboot, c);
658                 reboot.addActionListener(this);
659                 reboot.setActionCommand("Reboot");
660
661                 c = new GridBagConstraints();
662                 c.gridx = 6; c.gridy = row;
663                 c.gridwidth = 2;
664                 c.fill = GridBagConstraints.NONE;
665                 c.anchor = GridBagConstraints.LINE_END;
666                 c.insets = il;
667                 close = new JButton("Close");
668                 pane.add(close, c);
669                 close.addActionListener(this);
670                 close.setActionCommand("Close");
671
672                 addWindowListener(new ConfigListener(this));
673                 AltosPreferences.register_units_listener(this);
674         }
675
676         /* Once the initial values are set, the config code will show the dialog */
677         public void make_visible() {
678                 pack();
679                 setLocationRelativeTo(owner);
680                 setVisible(true);
681         }
682
683         /* If any values have been changed, confirm before closing */
684         public boolean check_dirty(String operation) {
685                 if (dirty) {
686                         Object[] options = { String.format("%s anyway", operation), "Keep editing" };
687                         int i;
688                         i = JOptionPane.showOptionDialog(this,
689                                                          String.format("Configuration modified. %s anyway?", operation),
690                                                          "Configuration Modified",
691                                                          JOptionPane.DEFAULT_OPTION,
692                                                          JOptionPane.WARNING_MESSAGE,
693                                                          null, options, options[1]);
694                         if (i != 0)
695                                 return false;
696                 }
697                 return true;
698         }
699
700         void set_dirty() {
701                 dirty = true;
702                 save.setEnabled(true);
703         }
704
705         public void set_clean() {
706                 dirty = false;
707                 save.setEnabled(false);
708         }
709
710         AltosConfigPyroUI       pyro_ui;
711
712         public void dispose() {
713                 if (pyro_ui != null)
714                         pyro_ui.dispose();
715                 AltosPreferences.unregister_units_listener(this);
716                 super.dispose();
717         }
718
719         /* Listen for events from our buttons */
720         public void actionPerformed(ActionEvent e) {
721                 String  cmd = e.getActionCommand();
722
723                 if (cmd.equals("Pyro")) {
724                         if (pyro_ui == null && pyros != null)
725                                 pyro_ui = new AltosConfigPyroUI(this, pyros);
726                         if (pyro_ui != null)
727                                 pyro_ui.make_visible();
728                         return;
729                 }
730
731                 if (cmd.equals("Close") || cmd.equals("Reboot"))
732                         if (!check_dirty(cmd))
733                                 return;
734                 listener.actionPerformed(e);
735                 if (cmd.equals("Close") || cmd.equals("Reboot")) {
736                         setVisible(false);
737                         dispose();
738                 }
739                 set_clean();
740         }
741
742         /* ItemListener interface method */
743         public void itemStateChanged(ItemEvent e) {
744                 set_dirty();
745         }
746
747         /* DocumentListener interface methods */
748         public void changedUpdate(DocumentEvent e) {
749                 set_dirty();
750         }
751
752         public void insertUpdate(DocumentEvent e) {
753                 set_dirty();
754         }
755
756         public void removeUpdate(DocumentEvent e) {
757                 set_dirty();
758         }
759
760         /* Let the config code hook on a listener */
761         public void addActionListener(ActionListener l) {
762                 listener = l;
763         }
764
765         /* set and get all of the dialog values */
766         public void set_product(String product) {
767                 radio_frequency_value.set_product(product);
768                 product_value.setText(product);
769                 set_pad_orientation_tool_tip();
770                 set_flight_log_max_tool_tip();
771         }
772
773         public void set_version(String version) {
774                 version_value.setText(version);
775         }
776
777         public void set_serial(int serial) {
778                 radio_frequency_value.set_serial(serial);
779                 serial_value.setText(String.format("%d", serial));
780         }
781
782         public void set_main_deploy(int new_main_deploy) {
783                 main_deploy_value.setSelectedItem(AltosConvert.height.say(new_main_deploy));
784                 main_deploy_value.setEnabled(new_main_deploy >= 0);
785         }
786
787         public int main_deploy() {
788                 return (int) (AltosConvert.height.parse(main_deploy_value.getSelectedItem().toString()) + 0.5);
789         }
790
791         String get_main_deploy_label() {
792                 return String.format("Main Deploy Altitude(%s):", AltosConvert.height.show_units());
793         }
794
795         String[] main_deploy_values() {
796                 if (AltosConvert.imperial_units)
797                         return main_deploy_values_ft;
798                 else
799                         return main_deploy_values_m;
800         }
801
802         void set_main_deploy_values() {
803                 String[]        v = main_deploy_values();
804                 while (main_deploy_value.getItemCount() > 0)
805                         main_deploy_value.removeItemAt(0);
806                 for (int i = 0; i < v.length; i++)
807                         main_deploy_value.addItem(v[i]);
808                 main_deploy_value.setMaximumRowCount(v.length);
809         }
810
811         public void units_changed(boolean imperial_units) {
812                 String v = main_deploy_value.getSelectedItem().toString();
813                 main_deploy_label.setText(get_main_deploy_label());
814                 set_main_deploy_values();
815                 int m = (int) (AltosConvert.height.parse(v, !imperial_units) + 0.5);
816                 set_main_deploy(m);
817         }
818
819         public void set_apogee_delay(int new_apogee_delay) {
820                 apogee_delay_value.setSelectedItem(Integer.toString(new_apogee_delay));
821                 apogee_delay_value.setEnabled(new_apogee_delay >= 0);
822         }
823
824         public int apogee_delay() {
825                 return Integer.parseInt(apogee_delay_value.getSelectedItem().toString());
826         }
827
828         public void set_apogee_lockout(int new_apogee_lockout) {
829                 apogee_lockout_value.setSelectedItem(Integer.toString(new_apogee_lockout));
830                 apogee_lockout_value.setEnabled(new_apogee_lockout >= 0);
831         }
832
833         public int apogee_lockout() {
834                 return Integer.parseInt(apogee_lockout_value.getSelectedItem().toString());
835         }
836
837         public void set_radio_frequency(double new_radio_frequency) {
838                 radio_frequency_value.set_frequency(new_radio_frequency);
839         }
840
841         public double radio_frequency() {
842                 return radio_frequency_value.frequency();
843         }
844
845         public void set_radio_calibration(int new_radio_calibration) {
846                 radio_calibration_value.setVisible(new_radio_calibration >= 0);
847                 if (new_radio_calibration < 0)
848                         radio_calibration_value.setText("Disabled");
849                 else
850                         radio_calibration_value.setText(String.format("%d", new_radio_calibration));
851         }
852
853         public int radio_calibration() {
854                 return Integer.parseInt(radio_calibration_value.getText());
855         }
856
857         public void set_radio_enable(int new_radio_enable) {
858                 if (new_radio_enable >= 0) {
859                         radio_enable_value.setSelected(new_radio_enable > 0);
860                         radio_enable_value.setEnabled(true);
861                 } else {
862                         radio_enable_value.setSelected(true);
863                         radio_enable_value.setVisible(radio_frequency() > 0);
864                         radio_enable_value.setEnabled(false);
865                 }
866                 set_radio_enable_tool_tip();
867         }
868
869         public int radio_enable() {
870                 if (radio_enable_value.isEnabled())
871                         return radio_enable_value.isSelected() ? 1 : 0;
872                 else
873                         return -1;
874         }
875
876         public void set_callsign(String new_callsign) {
877                 callsign_value.setVisible(new_callsign != null);
878                 callsign_value.setText(new_callsign);
879         }
880
881         public String callsign() {
882                 return callsign_value.getText();
883         }
884
885         public void set_flight_log_max(int new_flight_log_max) {
886                 flight_log_max_value.setSelectedItem(Integer.toString(new_flight_log_max));
887                 set_flight_log_max_tool_tip();
888         }
889
890         public void set_flight_log_max_enabled(boolean enable) {
891                 flight_log_max_value.setEnabled(enable);
892                 set_flight_log_max_tool_tip();
893         }
894
895         public int flight_log_max() {
896                 return Integer.parseInt(flight_log_max_value.getSelectedItem().toString());
897         }
898
899         public void set_flight_log_max_limit(int flight_log_max_limit) {
900                 //boolean       any_added = false;
901                 flight_log_max_value.removeAllItems();
902                 for (int i = 0; i < flight_log_max_values.length; i++) {
903                         if (Integer.parseInt(flight_log_max_values[i]) < flight_log_max_limit){
904                                 flight_log_max_value.addItem(flight_log_max_values[i]);
905                                 //any_added = true;
906                         }
907                 }
908                 flight_log_max_value.addItem(String.format("%d", flight_log_max_limit));
909         }
910
911         public void set_ignite_mode(int new_ignite_mode) {
912                 if (new_ignite_mode >= ignite_mode_values.length)
913                         new_ignite_mode = 0;
914                 if (new_ignite_mode < 0) {
915                         ignite_mode_value.setEnabled(false);
916                         new_ignite_mode = 0;
917                 } else {
918                         ignite_mode_value.setEnabled(true);
919                 }
920                 ignite_mode_value.setSelectedIndex(new_ignite_mode);
921                 set_ignite_mode_tool_tip();
922         }
923
924         public int ignite_mode() {
925                 if (ignite_mode_value.isEnabled())
926                         return ignite_mode_value.getSelectedIndex();
927                 else
928                         return -1;
929         }
930
931
932         public void set_pad_orientation(int new_pad_orientation) {
933                 if (new_pad_orientation >= pad_orientation_values.length)
934                         new_pad_orientation = 0;
935                 if (new_pad_orientation < 0) {
936                         pad_orientation_value.setVisible(false);
937                         new_pad_orientation = 0;
938                 } else {
939                         pad_orientation_value.setVisible(true);
940                 }
941                 pad_orientation_value.setSelectedIndex(new_pad_orientation);
942                 set_pad_orientation_tool_tip();
943         }
944
945         public int pad_orientation() {
946                 if (pad_orientation_value.isEnabled())
947                         return pad_orientation_value.getSelectedIndex();
948                 else
949                         return -1;
950         }
951
952         public void set_beep(int new_beep) {
953                 int new_freq = (int) Math.floor (AltosConvert.beep_value_to_freq(new_beep) + 0.5);
954                 for (int i = 0; i < beep_values.length; i++)
955                         if (new_beep == AltosConvert.beep_freq_to_value(Integer.parseInt(beep_values[i]))) {
956                                 beep_value.setSelectedIndex(i);
957                                 set_beep_tool_tip();
958                                 return;
959                         }
960                 beep_value.setSelectedItem(String.format("%d", new_freq));
961                 beep_value.setEnabled(new_beep >= 0);
962                 set_beep_tool_tip();
963         }
964
965         public int beep() {
966                 if (beep_value.isEnabled())
967                         return AltosConvert.beep_freq_to_value(Integer.parseInt(beep_value.getSelectedItem().toString()));
968                 else
969                         return -1;
970         }
971
972         public void set_pyros(AltosPyro[] new_pyros) {
973                 pyros = new_pyros;
974                 pyro.setVisible(pyros != null);
975                 if (pyros != null && pyro_ui != null)
976                         pyro_ui.set_pyros(pyros);
977         }
978
979         public AltosPyro[] pyros() {
980                 if (pyro_ui != null)
981                         pyros = pyro_ui.get_pyros();
982                 return pyros;
983         }
984
985         public void set_aprs_interval(int new_aprs_interval) {
986                 String  s;
987
988                 if (new_aprs_interval <= 0)
989                         s = "Disabled";
990                 else
991                         s = Integer.toString(new_aprs_interval);
992                 aprs_interval_value.setSelectedItem(s);
993                 aprs_interval_value.setVisible(new_aprs_interval >= 0);
994                 set_aprs_interval_tool_tip();
995         }
996
997         public int aprs_interval() {
998                 String  s = aprs_interval_value.getSelectedItem().toString();
999
1000                 if (s.equals("Disabled"))
1001                         return 0;
1002                 return Integer.parseInt(s);
1003         }
1004 }