altosui/telegps: Use menus for frequency list and telemetry formats
authorKeith Packard <keithp@keithp.com>
Tue, 17 Jun 2014 04:57:35 +0000 (21:57 -0700)
committerKeith Packard <keithp@keithp.com>
Tue, 17 Jun 2014 04:57:35 +0000 (21:57 -0700)
This will let us move them into the Mac OS X menu bar, as well as
making them look a bit nicer.

Signed-off-by: Keith Packard <keithp@keithp.com>
altoslib/AltosFrequency.java
altosui/AltosFlightUI.java
altosuilib/AltosFreqList.java
altosuilib/AltosUITelemetryMenu.java [new file with mode: 0644]
altosuilib/Makefile.am
telegps/TeleGPS.java

index 7c291ea..d1ab847 100644 (file)
@@ -41,4 +41,4 @@ public class AltosFrequency {
                frequency = f;
                description = d;
        }
-}
\ No newline at end of file
+}
index 43deb63..2e192c7 100644 (file)
@@ -170,13 +170,17 @@ public class AltosFlightUI extends AltosUIFrame implements AltosFlightDisplay {
        }
 
        Container       bag;
-       AltosFreqList   frequencies;
-       JComboBox<String>       telemetries;
-       JLabel          telemetry;
+
+       JMenuBar                menu_bar;
+       AltosFreqList           frequencies;
+       AltosUITelemetryMenu    telemetries;
+       JLabel                  telemetry;
 
        ActionListener  show_timer;
 
        public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
+               super();
+
                AltosUIPreferences.set_component(this);
 
                displays = new LinkedList<AltosFlightDisplay>();
@@ -193,6 +197,10 @@ public class AltosFlightUI extends AltosUIFrame implements AltosFlightDisplay {
 
                /* Stick channel selector at top of table for telemetry monitoring */
                if (serial >= 0) {
+
+                       menu_bar = new JMenuBar();
+                       setJMenuBar(menu_bar);
+
                        // Channel menu
                        frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
                        frequencies.set_product("Monitor");
@@ -208,43 +216,19 @@ public class AltosFlightUI extends AltosUIFrame implements AltosFlightDisplay {
                                                reader.save_frequency();
                                        }
                        });
-                       c.gridx = 0;
-                       c.gridy = 0;
-                       c.weightx = 0;
-                       c.weighty = 0;
-                       c.insets = new Insets(3, 3, 3, 3);
-                       c.fill = GridBagConstraints.NONE;
-                       c.anchor = GridBagConstraints.WEST;
-                       bag.add (frequencies, c);
+                       menu_bar.add (frequencies);
 
                        // Telemetry format menu
-                       if (reader.supports_telemetry(Altos.ao_telemetry_standard)) {
-                               telemetries = new JComboBox<String>();
-                               for (int i = 1; i <= Altos.ao_telemetry_max; i++)
-                                       telemetries.addItem(Altos.telemetry_name(i));
-                               int telemetry = AltosPreferences.telemetry(serial);
-                               if (telemetry <= Altos.ao_telemetry_off ||
-                                   telemetry > Altos.ao_telemetry_max)
-                                       telemetry = Altos.ao_telemetry_standard;
-                               telemetries.setSelectedIndex(telemetry - 1);
-                               telemetries.setMaximumRowCount(Altos.ao_telemetry_max);
-                               telemetries.setPreferredSize(null);
-                               telemetries.revalidate();
+                       if (reader.supports_telemetry(AltosLib.ao_telemetry_standard)) {
+                               telemetries = new AltosUITelemetryMenu(serial);
                                telemetries.addActionListener(new ActionListener() {
                                                public void actionPerformed(ActionEvent e) {
-                                                       int telemetry = telemetries.getSelectedIndex() + 1;
+                                                       int telemetry = telemetries.get_selected();
                                                        reader.set_telemetry(telemetry);
                                                        reader.save_telemetry();
                                                }
                                        });
-                               c.gridx = 1;
-                               c.gridy = 0;
-                               c.weightx = 0;
-                               c.weighty = 0;
-                               c.fill = GridBagConstraints.NONE;
-                               c.anchor = GridBagConstraints.WEST;
-                               bag.add (telemetries, c);
-                               c.insets = new Insets(0, 0, 0, 0);
+                               menu_bar.add(telemetries);
                        } else {
                                String  version;
 
@@ -256,14 +240,7 @@ public class AltosFlightUI extends AltosUIFrame implements AltosFlightDisplay {
                                        version = "Telemetry: None";
 
                                telemetry = new JLabel(version);
-                               c.gridx = 1;
-                               c.gridy = 0;
-                               c.weightx = 0;
-                               c.weighty = 0;
-                               c.fill = GridBagConstraints.NONE;
-                               c.anchor = GridBagConstraints.WEST;
-                               bag.add (telemetry, c);
-                               c.insets = new Insets(0, 0, 0, 0);
+                               menu_bar.add(telemetry);
                        }
                }
 
index e1299aa..b985d47 100644 (file)
 
 package org.altusmetrum.altosuilib_2;
 
+import java.util.*;
 import javax.swing.*;
+import java.awt.event.*;
 import org.altusmetrum.altoslib_4.*;
 
-public class AltosFreqList extends JComboBox<AltosFrequency> {
+class FreqEntry extends JMenuItem {
+       AltosFrequency  frequency;
+
+       public FreqEntry(AltosFrequency frequency) {
+               super(frequency.toShortString());
+               this.frequency = frequency;
+       }
+}
+
+public class AltosFreqList extends JMenu implements ActionListener {
 
        String  product;
        int     serial;
        int     calibrate;
 
+       AltosFrequency[]        frequencies = new AltosFrequency[0];
+
+       int     selected = -1;
+
+       LinkedList<ActionListener> action_listeners = new LinkedList<ActionListener>();
+
+       public void addActionListener(ActionListener listener) {
+               action_listeners.add(listener);
+       }
+
+       public void removeActionListener(ActionListener listener) {
+               action_listeners.remove(listener);
+       }
+
+       public void actionPerformed(ActionEvent ev) {
+               FreqEntry e = (FreqEntry) ev.getSource();
+               set_selected(e.frequency);
+               ActionEvent event = new ActionEvent(e.frequency, 0, "selected");
+               for (ActionListener l : action_listeners)
+                       l.actionPerformed(event);
+       }
+
+       private void set_selected(AltosFrequency frequency) {
+               for (int i = 0; i < frequencies.length; i++) {
+                       if (frequencies[i].frequency == frequency.frequency) {
+                               selected = i;
+                               String  new_text = String.format("Frequency: %7.3f MHz (%s) ▾",
+                                                                frequency.frequency,
+                                                                frequency.description);
+                               setText(new_text);
+                       }
+               }
+       }
+
+       private AltosFrequency get_selected() {
+               if (0 <= selected && selected < frequencies.length)
+                       return frequencies[selected];
+               return null;
+       }
+
+       private void add(AltosFrequency add) {
+               int insert;
+
+               for (insert = 0; insert < frequencies.length; insert++) {
+                       if (frequencies[insert].frequency == add.frequency)
+                               return;
+                       if (add.frequency < frequencies[insert].frequency)
+                               break;
+               }
+
+               AltosFrequency[]        new_frequencies = new AltosFrequency[frequencies.length + 1];
+
+               for (int before = 0; before < insert; before++)
+                       new_frequencies[before] = frequencies[before];
+               new_frequencies[insert] = add;
+
+               for (int after = insert; after < frequencies.length; after++)
+                       new_frequencies[after+1] = frequencies[after];
+
+               frequencies = new_frequencies;
+
+               FreqEntry       e = new FreqEntry(add);
+               add(e, insert);
+               e.addActionListener(this);
+       }
+
+       private void remove(AltosFrequency remove) {
+               int delete;
+               for (delete = 0; delete < frequencies.length; delete++) {
+                       if (frequencies[delete].frequency == remove.frequency)
+                               break;
+                       if (remove.frequency < frequencies[delete].frequency)
+                               return;
+               }
+
+               remove(delete);
+
+               AltosFrequency[]        new_frequencies = new AltosFrequency[frequencies.length - 1];
+
+               for (int before = 0; before < delete; before++)
+                       new_frequencies[before] = frequencies[before];
+
+               for (int after = delete + 1; after < frequencies.length; after++)
+                       new_frequencies[after-1] = frequencies[after];
+               frequencies = new_frequencies;
+       }
+
        public void set_frequency(double new_frequency) {
                int i;
 
@@ -34,25 +132,21 @@ public class AltosFreqList extends JComboBox<AltosFrequency> {
                        return;
                }
 
-               for (i = 0; i < getItemCount(); i++) {
-                       AltosFrequency  f = (AltosFrequency) getItemAt(i);
+               for (i = 0; i < frequencies.length; i++) {
+                       AltosFrequency  f = frequencies[i];
 
                        if (f.close(new_frequency)) {
-                               setSelectedIndex(i);
+                               set_selected(f);
                                return;
                        }
                }
-               for (i = 0; i < getItemCount(); i++) {
-                       AltosFrequency  f = (AltosFrequency) getItemAt(i);
 
-                       if (new_frequency < f.frequency)
-                               break;
-               }
                String  description = String.format("%s serial %d", product, serial);
                AltosFrequency  frequency = new AltosFrequency(new_frequency, description);
                AltosUIPreferences.add_common_frequency(frequency);
-               insertItemAt(frequency, i);
-               setMaximumRowCount(getItemCount());
+
+               add(frequency);
+               set_selected(frequency);
        }
 
        public void set_product(String new_product) {
@@ -64,16 +158,16 @@ public class AltosFreqList extends JComboBox<AltosFrequency> {
        }
 
        public double frequency() {
-               AltosFrequency  f = (AltosFrequency) getSelectedItem();
+               AltosFrequency  f = get_selected();
                if (f != null)
                        return f.frequency;
                return 434.550;
        }
 
        public AltosFreqList () {
-               super(AltosUIPreferences.common_frequencies());
-               setMaximumRowCount(getItemCount());
-               setEditable(false);
+               super();
+               for (AltosFrequency frequency: AltosUIPreferences.common_frequencies())
+                       add(frequency);
                product = "Unknown";
                serial = 0;
        }
diff --git a/altosuilib/AltosUITelemetryMenu.java b/altosuilib/AltosUITelemetryMenu.java
new file mode 100644 (file)
index 0000000..fc4a91c
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright © 2014 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.altosuilib_2;
+
+import java.util.*;
+import javax.swing.*;
+import java.awt.event.*;
+import org.altusmetrum.altoslib_4.*;
+
+class TelemetryMenuItem extends JMenuItem {
+       public int      telemetry;
+
+       public TelemetryMenuItem (int telemetry) {
+               super(AltosLib.telemetry_name(telemetry));
+               this.telemetry = telemetry;
+       }
+}
+
+public class AltosUITelemetryMenu extends JMenu implements ActionListener {
+       TelemetryMenuItem       selected = null;
+
+       public int get_selected() {
+               if (selected == null)
+                       return AltosLib.ao_telemetry_off;
+               return selected.telemetry;
+       }
+
+       public void set_selected(int telemetry) {
+               for (int i = 0; i < getItemCount(); i++) {
+                       TelemetryMenuItem       item = (TelemetryMenuItem) getItem(i);
+                       if (item.telemetry == telemetry) {
+                               selected = item;
+                               String new_text = String.format("Format: %s ▾", AltosLib.telemetry_name(telemetry));
+                               setText(new_text);
+                               break;
+                       }
+               }
+       }
+
+       private LinkedList<ActionListener> action_listeners = new LinkedList<ActionListener>();
+
+       public void addActionListener(ActionListener l) {
+               action_listeners.add(l);
+       }
+
+       public void removeActionListener(ActionListener l) {
+               action_listeners.remove(l);
+       }
+
+       public void actionPerformed(ActionEvent e) {
+               TelemetryMenuItem item = (TelemetryMenuItem) e.getSource();
+               set_selected(item.telemetry);
+               ActionEvent my_e = new ActionEvent(selected, 0, "selected");
+               for (ActionListener l : action_listeners)
+                       l.actionPerformed(my_e);
+       }
+
+       public AltosUITelemetryMenu(int serial) {
+               super();
+               for (int i = AltosLib.ao_telemetry_min; i <= AltosLib.ao_telemetry_max; i++) {
+                       TelemetryMenuItem       item = new TelemetryMenuItem(i);
+
+                       item.addActionListener(this);
+                       add(item);
+               }
+
+               int telemetry = AltosPreferences.telemetry(serial);
+               if (telemetry < AltosLib.ao_telemetry_min || AltosLib.ao_telemetry_max < telemetry)
+                       telemetry = AltosLib.ao_telemetry_standard;
+               set_selected(telemetry);
+       }
+}
+
index e08fbe7..157cd5e 100644 (file)
@@ -80,7 +80,8 @@ altosuilib_JAVA = \
        AltosUIFlightTab.java \
        AltosUIIndicator.java \
        AltosUIUnitsIndicator.java \
-       AltosUIVoltageIndicator.java
+       AltosUIVoltageIndicator.java \
+       AltosUITelemetryMenu.java
 
 JAR=altosuilib_$(ALTOSUILIB_VERSION).jar
 
index 6e68dd3..646495b 100644 (file)
@@ -351,17 +351,18 @@ public class TeleGPS
                frequencies.set_product("Monitor");
                frequencies.set_serial(serial);
                frequencies.set_frequency(AltosUIPreferences.frequency(serial));
-               frequencies.setEnabled(true);
 
+               menu_bar.add(frequencies);
+               menu_bar.repaint();
        }
 
        void disable_frequency_menu() {
-               if (frequency_listener != null) {
-                       frequencies.removeActionListener(frequency_listener);
-                       frequencies.setEnabled(false);
-                       frequency_listener = null;
-               }
-
+               if (frequency_listener == null)
+                       return;
+               frequencies.removeActionListener(frequency_listener);
+               menu_bar.remove(frequencies);
+               menu_bar.repaint();
+               frequency_listener = null;
        }
 
        public void set_reader(AltosFlightReader reader, AltosDevice device) {
@@ -435,8 +436,6 @@ public class TeleGPS
                monitor_menu = make_menu("Monitor", monitor_menu_entries);
                device_menu = make_menu("Device", device_menu_entries);
                frequencies = new AltosFreqList();
-               frequencies.setEnabled(false);
-               menu_bar.add(frequencies);
 
                displays = new LinkedList<AltosFlightDisplay>();