+ int poll_remaining;
+
+ LinkedBlockingQueue<String> command_queue;
+
+ class Igniter {
+ JRadioButton button;
+ JLabel status_label;
+ String name;
+ int status;
+
+ void set_status (int status) {
+ this.status = status;
+ status_label.setText(String.format("\"%s\"", AltosIgnite.status_string(status)));
+ }
+
+ Igniter(AltosIgniteUI ui, String label, String name, int y) {
+ Container pane = getContentPane();
+ GridBagConstraints c = new GridBagConstraints();
+ Insets i = new Insets(4,4,4,4);
+
+ this.name = name;
+ this.status = AltosIgnite.Unknown;
+
+ c.gridx = 0;
+ c.gridy = y;
+ c.gridwidth = 1;
+ c.anchor = GridBagConstraints.WEST;
+ button = new JRadioButton (label);
+ pane.add(button, c);
+ button.addActionListener(ui);
+ button.setActionCommand(name);
+ group.add(button);
+
+ c.gridx = 1;
+ c.gridy = y;
+ c.gridwidth = 1;
+ c.anchor = GridBagConstraints.WEST;
+ status_label = new JLabel("plenty of text");
+ pane.add(status_label, c);
+
+ status = AltosIgnite.Unknown;
+ }
+ }
+
+ Igniter igniters[];
+
+ void set_status(String _name, int _status) {
+
+ final String name = _name;
+ final int status = _status;
+ Runnable r = new Runnable() {
+ public void run() {
+ for (int p = 0; p < igniters.length; p++)
+ if (name.equals(igniters[p].name))
+ igniters[p].set_status(status);
+ }
+ };
+ SwingUtilities.invokeLater(r);
+ }
+
+ class IgniteHandler implements Runnable {
+ AltosIgnite ignite;
+ JFrame owner;
+ AltosLink link;
+
+ void send_exception(Exception e) {
+ final Exception f_e = e;
+ Runnable r = new Runnable() {
+ public void run() {
+ ignite_exception(f_e);
+ }
+ };
+ SwingUtilities.invokeLater(r);
+ }
+
+ public void run () {
+ try {
+ ignite = new AltosIgnite(link,
+ !device.matchProduct(Altos.product_altimeter));
+
+ } catch (Exception e) {
+ send_exception(e);
+ return;
+ }
+
+ for (;;) {
+ Runnable r;
+
+ try {
+ String command = command_queue.take();
+ String reply = null;
+
+ if (command.equals("get_status")) {
+ HashMap<String,Integer> status_map = ignite.status();
+
+ for (int p = 0; p < igniters.length; p++) {
+ Integer i = status_map.get(igniters[p].name);
+ if (i != null)
+ set_status(igniters[p].name, i);
+ }
+ reply = "status";
+ } else if (command.equals("get_npyro")) {
+ reply = String.format("npyro %d", ignite.npyro());
+ } else if (command.equals("quit")) {
+ ignite.close();
+ break;
+ } else {
+ ignite.fire(command);
+ reply = "fired";
+ }
+ final String f_reply = reply;
+ r = new Runnable() {
+ public void run() {
+ ignite_reply(f_reply);
+ }
+ };
+ SwingUtilities.invokeLater(r);
+ } catch (Exception e) {
+ send_exception(e);
+ }
+ }
+ }
+
+ public IgniteHandler(JFrame in_owner, AltosLink in_link) {
+ owner = in_owner;
+ link = in_link;
+ }
+ }
+
+ void ignite_exception(Exception e) {
+ if (e instanceof FileNotFoundException) {
+ JOptionPane.showMessageDialog(owner,
+ ((FileNotFoundException) e).getMessage(),
+ "Cannot open target device",
+ JOptionPane.ERROR_MESSAGE);
+ } else if (e instanceof AltosSerialInUseException) {
+ JOptionPane.showMessageDialog(owner,
+ String.format("Device \"%s\" already in use",
+ device.toShortString()),
+ "Device in use",
+ JOptionPane.ERROR_MESSAGE);
+ } else if (e instanceof IOException) {
+ IOException ee = (IOException) e;
+ JOptionPane.showMessageDialog(owner,
+ device.toShortString(),
+ ee.getLocalizedMessage(),
+ JOptionPane.ERROR_MESSAGE);
+ } else {
+ JOptionPane.showMessageDialog(owner,
+ String.format("Connection to \"%s\" failed",
+ device.toShortString()),
+ "Connection Failed",
+ JOptionPane.ERROR_MESSAGE);
+ }
+ close();
+ }
+
+ void ignite_reply(String reply) {
+ if (reply.equals("status")) {
+ set_ignite_status();
+ } else if (reply.equals("fired")) {
+ fired();
+ } else if (reply.startsWith("npyro")) {
+ npyro = Integer.parseInt(reply.substring(6));
+ make_ui();
+ }
+ }
+