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 class FireButton extends JButton {
33 protected void processMouseEvent(MouseEvent e) {
34 super.processMouseEvent(e);
36 case MouseEvent.MOUSE_PRESSED:
37 if (actionListener != null)
38 actionListener.actionPerformed(new ActionEvent(this, e.getID(), "fire_down"));
40 case MouseEvent.MOUSE_RELEASED:
41 if (actionListener != null)
42 actionListener.actionPerformed(new ActionEvent(this, e.getID(), "fire_up"));
47 public FireButton(String s) {
52 public class AltosLaunchUI
54 implements ActionListener
61 JLabel radio_channel_label;
62 JTextField radio_channel_text;
65 JLabel launcher_serial_label;
66 JTextField launcher_serial_text;
69 JLabel launcher_channel_label;
70 JTextField launcher_channel_text;
73 JLabel armed_status_label;
75 JLabel igniter_status_label;
78 javax.swing.Timer arm_timer;
79 javax.swing.Timer fire_timer;
87 final static int arm_timeout = 1 * 1000;
88 final static int fire_timeout = 250;
92 LinkedBlockingQueue<String> command_queue;
94 class LaunchHandler implements Runnable {
98 void send_exception(Exception e) {
99 final Exception f_e = e;
100 Runnable r = new Runnable() {
102 launch_exception(f_e);
105 SwingUtilities.invokeLater(r);
110 launch = new AltosLaunch(device);
111 } catch (Exception e) {
115 launch.set_frame(owner);
116 launch.set_remote(launcher_serial, launcher_channel);
122 String command = command_queue.take();
125 if (command.equals("get_status")) {
128 armed_status = launch.armed;
129 igniter_status = launch.igniter;
131 } else if (command.equals("set_remote")) {
132 launch.set_remote(launcher_serial, launcher_channel);
133 reply = "remote set";
134 } else if (command.equals("arm")) {
137 } else if (command.equals("fire")) {
140 } else if (command.equals("quit")) {
144 throw new ParseException(String.format("invalid command %s", command), 0);
146 final String f_reply = reply;
149 launch_reply(f_reply);
152 SwingUtilities.invokeLater(r);
153 } catch (Exception e) {
159 public LaunchHandler(JFrame in_owner) {
164 void launch_exception(Exception e) {
165 if (e instanceof FileNotFoundException) {
166 JOptionPane.showMessageDialog(owner,
167 ((FileNotFoundException) e).getMessage(),
168 "Cannot open target device",
169 JOptionPane.ERROR_MESSAGE);
170 } else if (e instanceof AltosSerialInUseException) {
171 JOptionPane.showMessageDialog(owner,
172 String.format("Device \"%s\" already in use",
173 device.toShortString()),
175 JOptionPane.ERROR_MESSAGE);
176 } else if (e instanceof IOException) {
177 IOException ee = (IOException) e;
178 JOptionPane.showMessageDialog(owner,
179 device.toShortString(),
180 ee.getLocalizedMessage(),
181 JOptionPane.ERROR_MESSAGE);
183 JOptionPane.showMessageDialog(owner,
184 String.format("Connection to \"%s\" failed",
185 device.toShortString()),
187 JOptionPane.ERROR_MESSAGE);
192 void launch_reply(String reply) {
195 if (reply.equals("remote set"))
196 poll_launch_status();
197 if (reply.equals("status")) {
202 void set_arm_text() {
203 if (arm.isSelected())
204 arm.setText(String.format("%d", armed_count));
209 void start_arm_timer() {
214 void stop_arm_timer() {
217 arm.setSelected(false);
218 fire.setEnabled(false);
223 fire.setEnabled(false);
228 void send_command(String command) {
230 command_queue.put(command);
231 } catch (Exception ex) {
232 launch_exception(ex);
236 boolean getting_status = false;
238 void set_launch_status() {
239 getting_status = false;
240 armed_status_label.setText(String.format("\"%s\"", AltosLaunch.status_string(armed_status)));
241 igniter_status_label.setText(String.format("\"%s\"", AltosLaunch.status_string(igniter_status)));
244 void poll_launch_status() {
245 if (!getting_status && !firing && !armed) {
246 getting_status = true;
247 send_command("get_status");
257 send_command("quit");
263 void tick_arm_timer() {
264 if (armed_count > 0) {
266 if (armed_count <= 0) {
276 poll_launch_status();
280 if (arm.isSelected()) {
281 fire.setEnabled(true);
292 send_command("fire");
296 if (arm.isEnabled() && arm.isSelected() && armed_count > 0) {
299 fire_timer.restart();
310 radio_channel = Integer.parseInt(radio_channel_text.getText());
311 } catch (NumberFormatException ne) {
312 radio_channel_text.setText(String.format("%d", radio_channel));
318 launcher_serial = Integer.parseInt(launcher_serial_text.getText());
319 AltosPreferences.set_launcher_serial(launcher_serial);
320 send_command("set_remote");
321 } catch (NumberFormatException ne) {
322 launcher_serial_text.setText(String.format("%d", launcher_serial));
328 launcher_channel = Integer.parseInt(launcher_channel_text.getText());
329 AltosPreferences.set_launcher_serial(launcher_channel);
330 send_command("set_remote");
331 } catch (NumberFormatException ne) {
332 launcher_channel_text.setText(String.format("%d", launcher_channel));
336 public void actionPerformed(ActionEvent e) {
337 String cmd = e.getActionCommand();
338 System.out.printf("cmd %s\n", cmd);
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 = AltosPreferences.launcher_channel();
392 launcher_serial = AltosPreferences.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));