altosui: Fix Save flight data monitor layout, add cancel
authorKeith Packard <keithp@keithp.com>
Thu, 29 Jul 2010 02:37:02 +0000 (19:37 -0700)
committerKeith Packard <keithp@keithp.com>
Thu, 29 Jul 2010 02:37:02 +0000 (19:37 -0700)
Use GridBagLayout to improve the appearance of the flight data monitor
widget, add a cancel button to stop loading data (useful if the
connection is wedged).

Signed-off-by: Keith Packard <keithp@keithp.com>
ao-tools/altosui/AltosEeprom.java
ao-tools/altosui/AltosEepromMonitor.java

index 63698407fb3c9285ee75f4244355052fa05b1e6a..6162521f11435b2fbe1e9b14144fa08eb483c71c 100644 (file)
@@ -65,7 +65,7 @@ public class AltosEeprom implements Runnable {
        static final int ao_flight_landed = 8;
        static final int ao_flight_invalid = 9;
 
-       static String[] state_names = {
+       static final String[] state_names = {
                "startup",
                "idle",
                "pad",
@@ -78,7 +78,7 @@ public class AltosEeprom implements Runnable {
                "invalid",
        };
 
-       static int[] ParseHex(String line) {
+       int[] ParseHex(String line) {
                String[] tokens = line.split("\\s+");
                int[] array = new int[tokens.length];
 
@@ -91,20 +91,27 @@ public class AltosEeprom implements Runnable {
                return array;
        }
 
-       static int checksum(int[] line) {
+       int checksum(int[] line) {
                int     csum = 0x5a;
                for (int i = 1; i < line.length; i++)
                        csum += line[i];
                return csum & 0xff;
        }
 
-       static void FlushPending(FileWriter file, LinkedList<String> pending) throws IOException {
+       void FlushPending(FileWriter file, LinkedList<String> pending) throws IOException {
                while (!pending.isEmpty()) {
                        file.write(pending.remove());
                }
        }
 
-       static void CaptureLog(JFrame frame, AltosSerial serial_line) throws IOException, InterruptedException {
+       JFrame                  frame;
+       altos_device            device;
+       AltosSerial             serial_line;
+       boolean                 remote;
+       Thread                  eeprom_thread;
+       AltosEepromMonitor      monitor;
+
+       void CaptureLog() throws IOException, InterruptedException {
                int                     serial = 0;
                int                     block, state_block = 0;
                int                     addr;
@@ -117,7 +124,6 @@ public class AltosEeprom implements Runnable {
                FileWriter              eeprom_file = null;
                AltosFile               eeprom_name;
                LinkedList<String>      eeprom_pending = new LinkedList<String>();
-               AltosEepromMonitor      monitor = new AltosEepromMonitor(frame, ao_flight_boost, ao_flight_landed);
 
                serial_line.printf("v\n");
 
@@ -176,7 +182,6 @@ public class AltosEeprom implements Runnable {
 
                                        /* Monitor state transitions to update display */
                                        if (cmd == AO_LOG_STATE && a <= ao_flight_landed) {
-                                               System.out.printf ("%s\n", state_names[a]);
                                                if (a > ao_flight_pad)
                                                        want_file = true;
                                                if (a > state)
@@ -234,23 +239,23 @@ public class AltosEeprom implements Runnable {
                        eeprom_file.flush();
                        eeprom_file.close();
                }
-               monitor.done();
        }
 
-       JFrame          frame;
-       altos_device    device;
-       AltosSerial     serial_line;
-       boolean         remote;
-       Thread          eeprom_thread;
-
        public void run () {
                if (remote) {
                        serial_line.printf("m 0\n");
                        serial_line.set_channel(AltosPreferences.channel());
                        serial_line.printf("p\n");
                }
+
+               monitor = new AltosEepromMonitor(frame, ao_flight_boost, ao_flight_landed);
+               monitor.addActionListener(new ActionListener() {
+                               public void actionPerformed(ActionEvent e) {
+                                       eeprom_thread.interrupt();
+                               }
+                       });
                try {
-                       CaptureLog(frame, serial_line);
+                       CaptureLog();
                } catch (IOException ee) {
                        JOptionPane.showMessageDialog(frame,
                                                      device.getPath(),
@@ -260,12 +265,14 @@ public class AltosEeprom implements Runnable {
                }
                if (remote)
                        serial_line.printf("~");
+               monitor.done();
                serial_line.close();
        }
 
        public AltosEeprom(JFrame given_frame) {
                frame = given_frame;
                device = AltosDeviceDialog.show(frame, null);
+
                serial_line = new AltosSerial();
                remote = false;
 
index a7b1dd3ed11cacb35e64d25c309d28704b931e2e..9eb3f0d03abbfc4015eafad6ee71835d79cfd21c 100644 (file)
@@ -39,25 +39,78 @@ import altosui.AltosVoice;
 
 public class AltosEepromMonitor extends JDialog {
 
-       JPanel          panel;
+       Container       pane;
        Box             box;
        JLabel          serial_label;
        JLabel          flight_label;
        JLabel          file_label;
+       JLabel          serial_value;
+       JLabel          flight_value;
+       JLabel          file_value;
+       JButton         cancel;
        JProgressBar    pbar;
        int             min_state, max_state;
 
        public AltosEepromMonitor(JFrame owner, int in_min_state, int in_max_state) {
-               super (owner, "Download Flight Data");
+               super (owner, "Download Flight Data", false);
 
-               box = Box.createVerticalBox();
+               GridBagConstraints c;
+               Insets il = new Insets(4,0,4,4);
+               Insets ir = new Insets(4,4,4,0);
 
+               pane = getContentPane();
+               pane.setLayout(new GridBagLayout());
+
+               c = new GridBagConstraints();
+               c.gridx = 0; c.gridy = 0;
+               c.fill = GridBagConstraints.NONE;
+               c.anchor = GridBagConstraints.LINE_START;
+               c.insets = il;
                serial_label = new JLabel("Serial:");
-               box.add(serial_label);
+               pane.add(serial_label, c);
+
+               c = new GridBagConstraints();
+               c.gridx = 1; c.gridy = 0;
+               c.fill = GridBagConstraints.HORIZONTAL;
+               c.weightx = 1;
+               c.anchor = GridBagConstraints.LINE_START;
+               c.insets = ir;
+               serial_value = new JLabel("");
+               pane.add(serial_value, c);
+
+               c = new GridBagConstraints();
+               c.fill = GridBagConstraints.NONE;
+               c.gridx = 0; c.gridy = 1;
+               c.anchor = GridBagConstraints.LINE_START;
+               c.insets = il;
                flight_label = new JLabel("Flight:");
-               box.add(flight_label);
+               pane.add(flight_label, c);
+
+               c = new GridBagConstraints();
+               c.fill = GridBagConstraints.HORIZONTAL;
+               c.weightx = 1;
+               c.gridx = 1; c.gridy = 1;
+               c.anchor = GridBagConstraints.LINE_START;
+               c.insets = ir;
+               flight_value = new JLabel("");
+               pane.add(flight_value, c);
+
+               c = new GridBagConstraints();
+               c.fill = GridBagConstraints.NONE;
+               c.gridx = 0; c.gridy = 2;
+               c.anchor = GridBagConstraints.LINE_START;
+               c.insets = il;
                file_label = new JLabel("File:");
-               box.add(file_label);
+               pane.add(file_label, c);
+
+               c = new GridBagConstraints();
+               c.fill = GridBagConstraints.HORIZONTAL;
+               c.weightx = 1;
+               c.gridx = 1; c.gridy = 2;
+               c.anchor = GridBagConstraints.LINE_START;
+               c.insets = ir;
+               file_value = new JLabel("");
+               pane.add(file_value, c);
 
                min_state = in_min_state;
                max_state = in_max_state;
@@ -67,19 +120,35 @@ public class AltosEepromMonitor extends JDialog {
                pbar.setValue(0);
                pbar.setString("startup");
                pbar.setStringPainted(true);
-               box.add(pbar);
-
-               panel = new JPanel();
-               panel.add(box);
-
-               add(panel);
+               pbar.setPreferredSize(new Dimension(600, 20));
+               c = new GridBagConstraints();
+               c.fill = GridBagConstraints.HORIZONTAL;
+               c.anchor = GridBagConstraints.CENTER;
+               c.gridx = 0; c.gridy = 3;
+               c.gridwidth = GridBagConstraints.REMAINDER;
+               Insets ib = new Insets(4,0,4,0);
+               c.insets = ib;
+               pane.add(pbar, c);
+
+
+               cancel = new JButton("Cancel");
+               c = new GridBagConstraints();
+               c.fill = GridBagConstraints.NONE;
+               c.anchor = GridBagConstraints.CENTER;
+               c.gridx = 0; c.gridy = 4;
+               c.gridwidth = GridBagConstraints.REMAINDER;
+               Insets ic = new Insets(4,4,4,4);
+               c.insets = ic;
+               pane.add(cancel, c);
 
-               setMinimumSize(new Dimension(600, 0));
-               setContentPane(panel);
                pack();
                setVisible(true);
        }
 
+       public void addActionListener (ActionListener l) {
+               cancel.addActionListener(l);
+       }
+
        public void set_value(String state_name, int in_state, int in_block) {
                int block = in_block;
                int state = in_state;
@@ -92,23 +161,20 @@ public class AltosEepromMonitor extends JDialog {
 
                int pos = state * 100 + block;
 
-               System.out.printf ("State %s (%d + %d) = %d\n",
-                                  state_name, in_state, in_block, pos);
-
                pbar.setString(state_name);
                pbar.setValue(pos);
        }
 
        public void set_serial(int serial) {
-               serial_label.setText(String.format("Serial: %d", serial));
+               serial_value.setText(String.format("%d", serial));
        }
 
        public void set_flight(int flight) {
-               flight_label.setText(String.format("Flight: %d", flight));
+               flight_value.setText(String.format("%d", flight));
        }
 
        public void set_file(String file) {
-               file_label.setText(String.format("File: %s", file));
+               file_value.setText(String.format("%s", file));
        }
 
        public void done() {