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