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 String.format("Cannot open device \"%s\"",
168 device.toShortString()),
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 AltosPreferences.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 AltosPreferences.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 System.out.printf("cmd %s\n", cmd);
340 if (cmd.equals("armed") || cmd.equals("igniter")) {
344 if (cmd.equals("arm"))
346 if (cmd.equals("tick_arm"))
348 if (cmd.equals("close"))
350 if (cmd.equals("fire_down"))
352 if (cmd.equals("fire_up"))
354 if (cmd.equals("tick_fire"))
356 if (cmd.equals("new_serial"))
358 if (cmd.equals("new_channel"))
362 /* A window listener to catch closing events and tell the config code */
363 class ConfigListener extends WindowAdapter {
366 public ConfigListener(AltosLaunchUI this_ui) {
370 public void windowClosing(WindowEvent e) {
371 ui.actionPerformed(new ActionEvent(e.getSource(),
372 ActionEvent.ACTION_PERFORMED,
377 private boolean open() {
378 command_queue = new LinkedBlockingQueue<String>();
380 device = AltosDeviceDialog.show(owner, Altos.product_any);
381 if (device != null) {
382 LaunchHandler handler = new LaunchHandler(owner);
383 Thread t = new Thread(handler);
390 public AltosLaunchUI(JFrame in_owner) {
392 launcher_channel = AltosPreferences.launcher_channel();
393 launcher_serial = AltosPreferences.launcher_serial();
395 armed_status = AltosLaunch.Unknown;
396 igniter_status = AltosLaunch.Unknown;
401 Container pane = getContentPane();
402 GridBagConstraints c = new GridBagConstraints();
403 Insets i = new Insets(4,4,4,4);
405 arm_timer = new javax.swing.Timer(arm_timeout, this);
406 arm_timer.setActionCommand("tick_arm");
409 fire_timer = new javax.swing.Timer(fire_timeout, this);
410 fire_timer.setActionCommand("tick_fire");
414 pane.setLayout(new GridBagLayout());
416 c.fill = GridBagConstraints.NONE;
417 c.anchor = GridBagConstraints.CENTER;
425 c.anchor = GridBagConstraints.CENTER;
426 label = new JLabel ("Launch Controller");
432 c.anchor = GridBagConstraints.WEST;
433 launcher_serial_label = new JLabel("Launcher Serial");
434 pane.add(launcher_serial_label, c);
439 c.anchor = GridBagConstraints.WEST;
440 launcher_serial_text = new JTextField(7);
441 launcher_serial_text.setText(String.format("%d", launcher_serial));
442 launcher_serial_text.setActionCommand("new_serial");
443 launcher_serial_text.addActionListener(this);
444 pane.add(launcher_serial_text, c);
449 c.anchor = GridBagConstraints.WEST;
450 launcher_channel_label = new JLabel("Launcher Channel");
451 pane.add(launcher_channel_label, c);
456 c.anchor = GridBagConstraints.WEST;
457 launcher_channel_text = new JTextField(7);
458 launcher_channel_text.setText(String.format("%d", launcher_channel));
459 launcher_channel_text.setActionCommand("new_channel");
460 launcher_channel_text.addActionListener(this);
461 pane.add(launcher_channel_text, c);
466 c.anchor = GridBagConstraints.WEST;
467 armed_label = new JLabel ("Armed");
468 pane.add(armed_label, c);
473 c.anchor = GridBagConstraints.WEST;
474 armed_status_label = new JLabel();
475 pane.add(armed_status_label, c);
480 c.anchor = GridBagConstraints.WEST;
481 igniter = new JLabel ("Igniter");
482 pane.add(igniter, c);
487 c.anchor = GridBagConstraints.WEST;
488 igniter_status_label = new JLabel();
489 pane.add(igniter_status_label, c);
494 c.anchor = GridBagConstraints.CENTER;
495 arm = new JToggleButton ("Arm");
497 arm.addActionListener(this);
498 arm.setActionCommand("arm");
499 arm.setEnabled(true);
504 c.anchor = GridBagConstraints.CENTER;
505 fire = new FireButton ("Fire");
506 fire.setEnabled(false);
508 fire.addActionListener(this);
509 fire.setActionCommand("fire");
512 setLocationRelativeTo(owner);
514 addWindowListener(new ConfigListener(this));