altosui: switch channel selector to combo box. Shorten displayed device names
authorKeith Packard <keithp@keithp.com>
Sat, 20 Nov 2010 06:44:48 +0000 (22:44 -0800)
committerKeith Packard <keithp@keithp.com>
Sat, 20 Nov 2010 06:48:11 +0000 (22:48 -0800)
A combo box displays the current value, which is quite nice to
have. Add a 'toShortString' for AltosDevice so that the window frames
and error messages don't have extra spaces generated by the
altos_device toString method.

Signed-off-by: Keith Packard <keithp@keithp.com>
ao-tools/altosui/AltosChannelMenu.java
ao-tools/altosui/AltosConfig.java
ao-tools/altosui/AltosDebug.java
ao-tools/altosui/AltosDevice.java
ao-tools/altosui/AltosEepromDownload.java
ao-tools/altosui/AltosFlashUI.java
ao-tools/altosui/AltosFlightUI.java
ao-tools/altosui/AltosIgniteUI.java
ao-tools/altosui/AltosSerial.java
ao-tools/altosui/AltosTelemetryReader.java
ao-tools/altosui/AltosUI.java

index 504c13c6273c5ee366b56313463c4ea5a7e4ba2e..8069c8531b1c71f9a5802be9c1e3a94d23a4c352 100644 (file)
@@ -28,8 +28,7 @@ import java.text.*;
 import java.util.prefs.*;
 import java.util.concurrent.LinkedBlockingQueue;
 
-public class AltosChannelMenu extends JMenu implements ActionListener {
-       ButtonGroup                     group;
+public class AltosChannelMenu extends JComboBox implements ActionListener {
        int                             channel;
        LinkedList<ActionListener>      listeners;
 
@@ -38,33 +37,28 @@ public class AltosChannelMenu extends JMenu implements ActionListener {
        }
 
        public void actionPerformed(ActionEvent e) {
-               channel = Integer.parseInt(e.getActionCommand());
+               channel = getSelectedIndex();
+
+               ActionEvent newe = new ActionEvent(this, channel, e.getActionCommand());
 
                ListIterator<ActionListener>    i = listeners.listIterator();
 
-               ActionEvent newe = new ActionEvent(this, channel, e.getActionCommand());
                while (i.hasNext()) {
                        ActionListener  listener = i.next();
                        listener.actionPerformed(newe);
                }
+               setMaximumSize(getPreferredSize());
        }
 
        public AltosChannelMenu(int current_channel) {
-               super("Channel", true);
-               group = new ButtonGroup();
 
                channel = current_channel;
 
                listeners = new LinkedList<ActionListener>();
-               for (int c = 0; c <= 9; c++) {
-                       JRadioButtonMenuItem radioitem = new JRadioButtonMenuItem(String.format("Channel %1d (%7.3fMHz)", c,
-                                                                                               434.550 + c * 0.1),
-                                                            c == channel);
-                       radioitem.setActionCommand(String.format("%d", c));
-                       radioitem.addActionListener(this);
-                       add(radioitem);
-                       group.add(radioitem);
-               }
+               for (int c = 0; c <= 9; c++)
+                       addItem(String.format("Channel %1d (%7.3fMHz)", c, 434.550 + c * 0.1));
+               setSelectedIndex(channel);
+               setMaximumRowCount(10);
        }
 
 }
index 19503dcbe78e5f89c8fcf3fabfa5296f4ffb8188..6bda20d804135880b7511f8b4e89bba806fa3d42 100644 (file)
@@ -158,7 +158,7 @@ public class AltosConfig implements Runnable, ActionListener {
        void abort() {
                JOptionPane.showMessageDialog(owner,
                                              String.format("Connection to \"%s\" failed",
-                                                           device.toString()),
+                                                           device.toShortString()),
                                              "Connection Failed",
                                              JOptionPane.ERROR_MESSAGE);
                serial_line.close();
@@ -266,18 +266,18 @@ public class AltosConfig implements Runnable, ActionListener {
                        } catch (FileNotFoundException ee) {
                                JOptionPane.showMessageDialog(owner,
                                                              String.format("Cannot open device \"%s\"",
-                                                                           device.toString()),
+                                                                           device.toShortString()),
                                                              "Cannot open target device",
                                                              JOptionPane.ERROR_MESSAGE);
                        } catch (AltosSerialInUseException si) {
                                JOptionPane.showMessageDialog(owner,
                                                              String.format("Device \"%s\" already in use",
-                                                                           device.toString()),
+                                                                           device.toShortString()),
                                                              "Device in use",
                                                              JOptionPane.ERROR_MESSAGE);
                        } catch (IOException ee) {
                                JOptionPane.showMessageDialog(owner,
-                                                             device.toString(),
+                                                             device.toShortString(),
                                                              ee.getLocalizedMessage(),
                                                              JOptionPane.ERROR_MESSAGE);
                        }
index 9aa35d3f2f834813d7cfdd51a5dbf2a631b750ee..8d435b667d5ecb584b118bed27a5d9945b4abb92 100644 (file)
@@ -261,7 +261,7 @@ public class AltosDebug extends AltosSerial {
                printf ("R\n");
        }
 
-       public AltosDebug (altos_device in_device) throws FileNotFoundException, AltosSerialInUseException {
+       public AltosDebug (AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
                super(in_device);
        }
 }
\ No newline at end of file
index f646305b3dc178da98433012ef24adabe1cb8136..f0fda57bc0b825c71d4af0dfa08180e4b3d4899e 100644 (file)
@@ -101,6 +101,15 @@ public class AltosDevice extends altos_device {
                                     getName(), getSerial(), getPath());
        }
 
+       public String toShortString() {
+               String  name = getName();
+               if (name == null)
+                       name = "Altus Metrum";
+               return String.format("%s %d %s",
+                                    name, getSerial(), getPath());
+
+       }
+
        public boolean isAltusMetrum() {
                if (getVendor() != vendor_altusmetrum)
                        return false;
index 912ff476df8bbb69013db1d872f06384da09ebd8..fb5dcfc0db06574611cc00b4f277f6be6a5c9ca5 100644 (file)
@@ -232,14 +232,14 @@ public class AltosEepromDownload implements Runnable {
                        CaptureLog();
                } catch (IOException ee) {
                        JOptionPane.showMessageDialog(frame,
-                                                     device.toString(),
+                                                     device.toShortString(),
                                                      ee.getLocalizedMessage(),
                                                      JOptionPane.ERROR_MESSAGE);
                } catch (InterruptedException ie) {
                } catch (TimeoutException te) {
                        JOptionPane.showMessageDialog(frame,
                                                      String.format("Connection to \"%s\" failed",
-                                                                   device.toString()),
+                                                                   device.toShortString()),
                                                      "Connection Failed",
                                                      JOptionPane.ERROR_MESSAGE);
                }
@@ -266,18 +266,18 @@ public class AltosEepromDownload implements Runnable {
                        } catch (FileNotFoundException ee) {
                                JOptionPane.showMessageDialog(frame,
                                                              String.format("Cannot open device \"%s\"",
-                                                                           device.toString()),
+                                                                           device.toShortString()),
                                                              "Cannot open target device",
                                                              JOptionPane.ERROR_MESSAGE);
                        } catch (AltosSerialInUseException si) {
                                JOptionPane.showMessageDialog(frame,
                                                              String.format("Device \"%s\" already in use",
-                                                                           device.toString()),
+                                                                           device.toShortString()),
                                                              "Device in use",
                                                              JOptionPane.ERROR_MESSAGE);
                        } catch (IOException ee) {
                                JOptionPane.showMessageDialog(frame,
-                                                             device.toString(),
+                                                             device.toShortString(),
                                                              ee.getLocalizedMessage(),
                                                              JOptionPane.ERROR_MESSAGE);
                        }
index d3b72c67737b507fd6274429614cc6f1641a1138..f63097ac408ad1f71356311365b487203ce8788f 100644 (file)
@@ -90,7 +90,7 @@ public class AltosFlashUI
                } catch (AltosSerialInUseException si) {
                        JOptionPane.showMessageDialog(frame,
                                                      String.format("Device \"%s\" already in use",
-                                                                   debug_dongle.toString()),
+                                                                   debug_dongle.toShortString()),
                                                      "Device in use",
                                                      JOptionPane.ERROR_MESSAGE);
                } catch (IOException e) {
index 78b005c0908656158496e3fe0f235c0c82651a1f..56ab7ebc4ac01546d210b8446cb7c010d5816f9b 100644 (file)
@@ -36,8 +36,6 @@ public class AltosFlightUI extends JFrame implements AltosFlightDisplay {
        AltosFlightReader       reader;
        AltosDisplayThread      thread;
 
-       private Box vbox;
-
        JTabbedPane     pane;
 
        AltosPad        pad;
@@ -128,22 +126,47 @@ public class AltosFlightUI extends JFrame implements AltosFlightDisplay {
                exit_on_close = true;
        }
 
+       Container       bag;
+
        public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
                AltosPreferences.init(this);
 
                voice = in_voice;
                reader = in_reader;
 
+               bag = getContentPane();
+               bag.setLayout(new GridBagLayout());
+
+               GridBagConstraints c = new GridBagConstraints();
+
                java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
                if (imgURL != null)
                        setIconImage(new ImageIcon(imgURL).getImage());
 
                setTitle(String.format("AltOS %s", reader.name));
 
-               flightStatus = new AltosFlightStatus();
+               if (serial >= 0) {
+                       // Channel menu
+                       JComboBox channels = new AltosChannelMenu(AltosPreferences.channel(serial));
+                       channels.addActionListener(new ActionListener() {
+                                       public void actionPerformed(ActionEvent e) {
+                                               int channel = Integer.parseInt(e.getActionCommand());
+                                               reader.set_channel(channel);
+                                               AltosPreferences.set_channel(serial, channel);
+                                       }
+                               });
+                       c.gridx = 0;
+                       c.gridy = 0;
+                       c.anchor = GridBagConstraints.WEST;
+                       bag.add (channels, c);
+               }
 
-               vbox = new Box (BoxLayout.Y_AXIS);
-               vbox.add(flightStatus);
+               flightStatus = new AltosFlightStatus();
+               c.gridx = 0;
+               c.gridy = 1;
+               c.fill = GridBagConstraints.HORIZONTAL;
+               c.weightx = 1;
+               bag.add(flightStatus, c);
 
                pane = new JTabbedPane();
 
@@ -163,29 +186,12 @@ public class AltosFlightUI extends JFrame implements AltosFlightDisplay {
                flightInfoPane = new JScrollPane(flightInfo.box());
                pane.add("Table", flightInfoPane);
 
-               vbox.add(pane);
-
-               this.add(vbox);
-
-               if (serial >= 0) {
-                       JMenuBar menubar = new JMenuBar();
-
-                       // Channel menu
-                       {
-                               JMenu menu = new AltosChannelMenu(AltosPreferences.channel(serial));
-                               menu.addActionListener(new ActionListener() {
-                                               public void actionPerformed(ActionEvent e) {
-                                                       int channel = Integer.parseInt(e.getActionCommand());
-                                                       reader.set_channel(channel);
-                                                       AltosPreferences.set_channel(serial, channel);
-                                               }
-                                       });
-                               menu.setMnemonic(KeyEvent.VK_C);
-                               menubar.add(menu);
-                       }
-
-                       this.setJMenuBar(menubar);
-               }
+               c.gridx = 0;
+               c.gridy = 2;
+               c.fill = GridBagConstraints.BOTH;
+               c.weightx = 1;
+               c.weighty = 1;
+               bag.add(pane, c);
 
                this.setSize(this.getPreferredSize());
                this.validate();
index caecc3ef8fd7d392ec12cceac33ace39897089a2..0207e39fe182d2519e36b1d0c8fcdcaa1758d864 100644 (file)
@@ -103,7 +103,7 @@ public class AltosIgniteUI
                close();
                JOptionPane.showMessageDialog(owner,
                                              String.format("Connection to \"%s\" failed",
-                                                           device.toString()),
+                                                           device.toShortString()),
                                              "Connection Failed",
                                              JOptionPane.ERROR_MESSAGE);
        }
@@ -194,18 +194,18 @@ public class AltosIgniteUI
                        } catch (FileNotFoundException ee) {
                                JOptionPane.showMessageDialog(owner,
                                                              String.format("Cannot open device \"%s\"",
-                                                                           device.toString()),
+                                                                           device.toShortString()),
                                                              "Cannot open target device",
                                                              JOptionPane.ERROR_MESSAGE);
                        } catch (AltosSerialInUseException si) {
                                JOptionPane.showMessageDialog(owner,
                                                              String.format("Device \"%s\" already in use",
-                                                                           device.toString()),
+                                                                           device.toShortString()),
                                                              "Device in use",
                                                              JOptionPane.ERROR_MESSAGE);
                        } catch (IOException ee) {
                                JOptionPane.showMessageDialog(owner,
-                                                             device.toString(),
+                                                             device.toShortString(),
                                                              ee.getLocalizedMessage(),
                                                              JOptionPane.ERROR_MESSAGE);
                        }
index 0d32a5ae851807e0940a634acf7ccfef335030cd..ab74486b49904e018beae5ca66df2dd2a02cce8a 100644 (file)
@@ -38,7 +38,7 @@ public class AltosSerial implements Runnable {
 
        static List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
 
-       altos_device device;
+       AltosDevice device;
        SWIGTYPE_p_altos_file altos;
        LinkedList<LinkedBlockingQueue<AltosLine>> monitors;
        LinkedBlockingQueue<AltosLine> reply_queue;
@@ -195,7 +195,7 @@ public class AltosSerial implements Runnable {
                }
                altos = libaltos.altos_open(device);
                if (altos == null)
-                       throw new FileNotFoundException(device.toString());
+                       throw new FileNotFoundException(device.toShortString());
                input_thread = new Thread(this);
                input_thread.start();
                print("~\nE 0\n");
@@ -233,7 +233,7 @@ public class AltosSerial implements Runnable {
                }
        }
 
-       public AltosSerial(altos_device in_device) throws FileNotFoundException, AltosSerialInUseException {
+       public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
                device = in_device;
                line = "";
                monitor_mode = false;
index 379e0e67466b7e402d1e35e2bf07a7c7263348e1..de5f50e93052895ac7d7e9aefd06ec48ccdae8c9 100644 (file)
@@ -55,7 +55,7 @@ class AltosTelemetryReader extends AltosFlightReader {
                device = in_device;
                serial = new AltosSerial(device);
                log = new AltosLog(serial);
-               name = device.toString();
+               name = device.toShortString();
 
                telem = new LinkedBlockingQueue<AltosLine>();
                serial.add_monitor(telem);
index b573ef7f7280a6de77cc825b2b4f67fad025abf9..6bfde014556b6e260c26306917b25dc5e5573d96 100644 (file)
@@ -53,18 +53,18 @@ public class AltosUI extends JFrame {
                } catch (FileNotFoundException ee) {
                        JOptionPane.showMessageDialog(AltosUI.this,
                                                      String.format("Cannot open device \"%s\"",
-                                                                   device.toString()),
+                                                                   device.toShortString()),
                                                      "Cannot open target device",
                                                      JOptionPane.ERROR_MESSAGE);
                } catch (AltosSerialInUseException si) {
                        JOptionPane.showMessageDialog(AltosUI.this,
                                                      String.format("Device \"%s\" already in use",
-                                                                   device.toString()),
+                                                                   device.toShortString()),
                                                      "Device in use",
                                                      JOptionPane.ERROR_MESSAGE);
                } catch (IOException ee) {
                        JOptionPane.showMessageDialog(AltosUI.this,
-                                                     device.toString(),
+                                                     device.toShortString(),
                                                      "Unkonwn I/O error",
                                                      JOptionPane.ERROR_MESSAGE);
                }