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.*;
32 public class AltosIgniteUI
34 implements ActionListener
41 JLabel apogee_status_label;
43 JLabel main_status_label;
46 javax.swing.Timer timer;
51 final static int timeout = 1 * 1000;
54 boolean timer_running;
58 arm.setText(String.format("%d", time_remaining));
71 arm.setSelected(false);
72 arm.setEnabled(false);
73 fire.setEnabled(false);
74 timer_running = false;
79 apogee.setSelected(false);
80 main.setSelected(false);
81 fire.setEnabled(false);
85 void get_ignite_status() throws InterruptedException, TimeoutException {
86 apogee_status = ignite.status(AltosIgnite.Apogee);
87 main_status = ignite.status(AltosIgnite.Main);
90 void set_ignite_status() throws InterruptedException, TimeoutException {
92 apogee_status_label.setText(String.format("\"%s\"", ignite.status_string(apogee_status)));
93 main_status_label.setText(String.format("\"%s\"", ignite.status_string(main_status)));
104 JOptionPane.showMessageDialog(owner,
105 String.format("Connection to \"%s\" failed",
106 device.toShortString()),
108 JOptionPane.ERROR_MESSAGE);
114 if (time_remaining <= 0)
121 } catch (InterruptedException ie) {
123 } catch (TimeoutException te) {
129 if (arm.isEnabled() && arm.isSelected() && time_remaining > 0) {
130 int igniter = AltosIgnite.None;
131 if (apogee.isSelected() && !main.isSelected())
132 igniter = AltosIgnite.Apogee;
133 else if (main.isSelected() && !apogee.isSelected())
134 igniter = AltosIgnite.Main;
135 ignite.fire(igniter);
140 public void actionPerformed(ActionEvent e) {
141 String cmd = e.getActionCommand();
142 if (cmd.equals("apogee") || cmd.equals("main")) {
146 if (cmd.equals("apogee") && apogee.isSelected()) {
147 main.setSelected(false);
148 if (apogee_status == AltosIgnite.Ready)
149 arm.setEnabled(true);
151 if (cmd.equals("main") && main.isSelected()) {
152 apogee.setSelected(false);
153 if (main_status == AltosIgnite.Ready)
154 arm.setEnabled(true);
157 if (cmd.equals("arm")) {
158 if (arm.isSelected()) {
159 fire.setEnabled(true);
164 if (cmd.equals("fire"))
166 if (cmd.equals("tick"))
168 if (cmd.equals("close")) {
173 /* A window listener to catch closing events and tell the config code */
174 class ConfigListener extends WindowAdapter {
177 public ConfigListener(AltosIgniteUI this_ui) {
181 public void windowClosing(WindowEvent e) {
182 ui.actionPerformed(new ActionEvent(e.getSource(),
183 ActionEvent.ACTION_PERFORMED,
188 private boolean open() {
189 device = AltosDeviceDialog.show(owner, AltosDevice.product_any);
190 if (device != null) {
192 ignite = new AltosIgnite(device);
194 } catch (FileNotFoundException ee) {
195 JOptionPane.showMessageDialog(owner,
196 String.format("Cannot open device \"%s\"",
197 device.toShortString()),
198 "Cannot open target device",
199 JOptionPane.ERROR_MESSAGE);
200 } catch (AltosSerialInUseException si) {
201 JOptionPane.showMessageDialog(owner,
202 String.format("Device \"%s\" already in use",
203 device.toShortString()),
205 JOptionPane.ERROR_MESSAGE);
206 } catch (IOException ee) {
207 JOptionPane.showMessageDialog(owner,
208 device.toShortString(),
209 ee.getLocalizedMessage(),
210 JOptionPane.ERROR_MESSAGE);
216 public AltosIgniteUI(JFrame in_owner) {
219 apogee_status = AltosIgnite.Unknown;
220 main_status = AltosIgnite.Unknown;
225 Container pane = getContentPane();
226 GridBagConstraints c = new GridBagConstraints();
227 Insets i = new Insets(4,4,4,4);
229 timer = new javax.swing.Timer(timeout, this);
230 timer.setActionCommand("tick");
231 timer_running = false;
236 pane.setLayout(new GridBagLayout());
238 c.fill = GridBagConstraints.NONE;
239 c.anchor = GridBagConstraints.CENTER;
247 c.anchor = GridBagConstraints.CENTER;
248 label = new JLabel ("Fire Igniter");
254 c.anchor = GridBagConstraints.WEST;
255 apogee = new JRadioButton ("Apogee");
257 apogee.addActionListener(this);
258 apogee.setActionCommand("apogee");
263 c.anchor = GridBagConstraints.WEST;
264 apogee_status_label = new JLabel();
265 pane.add(apogee_status_label, c);
270 c.anchor = GridBagConstraints.WEST;
271 main = new JRadioButton ("Main");
273 main.addActionListener(this);
274 main.setActionCommand("main");
279 c.anchor = GridBagConstraints.WEST;
280 main_status_label = new JLabel();
281 pane.add(main_status_label, c);
285 } catch (InterruptedException ie) {
288 } catch (TimeoutException te) {
296 c.anchor = GridBagConstraints.CENTER;
297 arm = new JToggleButton ("Arm");
299 arm.addActionListener(this);
300 arm.setActionCommand("arm");
301 arm.setEnabled(false);
306 c.anchor = GridBagConstraints.CENTER;
307 fire = new JButton ("Fire");
308 fire.setEnabled(false);
310 fire.addActionListener(this);
311 fire.setActionCommand("fire");
314 setLocationRelativeTo(owner);
317 addWindowListener(new ConfigListener(this));