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.*;
25 import java.util.concurrent.*;
27 class FireButton extends JButton {
28 protected void processMouseEvent(MouseEvent e) {
29 super.processMouseEvent(e);
31 case MouseEvent.MOUSE_PRESSED:
32 if (actionListener != null)
33 actionListener.actionPerformed(new ActionEvent(this, e.getID(), "fire_down"));
35 case MouseEvent.MOUSE_RELEASED:
36 if (actionListener != null)
37 actionListener.actionPerformed(new ActionEvent(this, e.getID(), "fire_up"));
42 public FireButton(String s) {
47 public class AltosLaunchUI
49 implements ActionListener
56 JLabel radio_channel_label;
57 JTextField radio_channel_text;
60 JLabel launcher_serial_label;
61 JTextField launcher_serial_text;
64 JLabel launcher_channel_label;
65 JTextField launcher_channel_text;
68 JLabel armed_status_label;
70 JLabel igniter_status_label;
73 javax.swing.Timer arm_timer;
74 javax.swing.Timer fire_timer;
82 final static int arm_timeout = 1 * 1000;
83 final static int fire_timeout = 250;
87 LinkedBlockingQueue<String> command_queue;
89 class LaunchHandler implements Runnable {
93 void send_exception(Exception e) {
94 final Exception f_e = e;
95 Runnable r = new Runnable() {
97 launch_exception(f_e);
100 SwingUtilities.invokeLater(r);
105 launch = new AltosLaunch(device);
106 } catch (Exception e) {
110 launch.set_frame(owner);
111 launch.set_remote(launcher_serial, launcher_channel);
117 String command = command_queue.take();
120 if (command.equals("get_status")) {
123 armed_status = launch.armed;
124 igniter_status = launch.igniter;
126 } else if (command.equals("set_remote")) {
127 launch.set_remote(launcher_serial, launcher_channel);
128 reply = "remote set";
129 } else if (command.equals("arm")) {
132 } else if (command.equals("fire")) {
135 } else if (command.equals("quit")) {
139 throw new ParseException(String.format("invalid command %s", command), 0);
141 final String f_reply = reply;
144 launch_reply(f_reply);
147 SwingUtilities.invokeLater(r);
148 } catch (Exception e) {
154 public LaunchHandler(JFrame in_owner) {
159 void launch_exception(Exception e) {
160 if (e instanceof FileNotFoundException) {
161 JOptionPane.showMessageDialog(owner,
162 ((FileNotFoundException) e).getMessage(),
163 "Cannot open target device",
164 JOptionPane.ERROR_MESSAGE);
165 } else if (e instanceof AltosSerialInUseException) {
166 JOptionPane.showMessageDialog(owner,
167 String.format("Device \"%s\" already in use",
168 device.toShortString()),
170 JOptionPane.ERROR_MESSAGE);
171 } else if (e instanceof IOException) {
172 IOException ee = (IOException) e;
173 JOptionPane.showMessageDialog(owner,
174 device.toShortString(),
175 ee.getLocalizedMessage(),
176 JOptionPane.ERROR_MESSAGE);
178 JOptionPane.showMessageDialog(owner,
179 String.format("Connection to \"%s\" failed",
180 device.toShortString()),
182 JOptionPane.ERROR_MESSAGE);
187 void launch_reply(String reply) {
190 if (reply.equals("remote set"))
191 poll_launch_status();
192 if (reply.equals("status")) {
197 void set_arm_text() {
198 if (arm.isSelected())
199 arm.setText(String.format("%d", armed_count));
204 void start_arm_timer() {
209 void stop_arm_timer() {
212 arm.setSelected(false);
213 fire.setEnabled(false);
218 fire.setEnabled(false);
223 void send_command(String command) {
225 command_queue.put(command);
226 } catch (Exception ex) {
227 launch_exception(ex);
231 boolean getting_status = false;
233 void set_launch_status() {
234 getting_status = false;
235 armed_status_label.setText(String.format("\"%s\"", AltosLaunch.status_string(armed_status)));
236 igniter_status_label.setText(String.format("\"%s\"", AltosLaunch.status_string(igniter_status)));
239 void poll_launch_status() {
240 if (!getting_status && !firing && !armed) {
241 getting_status = true;
242 send_command("get_status");
252 send_command("quit");
258 void tick_arm_timer() {
259 if (armed_count > 0) {
261 if (armed_count <= 0) {
271 poll_launch_status();
275 if (arm.isSelected()) {
276 fire.setEnabled(true);
287 send_command("fire");
291 if (arm.isEnabled() && arm.isSelected() && armed_count > 0) {
294 fire_timer.restart();
305 radio_channel = Integer.parseInt(radio_channel_text.getText());
306 } catch (NumberFormatException ne) {
307 radio_channel_text.setText(String.format("%d", radio_channel));
313 launcher_serial = Integer.parseInt(launcher_serial_text.getText());
314 AltosUIPreferences.set_launcher_serial(launcher_serial);
315 send_command("set_remote");
316 } catch (NumberFormatException ne) {
317 launcher_serial_text.setText(String.format("%d", launcher_serial));
323 launcher_channel = Integer.parseInt(launcher_channel_text.getText());
324 AltosUIPreferences.set_launcher_serial(launcher_channel);
325 send_command("set_remote");
326 } catch (NumberFormatException ne) {
327 launcher_channel_text.setText(String.format("%d", launcher_channel));
331 public void actionPerformed(ActionEvent e) {
332 String cmd = e.getActionCommand();
333 if (cmd.equals("armed") || cmd.equals("igniter")) {
337 if (cmd.equals("arm"))
339 if (cmd.equals("tick_arm"))
341 if (cmd.equals("close"))
343 if (cmd.equals("fire_down"))
345 if (cmd.equals("fire_up"))
347 if (cmd.equals("tick_fire"))
349 if (cmd.equals("new_serial"))
351 if (cmd.equals("new_channel"))
355 /* A window listener to catch closing events and tell the config code */
356 class ConfigListener extends WindowAdapter {
359 public ConfigListener(AltosLaunchUI this_ui) {
363 public void windowClosing(WindowEvent e) {
364 ui.actionPerformed(new ActionEvent(e.getSource(),
365 ActionEvent.ACTION_PERFORMED,
370 private boolean open() {
371 command_queue = new LinkedBlockingQueue<String>();
373 device = AltosDeviceDialog.show(owner, Altos.product_any);
374 if (device != null) {
375 LaunchHandler handler = new LaunchHandler(owner);
376 Thread t = new Thread(handler);
383 public AltosLaunchUI(JFrame in_owner) {
385 launcher_channel = AltosUIPreferences.launcher_channel();
386 launcher_serial = AltosUIPreferences.launcher_serial();
388 armed_status = AltosLaunch.Unknown;
389 igniter_status = AltosLaunch.Unknown;
394 Container pane = getContentPane();
395 GridBagConstraints c = new GridBagConstraints();
396 Insets i = new Insets(4,4,4,4);
398 arm_timer = new javax.swing.Timer(arm_timeout, this);
399 arm_timer.setActionCommand("tick_arm");
402 fire_timer = new javax.swing.Timer(fire_timeout, this);
403 fire_timer.setActionCommand("tick_fire");
407 pane.setLayout(new GridBagLayout());
409 c.fill = GridBagConstraints.NONE;
410 c.anchor = GridBagConstraints.CENTER;
418 c.anchor = GridBagConstraints.CENTER;
419 label = new JLabel ("Launch Controller");
425 c.anchor = GridBagConstraints.WEST;
426 launcher_serial_label = new JLabel("Launcher Serial");
427 pane.add(launcher_serial_label, c);
432 c.anchor = GridBagConstraints.WEST;
433 launcher_serial_text = new JTextField(7);
434 launcher_serial_text.setText(String.format("%d", launcher_serial));
435 launcher_serial_text.setActionCommand("new_serial");
436 launcher_serial_text.addActionListener(this);
437 pane.add(launcher_serial_text, c);
442 c.anchor = GridBagConstraints.WEST;
443 launcher_channel_label = new JLabel("Launcher Channel");
444 pane.add(launcher_channel_label, c);
449 c.anchor = GridBagConstraints.WEST;
450 launcher_channel_text = new JTextField(7);
451 launcher_channel_text.setText(String.format("%d", launcher_channel));
452 launcher_channel_text.setActionCommand("new_channel");
453 launcher_channel_text.addActionListener(this);
454 pane.add(launcher_channel_text, c);
459 c.anchor = GridBagConstraints.WEST;
460 armed_label = new JLabel ("Armed");
461 pane.add(armed_label, c);
466 c.anchor = GridBagConstraints.WEST;
467 armed_status_label = new JLabel();
468 pane.add(armed_status_label, c);
473 c.anchor = GridBagConstraints.WEST;
474 igniter = new JLabel ("Igniter");
475 pane.add(igniter, c);
480 c.anchor = GridBagConstraints.WEST;
481 igniter_status_label = new JLabel();
482 pane.add(igniter_status_label, c);
487 c.anchor = GridBagConstraints.CENTER;
488 arm = new JToggleButton ("Arm");
490 arm.addActionListener(this);
491 arm.setActionCommand("arm");
492 arm.setEnabled(true);
497 c.anchor = GridBagConstraints.CENTER;
498 fire = new FireButton ("Fire");
499 fire.setEnabled(false);
501 fire.addActionListener(this);
502 fire.setActionCommand("fire");
505 setLocationRelativeTo(owner);
507 addWindowListener(new ConfigListener(this));