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