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