2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import javax.swing.event.*;
29 import java.util.prefs.*;
30 import java.util.concurrent.LinkedBlockingQueue;
34 public class AltosConfigUI
36 implements ActionListener, ItemListener, DocumentListener
44 JLabel main_deploy_label;
45 JLabel apogee_delay_label;
46 JLabel radio_channel_label;
47 JLabel radio_calibration_label;
48 JLabel callsign_label;
56 JComboBox main_deploy_value;
57 JComboBox apogee_delay_value;
58 JComboBox radio_channel_value;
59 JTextField radio_calibration_value;
60 JTextField callsign_value;
67 ActionListener listener;
69 static String[] main_deploy_values = {
70 "100", "150", "200", "250", "300", "350",
74 static String[] apogee_delay_values = {
75 "0", "1", "2", "3", "4", "5"
78 static String[] radio_channel_values = new String[10];
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);
85 /* A window listener to catch closing events and tell the config code */
86 class ConfigListener extends WindowAdapter {
89 public ConfigListener(AltosConfigUI this_ui) {
93 public void windowClosing(WindowEvent e) {
94 ui.actionPerformed(new ActionEvent(e.getSource(),
95 ActionEvent.ACTION_PERFORMED,
100 /* Build the UI using a grid bag */
101 public AltosConfigUI(JFrame in_owner, boolean remote) {
102 super (in_owner, "Configure TeleMetrum", false);
105 GridBagConstraints c;
107 Insets il = new Insets(4,4,4,4);
108 Insets ir = new Insets(4,4,4,4);
110 pane = getContentPane();
111 pane.setLayout(new GridBagLayout());
114 c = new GridBagConstraints();
115 c.gridx = 0; c.gridy = 0;
117 c.fill = GridBagConstraints.NONE;
118 c.anchor = GridBagConstraints.LINE_START;
120 product_label = new JLabel("Product:");
121 pane.add(product_label, c);
123 c = new GridBagConstraints();
124 c.gridx = 4; c.gridy = 0;
126 c.fill = GridBagConstraints.HORIZONTAL;
128 c.anchor = GridBagConstraints.LINE_START;
130 product_value = new JLabel("");
131 pane.add(product_value, c);
134 c = new GridBagConstraints();
135 c.gridx = 0; c.gridy = 1;
137 c.fill = GridBagConstraints.NONE;
138 c.anchor = GridBagConstraints.LINE_START;
141 version_label = new JLabel("Software version:");
142 pane.add(version_label, c);
144 c = new GridBagConstraints();
145 c.gridx = 4; c.gridy = 1;
147 c.fill = GridBagConstraints.HORIZONTAL;
149 c.anchor = GridBagConstraints.LINE_START;
152 version_value = new JLabel("");
153 pane.add(version_value, c);
156 c = new GridBagConstraints();
157 c.gridx = 0; c.gridy = 2;
159 c.fill = GridBagConstraints.NONE;
160 c.anchor = GridBagConstraints.LINE_START;
163 serial_label = new JLabel("Serial:");
164 pane.add(serial_label, c);
166 c = new GridBagConstraints();
167 c.gridx = 4; c.gridy = 2;
169 c.fill = GridBagConstraints.HORIZONTAL;
171 c.anchor = GridBagConstraints.LINE_START;
174 serial_value = new JLabel("");
175 pane.add(serial_value, c);
178 c = new GridBagConstraints();
179 c.gridx = 0; c.gridy = 3;
181 c.fill = GridBagConstraints.NONE;
182 c.anchor = GridBagConstraints.LINE_START;
185 main_deploy_label = new JLabel("Main Deploy Altitude(m):");
186 pane.add(main_deploy_label, c);
188 c = new GridBagConstraints();
189 c.gridx = 4; c.gridy = 3;
191 c.fill = GridBagConstraints.HORIZONTAL;
193 c.anchor = GridBagConstraints.LINE_START;
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);
202 c = new GridBagConstraints();
203 c.gridx = 0; c.gridy = 4;
205 c.fill = GridBagConstraints.NONE;
206 c.anchor = GridBagConstraints.LINE_START;
209 apogee_delay_label = new JLabel("Apogee Delay(s):");
210 pane.add(apogee_delay_label, c);
212 c = new GridBagConstraints();
213 c.gridx = 4; c.gridy = 4;
215 c.fill = GridBagConstraints.HORIZONTAL;
217 c.anchor = GridBagConstraints.LINE_START;
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);
226 c = new GridBagConstraints();
227 c.gridx = 0; c.gridy = 5;
229 c.fill = GridBagConstraints.NONE;
230 c.anchor = GridBagConstraints.LINE_START;
233 radio_channel_label = new JLabel("Radio Channel:");
234 pane.add(radio_channel_label, c);
236 c = new GridBagConstraints();
237 c.gridx = 4; c.gridy = 5;
239 c.fill = GridBagConstraints.HORIZONTAL;
241 c.anchor = GridBagConstraints.LINE_START;
244 radio_channel_value = new JComboBox(radio_channel_values);
245 radio_channel_value.setEditable(false);
246 radio_channel_value.addItemListener(this);
248 radio_channel_value.setEnabled(false);
249 pane.add(radio_channel_value, c);
251 /* Radio Calibration */
252 c = new GridBagConstraints();
253 c.gridx = 0; c.gridy = 6;
255 c.fill = GridBagConstraints.NONE;
256 c.anchor = GridBagConstraints.LINE_START;
259 radio_calibration_label = new JLabel("RF Calibration:");
260 pane.add(radio_calibration_label, c);
262 c = new GridBagConstraints();
263 c.gridx = 4; c.gridy = 6;
265 c.fill = GridBagConstraints.HORIZONTAL;
267 c.anchor = GridBagConstraints.LINE_START;
270 radio_calibration_value = new JTextField(String.format("%d", 1186611));
271 radio_calibration_value.getDocument().addDocumentListener(this);
273 radio_calibration_value.setEnabled(false);
274 pane.add(radio_calibration_value, c);
277 c = new GridBagConstraints();
278 c.gridx = 0; c.gridy = 7;
280 c.fill = GridBagConstraints.NONE;
281 c.anchor = GridBagConstraints.LINE_START;
284 callsign_label = new JLabel("Callsign:");
285 pane.add(callsign_label, c);
287 c = new GridBagConstraints();
288 c.gridx = 4; c.gridy = 7;
290 c.fill = GridBagConstraints.HORIZONTAL;
292 c.anchor = GridBagConstraints.LINE_START;
295 callsign_value = new JTextField(AltosPreferences.callsign());
296 callsign_value.getDocument().addDocumentListener(this);
297 pane.add(callsign_value, c);
300 c = new GridBagConstraints();
301 c.gridx = 0; c.gridy = 8;
303 c.fill = GridBagConstraints.NONE;
304 c.anchor = GridBagConstraints.LINE_START;
306 save = new JButton("Save");
308 save.addActionListener(this);
309 save.setActionCommand("Save");
311 c = new GridBagConstraints();
312 c.gridx = 2; c.gridy = 8;
314 c.fill = GridBagConstraints.NONE;
315 c.anchor = GridBagConstraints.CENTER;
317 reset = new JButton("Reset");
319 reset.addActionListener(this);
320 reset.setActionCommand("Reset");
322 c = new GridBagConstraints();
323 c.gridx = 4; c.gridy = 8;
325 c.fill = GridBagConstraints.NONE;
326 c.anchor = GridBagConstraints.CENTER;
328 reboot = new JButton("Reboot");
330 reboot.addActionListener(this);
331 reboot.setActionCommand("Reboot");
333 c = new GridBagConstraints();
334 c.gridx = 6; c.gridy = 8;
336 c.fill = GridBagConstraints.NONE;
337 c.anchor = GridBagConstraints.LINE_END;
339 close = new JButton("Close");
341 close.addActionListener(this);
342 close.setActionCommand("Close");
344 addWindowListener(new ConfigListener(this));
347 /* Once the initial values are set, the config code will show the dialog */
348 public void make_visible() {
350 setLocationRelativeTo(owner);
354 /* If any values have been changed, confirm before closing */
355 public boolean check_dirty(String operation) {
357 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
359 i = JOptionPane.showOptionDialog(this,
360 String.format("Configuration modified. %s anyway?", operation),
361 "Configuration Modified",
362 JOptionPane.DEFAULT_OPTION,
363 JOptionPane.WARNING_MESSAGE,
364 null, options, options[1]);
371 /* Listen for events from our buttons */
372 public void actionPerformed(ActionEvent e) {
373 String cmd = e.getActionCommand();
375 if (cmd.equals("Close") || cmd.equals("Reboot"))
376 if (!check_dirty(cmd))
378 listener.actionPerformed(e);
379 if (cmd.equals("Close") || cmd.equals("Reboot")) {
386 /* ItemListener interface method */
387 public void itemStateChanged(ItemEvent e) {
391 /* DocumentListener interface methods */
392 public void changedUpdate(DocumentEvent e) {
396 public void insertUpdate(DocumentEvent e) {
400 public void removeUpdate(DocumentEvent e) {
404 /* Let the config code hook on a listener */
405 public void addActionListener(ActionListener l) {
409 /* set and get all of the dialog values */
410 public void set_product(String product) {
411 product_value.setText(product);
414 public void set_version(String version) {
415 version_value.setText(version);
418 public void set_serial(int serial) {
419 serial_value.setText(String.format("%d", serial));
422 public void set_main_deploy(int new_main_deploy) {
423 main_deploy_value.setSelectedItem(Integer.toString(new_main_deploy));
426 public int main_deploy() {
427 return Integer.parseInt(main_deploy_value.getSelectedItem().toString());
430 public void set_apogee_delay(int new_apogee_delay) {
431 apogee_delay_value.setSelectedItem(Integer.toString(new_apogee_delay));
434 public int apogee_delay() {
435 return Integer.parseInt(apogee_delay_value.getSelectedItem().toString());
438 public void set_radio_channel(int new_radio_channel) {
439 radio_channel_value.setSelectedIndex(new_radio_channel);
442 public int radio_channel() {
443 return radio_channel_value.getSelectedIndex();
446 public void set_radio_calibration(int new_radio_calibration) {
447 radio_calibration_value.setText(String.format("%d", new_radio_calibration));
450 public int radio_calibration() {
451 return Integer.parseInt(radio_calibration_value.getText());
454 public void set_callsign(String new_callsign) {
455 callsign_value.setText(new_callsign);
458 public String callsign() {
459 return callsign_value.getText();
462 public void set_clean() {