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.*;
31 import org.altusmetrum.AltosLib.*;
33 class FireButton extends JButton {
34 protected void processMouseEvent(MouseEvent e) {
35 super.processMouseEvent(e);
37 case MouseEvent.MOUSE_PRESSED:
38 if (actionListener != null)
39 actionListener.actionPerformed(new ActionEvent(this, e.getID(), "fire_down"));
41 case MouseEvent.MOUSE_RELEASED:
42 if (actionListener != null)
43 actionListener.actionPerformed(new ActionEvent(this, e.getID(), "fire_up"));
48 public FireButton(String s) {
53 public class AltosLaunchUI
55 implements ActionListener
62 JLabel radio_channel_label;
63 JTextField radio_channel_text;
66 JLabel launcher_serial_label;
67 JTextField launcher_serial_text;
70 JLabel launcher_channel_label;
71 JTextField launcher_channel_text;
74 JLabel armed_status_label;
76 JLabel igniter_status_label;
79 javax.swing.Timer arm_timer;
80 javax.swing.Timer fire_timer;
88 final static int arm_timeout = 1 * 1000;
89 final static int fire_timeout = 250;
93 LinkedBlockingQueue<String> command_queue;
95 class LaunchHandler implements Runnable {
99 void send_exception(Exception e) {
100 final Exception f_e = e;
101 Runnable r = new Runnable() {
103 launch_exception(f_e);
106 SwingUtilities.invokeLater(r);
111 launch = new AltosLaunch(device);
112 } catch (Exception e) {
116 launch.set_frame(owner);
117 launch.set_remote(launcher_serial, launcher_channel);
123 String command = command_queue.take();
126 if (command.equals("get_status")) {
129 armed_status = launch.armed;
130 igniter_status = launch.igniter;
132 } else if (command.equals("set_remote")) {
133 launch.set_remote(launcher_serial, launcher_channel);
134 reply = "remote set";
135 } else if (command.equals("arm")) {
138 } else if (command.equals("fire")) {
141 } else if (command.equals("quit")) {
145 throw new ParseException(String.format("invalid command %s", command), 0);
147 final String f_reply = reply;
150 launch_reply(f_reply);
153 SwingUtilities.invokeLater(r);
154 } catch (Exception e) {
160 public LaunchHandler(JFrame in_owner) {
165 void launch_exception(Exception e) {
166 if (e instanceof FileNotFoundException) {
167 JOptionPane.showMessageDialog(owner,
168 ((FileNotFoundException) e).getMessage(),
169 "Cannot open target device",
170 JOptionPane.ERROR_MESSAGE);
171 } else if (e instanceof AltosSerialInUseException) {
172 JOptionPane.showMessageDialog(owner,
173 String.format("Device \"%s\" already in use",
174 device.toShortString()),
176 JOptionPane.ERROR_MESSAGE);
177 } else if (e instanceof IOException) {
178 IOException ee = (IOException) e;
179 JOptionPane.showMessageDialog(owner,
180 device.toShortString(),
181 ee.getLocalizedMessage(),
182 JOptionPane.ERROR_MESSAGE);
184 JOptionPane.showMessageDialog(owner,
185 String.format("Connection to \"%s\" failed",
186 device.toShortString()),
188 JOptionPane.ERROR_MESSAGE);
193 void launch_reply(String reply) {
196 if (reply.equals("remote set"))
197 poll_launch_status();
198 if (reply.equals("status")) {
203 void set_arm_text() {
204 if (arm.isSelected())
205 arm.setText(String.format("%d", armed_count));
210 void start_arm_timer() {
215 void stop_arm_timer() {
218 arm.setSelected(false);
219 fire.setEnabled(false);
224 fire.setEnabled(false);
229 void send_command(String command) {
231 command_queue.put(command);
232 } catch (Exception ex) {
233 launch_exception(ex);
237 boolean getting_status = false;
239 void set_launch_status() {
240 getting_status = false;
241 armed_status_label.setText(String.format("\"%s\"", AltosLaunch.status_string(armed_status)));
242 igniter_status_label.setText(String.format("\"%s\"", AltosLaunch.status_string(igniter_status)));
245 void poll_launch_status() {
246 if (!getting_status && !firing && !armed) {
247 getting_status = true;
248 send_command("get_status");
258 send_command("quit");
264 void tick_arm_timer() {
265 if (armed_count > 0) {
267 if (armed_count <= 0) {
277 poll_launch_status();
281 if (arm.isSelected()) {
282 fire.setEnabled(true);
293 send_command("fire");
297 if (arm.isEnabled() && arm.isSelected() && armed_count > 0) {
300 fire_timer.restart();
311 radio_channel = Integer.parseInt(radio_channel_text.getText());
312 } catch (NumberFormatException ne) {
313 radio_channel_text.setText(String.format("%d", radio_channel));
319 launcher_serial = Integer.parseInt(launcher_serial_text.getText());
320 AltosUIPreferences.set_launcher_serial(launcher_serial);
321 send_command("set_remote");
322 } catch (NumberFormatException ne) {
323 launcher_serial_text.setText(String.format("%d", launcher_serial));
329 launcher_channel = Integer.parseInt(launcher_channel_text.getText());
330 AltosUIPreferences.set_launcher_serial(launcher_channel);
331 send_command("set_remote");
332 } catch (NumberFormatException ne) {
333 launcher_channel_text.setText(String.format("%d", launcher_channel));
337 public void actionPerformed(ActionEvent e) {
338 String cmd = e.getActionCommand();
339 if (cmd.equals("armed") || cmd.equals("igniter")) {
343 if (cmd.equals("arm"))
345 if (cmd.equals("tick_arm"))
347 if (cmd.equals("close"))
349 if (cmd.equals("fire_down"))
351 if (cmd.equals("fire_up"))
353 if (cmd.equals("tick_fire"))
355 if (cmd.equals("new_serial"))
357 if (cmd.equals("new_channel"))
361 /* A window listener to catch closing events and tell the config code */
362 class ConfigListener extends WindowAdapter {
365 public ConfigListener(AltosLaunchUI this_ui) {
369 public void windowClosing(WindowEvent e) {
370 ui.actionPerformed(new ActionEvent(e.getSource(),
371 ActionEvent.ACTION_PERFORMED,
376 private boolean open() {
377 command_queue = new LinkedBlockingQueue<String>();
379 device = AltosDeviceDialog.show(owner, Altos.product_any);
380 if (device != null) {
381 LaunchHandler handler = new LaunchHandler(owner);
382 Thread t = new Thread(handler);
389 public AltosLaunchUI(JFrame in_owner) {
391 launcher_channel = AltosUIPreferences.launcher_channel();
392 launcher_serial = AltosUIPreferences.launcher_serial();
394 armed_status = AltosLaunch.Unknown;
395 igniter_status = AltosLaunch.Unknown;
400 Container pane = getContentPane();
401 GridBagConstraints c = new GridBagConstraints();
402 Insets i = new Insets(4,4,4,4);
404 arm_timer = new javax.swing.Timer(arm_timeout, this);
405 arm_timer.setActionCommand("tick_arm");
408 fire_timer = new javax.swing.Timer(fire_timeout, this);
409 fire_timer.setActionCommand("tick_fire");
413 pane.setLayout(new GridBagLayout());
415 c.fill = GridBagConstraints.NONE;
416 c.anchor = GridBagConstraints.CENTER;
424 c.anchor = GridBagConstraints.CENTER;
425 label = new JLabel ("Launch Controller");
431 c.anchor = GridBagConstraints.WEST;
432 launcher_serial_label = new JLabel("Launcher Serial");
433 pane.add(launcher_serial_label, c);
438 c.anchor = GridBagConstraints.WEST;
439 launcher_serial_text = new JTextField(7);
440 launcher_serial_text.setText(String.format("%d", launcher_serial));
441 launcher_serial_text.setActionCommand("new_serial");
442 launcher_serial_text.addActionListener(this);
443 pane.add(launcher_serial_text, c);
448 c.anchor = GridBagConstraints.WEST;
449 launcher_channel_label = new JLabel("Launcher Channel");
450 pane.add(launcher_channel_label, c);
455 c.anchor = GridBagConstraints.WEST;
456 launcher_channel_text = new JTextField(7);
457 launcher_channel_text.setText(String.format("%d", launcher_channel));
458 launcher_channel_text.setActionCommand("new_channel");
459 launcher_channel_text.addActionListener(this);
460 pane.add(launcher_channel_text, c);
465 c.anchor = GridBagConstraints.WEST;
466 armed_label = new JLabel ("Armed");
467 pane.add(armed_label, c);
472 c.anchor = GridBagConstraints.WEST;
473 armed_status_label = new JLabel();
474 pane.add(armed_status_label, c);
479 c.anchor = GridBagConstraints.WEST;
480 igniter = new JLabel ("Igniter");
481 pane.add(igniter, c);
486 c.anchor = GridBagConstraints.WEST;
487 igniter_status_label = new JLabel();
488 pane.add(igniter_status_label, c);
493 c.anchor = GridBagConstraints.CENTER;
494 arm = new JToggleButton ("Arm");
496 arm.addActionListener(this);
497 arm.setActionCommand("arm");
498 arm.setEnabled(true);
503 c.anchor = GridBagConstraints.CENTER;
504 fire = new FireButton ("Fire");
505 fire.setEnabled(false);
507 fire.addActionListener(this);
508 fire.setActionCommand("fire");
511 setLocationRelativeTo(owner);
513 addWindowListener(new ConfigListener(this));