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 arm.setEnabled(true);
150 if (cmd.equals("main") && main.isSelected()) {
151 apogee.setSelected(false);
152 arm.setEnabled(true);
155 if (cmd.equals("arm")) {
156 if (arm.isSelected()) {
157 fire.setEnabled(true);
162 if (cmd.equals("fire"))
164 if (cmd.equals("tick"))
166 if (cmd.equals("close")) {
171 /* A window listener to catch closing events and tell the config code */
172 class ConfigListener extends WindowAdapter {
175 public ConfigListener(AltosIgniteUI this_ui) {
179 public void windowClosing(WindowEvent e) {
180 ui.actionPerformed(new ActionEvent(e.getSource(),
181 ActionEvent.ACTION_PERFORMED,
186 private boolean open() {
187 device = AltosDeviceDialog.show(owner, AltosDevice.product_any);
188 if (device != null) {
190 ignite = new AltosIgnite(device);
192 } catch (FileNotFoundException ee) {
193 JOptionPane.showMessageDialog(owner,
194 String.format("Cannot open device \"%s\"",
195 device.toShortString()),
196 "Cannot open target device",
197 JOptionPane.ERROR_MESSAGE);
198 } catch (AltosSerialInUseException si) {
199 JOptionPane.showMessageDialog(owner,
200 String.format("Device \"%s\" already in use",
201 device.toShortString()),
203 JOptionPane.ERROR_MESSAGE);
204 } catch (IOException ee) {
205 JOptionPane.showMessageDialog(owner,
206 device.toShortString(),
207 ee.getLocalizedMessage(),
208 JOptionPane.ERROR_MESSAGE);
214 public AltosIgniteUI(JFrame in_owner) {
217 apogee_status = AltosIgnite.Unknown;
218 main_status = AltosIgnite.Unknown;
223 Container pane = getContentPane();
224 GridBagConstraints c = new GridBagConstraints();
225 Insets i = new Insets(4,4,4,4);
227 timer = new javax.swing.Timer(timeout, this);
228 timer.setActionCommand("tick");
229 timer_running = false;
234 pane.setLayout(new GridBagLayout());
236 c.fill = GridBagConstraints.NONE;
237 c.anchor = GridBagConstraints.CENTER;
245 c.anchor = GridBagConstraints.CENTER;
246 label = new JLabel ("Fire Igniter");
252 c.anchor = GridBagConstraints.WEST;
253 apogee = new JRadioButton ("Apogee");
255 apogee.addActionListener(this);
256 apogee.setActionCommand("apogee");
261 c.anchor = GridBagConstraints.WEST;
262 apogee_status_label = new JLabel();
263 pane.add(apogee_status_label, c);
268 c.anchor = GridBagConstraints.WEST;
269 main = new JRadioButton ("Main");
271 main.addActionListener(this);
272 main.setActionCommand("main");
277 c.anchor = GridBagConstraints.WEST;
278 main_status_label = new JLabel();
279 pane.add(main_status_label, c);
283 } catch (InterruptedException ie) {
286 } catch (TimeoutException te) {
294 c.anchor = GridBagConstraints.CENTER;
295 arm = new JToggleButton ("Arm");
297 arm.addActionListener(this);
298 arm.setActionCommand("arm");
299 arm.setEnabled(false);
304 c.anchor = GridBagConstraints.CENTER;
305 fire = new JButton ("Fire");
306 fire.setEnabled(false);
308 fire.addActionListener(this);
309 fire.setActionCommand("fire");
312 setLocationRelativeTo(owner);
315 addWindowListener(new ConfigListener(this));