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