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