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.*;
26 import org.altusmetrum.altosuilib_10.*;
28 class FireButton extends JButton {
29 protected void processMouseEvent(MouseEvent e) {
30 super.processMouseEvent(e);
32 case MouseEvent.MOUSE_PRESSED:
33 if (actionListener != null)
34 actionListener.actionPerformed(new ActionEvent(this, e.getID(), "fire_down"));
36 case MouseEvent.MOUSE_RELEASED:
37 if (actionListener != null)
38 actionListener.actionPerformed(new ActionEvent(this, e.getID(), "fire_up"));
43 public FireButton(String s) {
48 public class AltosLaunchUI
50 implements ActionListener
57 JLabel radio_channel_label;
58 JTextField radio_channel_text;
61 JLabel launcher_serial_label;
62 JTextField launcher_serial_text;
65 JLabel launcher_channel_label;
66 JTextField launcher_channel_text;
69 JLabel armed_status_label;
71 JLabel igniter_status_label;
74 javax.swing.Timer arm_timer;
75 javax.swing.Timer fire_timer;
83 final static int arm_timeout = 1 * 1000;
84 final static int fire_timeout = 250;
88 LinkedBlockingQueue<String> command_queue;
90 class LaunchHandler implements Runnable {
94 void send_exception(Exception e) {
95 final Exception f_e = e;
96 Runnable r = new Runnable() {
98 launch_exception(f_e);
101 SwingUtilities.invokeLater(r);
106 launch = new AltosLaunch(device);
107 } catch (Exception e) {
111 launch.set_frame(owner);
112 launch.set_remote(launcher_serial, launcher_channel);
118 String command = command_queue.take();
121 if (command.equals("get_status")) {
124 armed_status = launch.armed;
125 igniter_status = launch.igniter;
127 } else if (command.equals("set_remote")) {
128 launch.set_remote(launcher_serial, launcher_channel);
129 reply = "remote set";
130 } else if (command.equals("arm")) {
133 } else if (command.equals("fire")) {
136 } else if (command.equals("quit")) {
140 throw new ParseException(String.format("invalid command %s", command), 0);
142 final String f_reply = reply;
145 launch_reply(f_reply);
148 SwingUtilities.invokeLater(r);
149 } catch (Exception e) {
155 public LaunchHandler(JFrame in_owner) {
160 void launch_exception(Exception e) {
161 if (e instanceof FileNotFoundException) {
162 JOptionPane.showMessageDialog(owner,
163 ((FileNotFoundException) e).getMessage(),
164 "Cannot open target device",
165 JOptionPane.ERROR_MESSAGE);
166 } else if (e instanceof AltosSerialInUseException) {
167 JOptionPane.showMessageDialog(owner,
168 String.format("Device \"%s\" already in use",
169 device.toShortString()),
171 JOptionPane.ERROR_MESSAGE);
172 } else if (e instanceof IOException) {
173 IOException ee = (IOException) e;
174 JOptionPane.showMessageDialog(owner,
175 device.toShortString(),
176 ee.getLocalizedMessage(),
177 JOptionPane.ERROR_MESSAGE);
179 JOptionPane.showMessageDialog(owner,
180 String.format("Connection to \"%s\" failed",
181 device.toShortString()),
183 JOptionPane.ERROR_MESSAGE);
188 void launch_reply(String reply) {
191 if (reply.equals("remote set"))
192 poll_launch_status();
193 if (reply.equals("status")) {
198 void set_arm_text() {
199 if (arm.isSelected())
200 arm.setText(String.format("%d", armed_count));
205 void start_arm_timer() {
210 void stop_arm_timer() {
213 arm.setSelected(false);
214 fire.setEnabled(false);
219 fire.setEnabled(false);
224 void send_command(String command) {
226 command_queue.put(command);
227 } catch (Exception ex) {
228 launch_exception(ex);
232 boolean getting_status = false;
234 void set_launch_status() {
235 getting_status = false;
236 armed_status_label.setText(String.format("\"%s\"", AltosLaunch.status_string(armed_status)));
237 igniter_status_label.setText(String.format("\"%s\"", AltosLaunch.status_string(igniter_status)));
240 void poll_launch_status() {
241 if (!getting_status && !firing && !armed) {
242 getting_status = true;
243 send_command("get_status");
253 send_command("quit");
259 void tick_arm_timer() {
260 if (armed_count > 0) {
262 if (armed_count <= 0) {
272 poll_launch_status();
276 if (arm.isSelected()) {
277 fire.setEnabled(true);
288 send_command("fire");
292 if (arm.isEnabled() && arm.isSelected() && armed_count > 0) {
295 fire_timer.restart();
306 radio_channel = Integer.parseInt(radio_channel_text.getText());
307 } catch (NumberFormatException ne) {
308 radio_channel_text.setText(String.format("%d", radio_channel));
314 launcher_serial = Integer.parseInt(launcher_serial_text.getText());
315 AltosUIPreferences.set_launcher_serial(launcher_serial);
316 send_command("set_remote");
317 } catch (NumberFormatException ne) {
318 launcher_serial_text.setText(String.format("%d", launcher_serial));
324 launcher_channel = Integer.parseInt(launcher_channel_text.getText());
325 AltosUIPreferences.set_launcher_serial(launcher_channel);
326 send_command("set_remote");
327 } catch (NumberFormatException ne) {
328 launcher_channel_text.setText(String.format("%d", launcher_channel));
332 public void actionPerformed(ActionEvent e) {
333 String cmd = e.getActionCommand();
334 if (cmd.equals("armed") || cmd.equals("igniter")) {
338 if (cmd.equals("arm"))
340 if (cmd.equals("tick_arm"))
342 if (cmd.equals("close"))
344 if (cmd.equals("fire_down"))
346 if (cmd.equals("fire_up"))
348 if (cmd.equals("tick_fire"))
350 if (cmd.equals("new_serial"))
352 if (cmd.equals("new_channel"))
356 /* A window listener to catch closing events and tell the config code */
357 class ConfigListener extends WindowAdapter {
360 public ConfigListener(AltosLaunchUI this_ui) {
364 public void windowClosing(WindowEvent e) {
365 ui.actionPerformed(new ActionEvent(e.getSource(),
366 ActionEvent.ACTION_PERFORMED,
371 private boolean open() {
372 command_queue = new LinkedBlockingQueue<String>();
374 device = AltosDeviceUIDialog.show(owner, Altos.product_any);
375 if (device != null) {
376 LaunchHandler handler = new LaunchHandler(owner);
377 Thread t = new Thread(handler);
384 public AltosLaunchUI(JFrame in_owner) {
386 launcher_channel = AltosUIPreferences.launcher_channel();
387 launcher_serial = AltosUIPreferences.launcher_serial();
389 armed_status = AltosLaunch.Unknown;
390 igniter_status = AltosLaunch.Unknown;
395 Container pane = getContentPane();
396 GridBagConstraints c = new GridBagConstraints();
397 Insets i = new Insets(4,4,4,4);
399 arm_timer = new javax.swing.Timer(arm_timeout, this);
400 arm_timer.setActionCommand("tick_arm");
403 fire_timer = new javax.swing.Timer(fire_timeout, this);
404 fire_timer.setActionCommand("tick_fire");
408 pane.setLayout(new GridBagLayout());
410 c.fill = GridBagConstraints.NONE;
411 c.anchor = GridBagConstraints.CENTER;
419 c.anchor = GridBagConstraints.CENTER;
420 label = new JLabel ("Launch Controller");
426 c.anchor = GridBagConstraints.WEST;
427 launcher_serial_label = new JLabel("Launcher Serial");
428 pane.add(launcher_serial_label, c);
433 c.anchor = GridBagConstraints.WEST;
434 launcher_serial_text = new JTextField(7);
435 launcher_serial_text.setText(String.format("%d", launcher_serial));
436 launcher_serial_text.setActionCommand("new_serial");
437 launcher_serial_text.addActionListener(this);
438 pane.add(launcher_serial_text, c);
443 c.anchor = GridBagConstraints.WEST;
444 launcher_channel_label = new JLabel("Launcher Channel");
445 pane.add(launcher_channel_label, c);
450 c.anchor = GridBagConstraints.WEST;
451 launcher_channel_text = new JTextField(7);
452 launcher_channel_text.setText(String.format("%d", launcher_channel));
453 launcher_channel_text.setActionCommand("new_channel");
454 launcher_channel_text.addActionListener(this);
455 pane.add(launcher_channel_text, c);
460 c.anchor = GridBagConstraints.WEST;
461 armed_label = new JLabel ("Armed");
462 pane.add(armed_label, c);
467 c.anchor = GridBagConstraints.WEST;
468 armed_status_label = new JLabel();
469 pane.add(armed_status_label, c);
474 c.anchor = GridBagConstraints.WEST;
475 igniter = new JLabel ("Igniter");
476 pane.add(igniter, c);
481 c.anchor = GridBagConstraints.WEST;
482 igniter_status_label = new JLabel();
483 pane.add(igniter_status_label, c);
488 c.anchor = GridBagConstraints.CENTER;
489 arm = new JToggleButton ("Arm");
491 arm.addActionListener(this);
492 arm.setActionCommand("arm");
493 arm.setEnabled(true);
498 c.anchor = GridBagConstraints.CENTER;
499 fire = new FireButton ("Fire");
500 fire.setEnabled(false);
502 fire.addActionListener(this);
503 fire.setActionCommand("fire");
506 setLocationRelativeTo(owner);
508 addWindowListener(new ConfigListener(this));