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