f835ee2e86d4df46a4055894031202374ccc6acf
[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.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import javax.swing.event.*;
26 import java.io.*;
27 import java.util.*;
28 import java.text.*;
29 import java.util.prefs.*;
30 import java.util.concurrent.LinkedBlockingQueue;
31
32 import libaltosJNI.*;
33
34 public class AltosConfigUI
35         extends JDialog
36         implements ActionListener, ItemListener, DocumentListener
37 {
38
39         Container       pane;
40         Box             box;
41         JLabel          product_label;
42         JLabel          version_label;
43         JLabel          serial_label;
44         JLabel          main_deploy_label;
45         JLabel          apogee_delay_label;
46         JLabel          radio_channel_label;
47         JLabel          radio_calibration_label;
48         JLabel          flight_log_max_label;
49         JLabel          callsign_label;
50
51         public boolean          dirty;
52
53         JFrame          owner;
54         JLabel          product_value;
55         JLabel          version_value;
56         JLabel          serial_value;
57         JComboBox       main_deploy_value;
58         JComboBox       apogee_delay_value;
59         JComboBox       radio_channel_value;
60         JTextField      radio_calibration_value;
61         JComboBox       flight_log_max_value;
62         JTextField      callsign_value;
63
64         JButton         save;
65         JButton         reset;
66         JButton         reboot;
67         JButton         close;
68
69         ActionListener  listener;
70
71         static String[] main_deploy_values = {
72                 "100", "150", "200", "250", "300", "350",
73                 "400", "450", "500"
74         };
75
76         static String[] apogee_delay_values = {
77                 "0", "1", "2", "3", "4", "5"
78         };
79
80         static String[] flight_log_max_values = {
81                 "64", "128", "192", "256", "320",
82                 "384", "448", "512", "576", "640",
83                 "704", "768", "832", "896", "960",
84         };
85
86         static String[] radio_channel_values = new String[10];
87                 {
88                         for (int i = 0; i <= 9; i++)
89                                 radio_channel_values[i] = String.format("Channel %1d (%7.3fMHz)",
90                                                                         i, 434.550 + i * 0.1);
91                 }
92
93         /* A window listener to catch closing events and tell the config code */
94         class ConfigListener extends WindowAdapter {
95                 AltosConfigUI   ui;
96
97                 public ConfigListener(AltosConfigUI this_ui) {
98                         ui = this_ui;
99                 }
100
101                 public void windowClosing(WindowEvent e) {
102                         ui.actionPerformed(new ActionEvent(e.getSource(),
103                                                            ActionEvent.ACTION_PERFORMED,
104                                                            "Close"));
105                 }
106         }
107
108         /* Build the UI using a grid bag */
109         public AltosConfigUI(JFrame in_owner, boolean remote) {
110                 super (in_owner, "Configure TeleMetrum", false);
111
112                 owner = in_owner;
113                 GridBagConstraints c;
114
115                 Insets il = new Insets(4,4,4,4);
116                 Insets ir = new Insets(4,4,4,4);
117
118                 pane = getContentPane();
119                 pane.setLayout(new GridBagLayout());
120
121                 /* Product */
122                 c = new GridBagConstraints();
123                 c.gridx = 0; c.gridy = 0;
124                 c.gridwidth = 4;
125                 c.fill = GridBagConstraints.NONE;
126                 c.anchor = GridBagConstraints.LINE_START;
127                 c.insets = il;
128                 product_label = new JLabel("Product:");
129                 pane.add(product_label, c);
130
131                 c = new GridBagConstraints();
132                 c.gridx = 4; c.gridy = 0;
133                 c.gridwidth = 4;
134                 c.fill = GridBagConstraints.HORIZONTAL;
135                 c.weightx = 1;
136                 c.anchor = GridBagConstraints.LINE_START;
137                 c.insets = ir;
138                 product_value = new JLabel("");
139                 pane.add(product_value, c);
140
141                 /* Version */
142                 c = new GridBagConstraints();
143                 c.gridx = 0; c.gridy = 1;
144                 c.gridwidth = 4;
145                 c.fill = GridBagConstraints.NONE;
146                 c.anchor = GridBagConstraints.LINE_START;
147                 c.insets = il;
148                 c.ipady = 5;
149                 version_label = new JLabel("Software version:");
150                 pane.add(version_label, c);
151
152                 c = new GridBagConstraints();
153                 c.gridx = 4; c.gridy = 1;
154                 c.gridwidth = 4;
155                 c.fill = GridBagConstraints.HORIZONTAL;
156                 c.weightx = 1;
157                 c.anchor = GridBagConstraints.LINE_START;
158                 c.insets = ir;
159                 c.ipady = 5;
160                 version_value = new JLabel("");
161                 pane.add(version_value, c);
162
163                 /* Serial */
164                 c = new GridBagConstraints();
165                 c.gridx = 0; c.gridy = 2;
166                 c.gridwidth = 4;
167                 c.fill = GridBagConstraints.NONE;
168                 c.anchor = GridBagConstraints.LINE_START;
169                 c.insets = il;
170                 c.ipady = 5;
171                 serial_label = new JLabel("Serial:");
172                 pane.add(serial_label, c);
173
174                 c = new GridBagConstraints();
175                 c.gridx = 4; c.gridy = 2;
176                 c.gridwidth = 4;
177                 c.fill = GridBagConstraints.HORIZONTAL;
178                 c.weightx = 1;
179                 c.anchor = GridBagConstraints.LINE_START;
180                 c.insets = ir;
181                 c.ipady = 5;
182                 serial_value = new JLabel("");
183                 pane.add(serial_value, c);
184
185                 /* Main deploy */
186                 c = new GridBagConstraints();
187                 c.gridx = 0; c.gridy = 3;
188                 c.gridwidth = 4;
189                 c.fill = GridBagConstraints.NONE;
190                 c.anchor = GridBagConstraints.LINE_START;
191                 c.insets = il;
192                 c.ipady = 5;
193                 main_deploy_label = new JLabel("Main Deploy Altitude(m):");
194                 pane.add(main_deploy_label, c);
195
196                 c = new GridBagConstraints();
197                 c.gridx = 4; c.gridy = 3;
198                 c.gridwidth = 4;
199                 c.fill = GridBagConstraints.HORIZONTAL;
200                 c.weightx = 1;
201                 c.anchor = GridBagConstraints.LINE_START;
202                 c.insets = ir;
203                 c.ipady = 5;
204                 main_deploy_value = new JComboBox(main_deploy_values);
205                 main_deploy_value.setEditable(true);
206                 main_deploy_value.addItemListener(this);
207                 pane.add(main_deploy_value, c);
208
209                 /* Apogee delay */
210                 c = new GridBagConstraints();
211                 c.gridx = 0; c.gridy = 4;
212                 c.gridwidth = 4;
213                 c.fill = GridBagConstraints.NONE;
214                 c.anchor = GridBagConstraints.LINE_START;
215                 c.insets = il;
216                 c.ipady = 5;
217                 apogee_delay_label = new JLabel("Apogee Delay(s):");
218                 pane.add(apogee_delay_label, c);
219
220                 c = new GridBagConstraints();
221                 c.gridx = 4; c.gridy = 4;
222                 c.gridwidth = 4;
223                 c.fill = GridBagConstraints.HORIZONTAL;
224                 c.weightx = 1;
225                 c.anchor = GridBagConstraints.LINE_START;
226                 c.insets = ir;
227                 c.ipady = 5;
228                 apogee_delay_value = new JComboBox(apogee_delay_values);
229                 apogee_delay_value.setEditable(true);
230                 apogee_delay_value.addItemListener(this);
231                 pane.add(apogee_delay_value, c);
232
233                 /* Radio channel */
234                 c = new GridBagConstraints();
235                 c.gridx = 0; c.gridy = 5;
236                 c.gridwidth = 4;
237                 c.fill = GridBagConstraints.NONE;
238                 c.anchor = GridBagConstraints.LINE_START;
239                 c.insets = il;
240                 c.ipady = 5;
241                 radio_channel_label = new JLabel("Radio Channel:");
242                 pane.add(radio_channel_label, c);
243
244                 c = new GridBagConstraints();
245                 c.gridx = 4; c.gridy = 5;
246                 c.gridwidth = 4;
247                 c.fill = GridBagConstraints.HORIZONTAL;
248                 c.weightx = 1;
249                 c.anchor = GridBagConstraints.LINE_START;
250                 c.insets = ir;
251                 c.ipady = 5;
252                 radio_channel_value = new JComboBox(radio_channel_values);
253                 radio_channel_value.setEditable(false);
254                 radio_channel_value.addItemListener(this);
255                 pane.add(radio_channel_value, c);
256
257                 /* Radio Calibration */
258                 c = new GridBagConstraints();
259                 c.gridx = 0; c.gridy = 6;
260                 c.gridwidth = 4;
261                 c.fill = GridBagConstraints.NONE;
262                 c.anchor = GridBagConstraints.LINE_START;
263                 c.insets = il;
264                 c.ipady = 5;
265                 radio_calibration_label = new JLabel("RF Calibration:");
266                 pane.add(radio_calibration_label, c);
267
268                 c = new GridBagConstraints();
269                 c.gridx = 4; c.gridy = 6;
270                 c.gridwidth = 4;
271                 c.fill = GridBagConstraints.HORIZONTAL;
272                 c.weightx = 1;
273                 c.anchor = GridBagConstraints.LINE_START;
274                 c.insets = ir;
275                 c.ipady = 5;
276                 radio_calibration_value = new JTextField(String.format("%d", 1186611));
277                 radio_calibration_value.getDocument().addDocumentListener(this);
278                 if (remote)
279                         radio_calibration_value.setEnabled(false);
280                 pane.add(radio_calibration_value, c);
281
282                 /* Callsign */
283                 c = new GridBagConstraints();
284                 c.gridx = 0; c.gridy = 7;
285                 c.gridwidth = 4;
286                 c.fill = GridBagConstraints.NONE;
287                 c.anchor = GridBagConstraints.LINE_START;
288                 c.insets = il;
289                 c.ipady = 5;
290                 callsign_label = new JLabel("Callsign:");
291                 pane.add(callsign_label, c);
292
293                 c = new GridBagConstraints();
294                 c.gridx = 4; c.gridy = 7;
295                 c.gridwidth = 4;
296                 c.fill = GridBagConstraints.HORIZONTAL;
297                 c.weightx = 1;
298                 c.anchor = GridBagConstraints.LINE_START;
299                 c.insets = ir;
300                 c.ipady = 5;
301                 callsign_value = new JTextField(AltosPreferences.callsign());
302                 callsign_value.getDocument().addDocumentListener(this);
303                 pane.add(callsign_value, c);
304
305                 /* Flight log max */
306                 c = new GridBagConstraints();
307                 c.gridx = 0; c.gridy = 8;
308                 c.gridwidth = 4;
309                 c.fill = GridBagConstraints.NONE;
310                 c.anchor = GridBagConstraints.LINE_START;
311                 c.insets = il;
312                 c.ipady = 5;
313                 flight_log_max_label = new JLabel("Maximum Flight Log Size:");
314                 pane.add(flight_log_max_label, c);
315
316                 c = new GridBagConstraints();
317                 c.gridx = 4; c.gridy = 8;
318                 c.gridwidth = 4;
319                 c.fill = GridBagConstraints.HORIZONTAL;
320                 c.weightx = 1;
321                 c.anchor = GridBagConstraints.LINE_START;
322                 c.insets = ir;
323                 c.ipady = 5;
324                 flight_log_max_value = new JComboBox(flight_log_max_values);
325                 flight_log_max_value.setEditable(true);
326                 flight_log_max_value.addItemListener(this);
327                 pane.add(flight_log_max_value, c);
328
329                 /* Buttons */
330                 c = new GridBagConstraints();
331                 c.gridx = 0; c.gridy = 9;
332                 c.gridwidth = 2;
333                 c.fill = GridBagConstraints.NONE;
334                 c.anchor = GridBagConstraints.LINE_START;
335                 c.insets = il;
336                 save = new JButton("Save");
337                 pane.add(save, c);
338                 save.addActionListener(this);
339                 save.setActionCommand("Save");
340
341                 c = new GridBagConstraints();
342                 c.gridx = 2; c.gridy = 9;
343                 c.gridwidth = 2;
344                 c.fill = GridBagConstraints.NONE;
345                 c.anchor = GridBagConstraints.CENTER;
346                 c.insets = il;
347                 reset = new JButton("Reset");
348                 pane.add(reset, c);
349                 reset.addActionListener(this);
350                 reset.setActionCommand("Reset");
351
352                 c = new GridBagConstraints();
353                 c.gridx = 4; c.gridy = 9;
354                 c.gridwidth = 2;
355                 c.fill = GridBagConstraints.NONE;
356                 c.anchor = GridBagConstraints.CENTER;
357                 c.insets = il;
358                 reboot = new JButton("Reboot");
359                 pane.add(reboot, c);
360                 reboot.addActionListener(this);
361                 reboot.setActionCommand("Reboot");
362
363                 c = new GridBagConstraints();
364                 c.gridx = 6; c.gridy = 9;
365                 c.gridwidth = 2;
366                 c.fill = GridBagConstraints.NONE;
367                 c.anchor = GridBagConstraints.LINE_END;
368                 c.insets = il;
369                 close = new JButton("Close");
370                 pane.add(close, c);
371                 close.addActionListener(this);
372                 close.setActionCommand("Close");
373
374                 addWindowListener(new ConfigListener(this));
375         }
376
377         /* Once the initial values are set, the config code will show the dialog */
378         public void make_visible() {
379                 pack();
380                 setLocationRelativeTo(owner);
381                 setVisible(true);
382         }
383
384         /* If any values have been changed, confirm before closing */
385         public boolean check_dirty(String operation) {
386                 if (dirty) {
387                         Object[] options = { String.format("%s anyway", operation), "Keep editing" };
388                         int i;
389                         i = JOptionPane.showOptionDialog(this,
390                                                          String.format("Configuration modified. %s anyway?", operation),
391                                                          "Configuration Modified",
392                                                          JOptionPane.DEFAULT_OPTION,
393                                                          JOptionPane.WARNING_MESSAGE,
394                                                          null, options, options[1]);
395                         if (i != 0)
396                                 return false;
397                 }
398                 return true;
399         }
400
401         /* Listen for events from our buttons */
402         public void actionPerformed(ActionEvent e) {
403                 String  cmd = e.getActionCommand();
404
405                 if (cmd.equals("Close") || cmd.equals("Reboot"))
406                         if (!check_dirty(cmd))
407                                 return;
408                 listener.actionPerformed(e);
409                 if (cmd.equals("Close") || cmd.equals("Reboot")) {
410                         setVisible(false);
411                         dispose();
412                 }
413                 dirty = false;
414         }
415
416         /* ItemListener interface method */
417         public void itemStateChanged(ItemEvent e) {
418                 dirty = true;
419         }
420
421         /* DocumentListener interface methods */
422         public void changedUpdate(DocumentEvent e) {
423                 dirty = true;
424         }
425
426         public void insertUpdate(DocumentEvent e) {
427                 dirty = true;
428         }
429
430         public void removeUpdate(DocumentEvent e) {
431                 dirty = true;
432         }
433
434         /* Let the config code hook on a listener */
435         public void addActionListener(ActionListener l) {
436                 listener = l;
437         }
438
439         /* set and get all of the dialog values */
440         public void set_product(String product) {
441                 product_value.setText(product);
442         }
443
444         public void set_version(String version) {
445                 version_value.setText(version);
446         }
447
448         public void set_serial(int serial) {
449                 serial_value.setText(String.format("%d", serial));
450         }
451
452         public void set_main_deploy(int new_main_deploy) {
453                 main_deploy_value.setSelectedItem(Integer.toString(new_main_deploy));
454         }
455
456         public int main_deploy() {
457                 return Integer.parseInt(main_deploy_value.getSelectedItem().toString());
458         }
459
460         public void set_apogee_delay(int new_apogee_delay) {
461                 apogee_delay_value.setSelectedItem(Integer.toString(new_apogee_delay));
462         }
463
464         public int apogee_delay() {
465                 return Integer.parseInt(apogee_delay_value.getSelectedItem().toString());
466         }
467
468         public void set_radio_channel(int new_radio_channel) {
469                 radio_channel_value.setSelectedIndex(new_radio_channel);
470         }
471
472         public int radio_channel() {
473                 return radio_channel_value.getSelectedIndex();
474         }
475
476         public void set_radio_calibration(int new_radio_calibration) {
477                 radio_calibration_value.setText(String.format("%d", new_radio_calibration));
478         }
479
480         public int radio_calibration() {
481                 return Integer.parseInt(radio_calibration_value.getText());
482         }
483
484         public void set_callsign(String new_callsign) {
485                 callsign_value.setText(new_callsign);
486         }
487
488         public String callsign() {
489                 return callsign_value.getText();
490         }
491
492         public void set_flight_log_max(int new_flight_log_max) {
493                 if (new_flight_log_max == 0)
494                         flight_log_max_value.setEnabled(false);
495                 flight_log_max_value.setSelectedItem(Integer.toString(new_flight_log_max));
496         }
497
498         public int flight_log_max() {
499                 return Integer.parseInt(flight_log_max_value.getSelectedItem().toString());
500         }
501
502         public void set_clean() {
503                 dirty = false;
504         }
505 }