micropeak/altosui/telegps: Fix icon file names
[fw/altos] / telegps / TeleGPS.java
index 1bb505e03c068e102e1a1551bce205c3750d0681..a4b221e814b5444df1630fb46cd82fb15273b580 100644 (file)
@@ -23,18 +23,21 @@ import javax.swing.*;
 import java.io.*;
 import java.util.concurrent.*;
 import java.util.*;
-import org.altusmetrum.altoslib_4.*;
-import org.altusmetrum.altosuilib_2.*;
+import org.altusmetrum.altoslib_5.*;
+import org.altusmetrum.altosuilib_3.*;
 
-public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFontListener, ActionListener {
+public class TeleGPS
+       extends AltosUIFrame
+       implements AltosFlightDisplay, AltosFontListener, AltosUnitsListener, ActionListener
+{
 
        static String[] telegps_icon_names = {
-               "/telegps-16.png",
-               "/telegps-32.png",
-               "/telegps-48.png",
-               "/telegps-64.png",
-               "/telegps-128.png",
-               "/telegps-256.png"
+               "/altusmetrum-telegps-16.png",
+               "/altusmetrum-telegps-32.png",
+               "/altusmetrum-telegps-48.png",
+               "/altusmetrum-telegps-64.png",
+               "/altusmetrum-telegps-128.png",
+               "/altusmetrum-telegps-256.png"
        };
 
        static { set_icon_names(telegps_icon_names); }
@@ -48,41 +51,59 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
        }
 
        AltosFlightReader       reader;
-       AltosDisplayThread      thread;
+       TeleGPSDisplayThread    thread;
 
-       JTabbedPane     pane;
+       JMenuBar                menu_bar;
 
-       AltosSiteMap    sitemap;
-       TeleGPSInfo     gps_info;
-       boolean         has_map;
+       JMenu                   file_menu;
+       JMenu                   monitor_menu;
+       JMenu                   device_menu;
+       AltosUIFreqList         frequencies;
+       ActionListener          frequency_listener;
+       AltosUIRateList         rates;
+       ActionListener          rate_listener;
 
-       JMenuBar        menu_bar;
-       JMenu           file_menu;
-       JMenu           monitor_menu;
-       JMenu           device_menu;
+       Container               bag;
+
+       TeleGPSStatus           telegps_status;
+       TeleGPSStatusUpdate     status_update;
+       javax.swing.Timer       status_timer;
+
+       JTabbedPane             pane;
+
+       AltosUIMap              map;
+       TeleGPSInfo             gps_info;
+       TeleGPSState            gps_state;
+       AltosInfoTable          info_table;
+
+       LinkedList<AltosFlightDisplay>  displays;
 
        /* File menu */
        final static String     new_command = "new";
-       final static String     preferences_command = "preferences";
+       final static String     graph_command = "graph";
+       final static String     export_command = "export";
        final static String     load_maps_command = "loadmaps";
+       final static String     preferences_command = "preferences";
        final static String     close_command = "close";
        final static String     exit_command = "exit";
 
        static final String[][] file_menu_entries = new String[][] {
                { "New Window",         new_command },
-               { "Preferences",        preferences_command },
+               { "Graph Data",         graph_command },
+               { "Export Data",        export_command },
                { "Load Maps",          load_maps_command },
+               { "Preferences",        preferences_command },
                { "Close",              close_command },
                { "Exit",               exit_command },
        };
 
        /* Monitor menu */
-       final static String     monitor_command = "monitor";
+       final static String     connect_command = "connect";
        final static String     disconnect_command = "disconnect";
        final static String     scan_command = "scan";
 
        static final String[][] monitor_menu_entries = new String[][] {
-               { "Monitor Device",     monitor_command },
+               { "Connect Device",     connect_command },
                { "Disconnect",         disconnect_command },
                { "Scan Channels",      scan_command },
        };
@@ -90,20 +111,14 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
        /* Device menu */
        final static String     download_command = "download";
        final static String     configure_command = "configure";
-       final static String     export_command = "export";
-       final static String     graph_command = "graph";
+       final static String     flash_command = "flash";
 
        static final String[][] device_menu_entries = new String[][] {
                { "Download Data",      download_command },
                { "Configure Device",   configure_command },
-               { "Export Data",        export_command },
-               { "Graph Data",         graph_command },
+               { "Flash Device",       flash_command },
        };
 
-//     private AltosInfoTable flightInfo;
-
-       boolean exit_on_close = false;
-
        void stop_display() {
                if (thread != null && thread.isAlive()) {
                        thread.interrupt();
@@ -115,41 +130,39 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
        }
 
        public void reset() {
-               sitemap.reset();
-               gps_info.reset();
-       }
-
-       public void set_font() {
-               sitemap.set_font();
-               gps_info.set_font();
+               for (AltosFlightDisplay display : displays)
+                       display.reset();
        }
 
        public void font_size_changed(int font_size) {
-               set_font();
+               for (AltosFlightDisplay display : displays)
+                       display.font_size_changed(font_size);
        }
 
-
-//     AltosFlightStatusUpdate status_update;
+       public void units_changed(boolean imperial_units) {
+               for (AltosFlightDisplay display : displays)
+                       display.units_changed(imperial_units);
+       }
 
        public void show(AltosState state, AltosListenerState listener_state) {
-//             status_update.saved_state = state;
+               try {
+                       status_update.saved_state = state;
 
-               if (state == null)
-                       state = new AltosState();
+                       if (state == null)
+                               state = new AltosState();
 
-               sitemap.show(state, listener_state);
-               gps_info.show(state, listener_state);
-               telegps_status.show(state, listener_state);
+                       int i = 0;
+                       for (AltosFlightDisplay display : displays) {
+                               display.show(state, listener_state);
+                               i++;
+                       }
+               } catch (Exception ex) {
+                       System.out.printf("Exception %s\n", ex.toString());
+                       for (StackTraceElement e : ex.getStackTrace())
+                               System.out.printf("%s\n", e.toString());
+               }
        }
 
-       Container               bag;
-       AltosFreqList           frequencies;
-       JLabel                  telemetry;
-       TeleGPSStatus           telegps_status;
-       TeleGPSStatusUpdate     status_update;
-
-       ActionListener          show_timer;
-
        void new_window() {
                new TeleGPS();
        }
@@ -159,13 +172,21 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
        }
 
        void load_maps() {
-               new AltosSiteMapPreload(this);
+               new AltosUIMapPreload(this);
        }
 
        void disconnect() {
                setTitle("TeleGPS");
                stop_display();
-               remove_frequency_menu();
+               if (status_timer != null) {
+                       status_timer.stop();
+                       status_timer = null;
+                       status_update = null;
+               }
+
+               telegps_status.disable_receive();
+               disable_frequency_menu();
+               disable_rate_menu();
        }
 
        void connect(AltosDevice device) {
@@ -173,8 +194,7 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                        disconnect();
                try {
                        AltosFlightReader       reader = new AltosTelemetryReader(new AltosSerial(device));
-                       set_reader(reader);
-                       add_frequency_menu(device.getSerial(), reader);
+                       set_reader(reader, device);
                } catch (FileNotFoundException ee) {
                        JOptionPane.showMessageDialog(this,
                                                      ee.getMessage(),
@@ -204,7 +224,7 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                }
        }
 
-       void monitor() {
+       void connect() {
                AltosDevice     device = AltosDeviceUIDialog.show(this,
                                                                  AltosLib.product_basestation);
                if (device == null)
@@ -238,6 +258,20 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
        }
 
        void graph() {
+               AltosDataChooser chooser;
+               chooser = new AltosDataChooser(this);
+               AltosStateIterable states = chooser.runDialog();
+               if (states == null)
+                       return;
+               try {
+                       new TeleGPSGraphUI(states, chooser.file());
+               } catch (InterruptedException ie) {
+               } catch (IOException ie) {
+               }
+       }
+
+       void flash() {
+               AltosFlashUI.show(this);
        }
 
        public void actionPerformed(ActionEvent ev) {
@@ -263,8 +297,8 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                        System.exit(0);
 
                /* Monitor menu */
-               if (monitor_command.equals(ev.getActionCommand())) {
-                       monitor();
+               if (connect_command.equals(ev.getActionCommand())) {
+                       connect();
                        return;
                }
                if (disconnect_command.equals(ev.getActionCommand())) {
@@ -293,14 +327,18 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                        graph();
                        return;
                }
+               if (flash_command.equals(ev.getActionCommand())) {
+                       flash();
+                       return;
+               }
        }
 
-       void add_frequency_menu(int serial, final AltosFlightReader reader) {
-               // Channel menu
-               frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
-               frequencies.set_product("Monitor");
-               frequencies.set_serial(serial);
-               frequencies.addActionListener(new ActionListener() {
+       void enable_frequency_menu(int serial, final AltosFlightReader reader) {
+
+               if (frequency_listener != null)
+                       disable_frequency_menu();
+
+               frequency_listener = new ActionListener() {
                                public void actionPerformed(ActionEvent e) {
                                        double frequency = frequencies.frequency();
                                        try {
@@ -310,32 +348,94 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                                        }
                                        reader.save_frequency();
                                }
-                       });
-               menu_bar.add(frequencies);
+                       };
+
+               frequencies.addActionListener(frequency_listener);
+               frequencies.set_product("Monitor");
+               frequencies.set_serial(serial);
+               frequencies.set_frequency(AltosUIPreferences.frequency(serial));
+               frequencies.setEnabled(true);
+
        }
 
-       void remove_frequency_menu() {
-               if (frequencies != null) {
-                       menu_bar.remove(frequencies);
-                       frequencies = null;
+       void disable_frequency_menu() {
+               if (frequency_listener != null) {
+                       frequencies.removeActionListener(frequency_listener);
+                       frequencies.setEnabled(false);
+                       frequency_listener = null;
                }
+
        }
 
-       public void set_reader(AltosFlightReader reader) {
+       void enable_rate_menu(int serial, final AltosFlightReader reader) {
+
+               if (rate_listener != null)
+                       disable_rate_menu();
+
+               rate_listener = new ActionListener() {
+                               public void actionPerformed(ActionEvent e) {
+                                       int rate = rates.rate();
+                                       try {
+                                               System.out.printf("set rate %d\n", rate);
+                                               reader.set_telemetry_rate(rate);
+                                       } catch (TimeoutException te) {
+                                       } catch (InterruptedException ie) {
+                                       }
+                                       reader.save_telemetry_rate();
+                               }
+                       };
+
+               rates.addActionListener(rate_listener);
+               rates.set_product("Monitor");
+               rates.set_serial(serial);
+               rates.set_rate(AltosUIPreferences.telemetry_rate(serial));
+               rates.setEnabled(reader.supports_telemetry_rate(AltosLib.ao_telemetry_rate_2400));
+       }
+
+       void disable_rate_menu() {
+               if (rate_listener != null) {
+                       rates.removeActionListener(rate_listener);
+                       rates.setEnabled(false);
+                       rate_listener = null;
+               }
+
+       }
+
+       public void set_reader(AltosFlightReader reader, AltosDevice device) {
+               status_update = new TeleGPSStatusUpdate(telegps_status);
+
+               status_timer = new javax.swing.Timer(100, status_update);
+               status_timer.start();
+
                setTitle(String.format("TeleGPS %s", reader.name));
-               thread = new AltosDisplayThread(this, voice(), this, reader);
+               thread = new TeleGPSDisplayThread(this, voice(), this, reader);
                thread.start();
+
+               if (device != null) {
+                       enable_frequency_menu(device.getSerial(), reader);
+                       enable_rate_menu(device.getSerial(), reader);
+               }
        }
 
        static int      number_of_windows;
 
+       static public void add_window() {
+               ++number_of_windows;
+       }
+
+       static public void subtract_window() {
+               --number_of_windows;
+               if (number_of_windows == 0)
+                       System.exit(0);
+       }
+
        private void close() {
+               disconnect();
                AltosUIPreferences.unregister_font_listener(this);
+               AltosPreferences.unregister_units_listener(this);
                setVisible(false);
                dispose();
-               --number_of_windows;
-               if (number_of_windows == 0)
-                       System.exit(0);
+               subtract_window();
        }
 
        private void add_menu(JMenu menu, String label, String action) {
@@ -348,16 +448,38 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
 
        private JMenu make_menu(String label, String[][] items) {
                JMenu   menu = new JMenu(label);
-               for (int i = 0; i < items.length; i++)
+               for (int i = 0; i < items.length; i++) {
+                       if (MAC_OS_X) {
+                               if (items[i][1].equals("exit"))
+                                       continue;
+                               if (items[i][1].equals("preferences"))
+                                       continue;
+                       }
                        add_menu(menu, items[i][0], items[i][1]);
+               }
                menu_bar.add(menu);
                return menu;
        }
 
+       /* OSXAdapter interfaces */
+       public void macosx_file_handler(String path) {
+               process_graph(new File(path));
+       }
+
+       public void macosx_quit_handler() {
+               System.exit(0);
+       }
+
+       public void macosx_preferences_handler() {
+               preferences();
+       }
+
        public TeleGPS() {
 
                AltosUIPreferences.set_component(this);
 
+               register_for_macosx_events();
+
                reader = null;
 
                bag = getContentPane();
@@ -374,6 +496,28 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                monitor_menu = make_menu("Monitor", monitor_menu_entries);
                device_menu = make_menu("Device", device_menu_entries);
 
+               frequencies = new AltosUIFreqList();
+               frequencies.setEnabled(false);
+               c.gridx = 0;
+               c.gridy = 0;
+               c.fill = GridBagConstraints.NONE;
+               c.anchor = GridBagConstraints.WEST;
+               c.weightx = 0;
+               c.gridwidth = 1;
+               bag.add(frequencies, c);
+
+               rates = new AltosUIRateList();
+               rates.setEnabled(false);
+               c.gridx = 1;
+               c.gridy = 0;
+               c.fill = GridBagConstraints.NONE;
+               c.anchor = GridBagConstraints.WEST;
+               c.weightx = 0;
+               c.gridwidth = 1;
+               bag.add(rates, c);
+
+               displays = new LinkedList<AltosFlightDisplay>();
+
                int serial = -1;
 
                /* TeleGPS status is always visible */
@@ -385,6 +529,7 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                c.gridwidth = 2;
                bag.add(telegps_status, c);
                c.gridwidth = 1;
+               displays.add(telegps_status);
 
 
                /* The rest of the window uses a tabbed pane to
@@ -401,15 +546,26 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                c.gridwidth = 2;
                bag.add(pane, c);
 
-               sitemap = new AltosSiteMap();
-               pane.add("Site Map", sitemap);
+               map = new AltosUIMap();
+               pane.add(map.getName(), map);
+               displays.add(map);
 
                gps_info = new TeleGPSInfo();
-               pane.add("Info", gps_info);
+               pane.add(gps_info.getName(), gps_info);
+               displays.add(gps_info);
+
+               gps_state = new TeleGPSState();
+               pane.add(gps_state.getName(), gps_state);
+               displays.add(gps_state);
+
+               info_table = new AltosInfoTable();
+               pane.add("Table", info_table);
+               displays.add(info_table);
 
                setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
 
                AltosUIPreferences.register_font_listener(this);
+               AltosPreferences.register_units_listener(this);
 
                addWindowListener(new WindowAdapter() {
                                @Override
@@ -421,16 +577,17 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                pack();
                setVisible(true);
 
-               ++number_of_windows;
-
-               status_update = new TeleGPSStatusUpdate(telegps_status);
-
-               new javax.swing.Timer(100, status_update).start();
+               add_window();
        }
 
        public TeleGPS(AltosFlightReader reader) {
                this();
-               set_reader(reader);
+               set_reader(reader, null);
+       }
+
+       public TeleGPS(AltosDevice device) {
+               this();
+               connect(device);
        }
 
        static AltosStateIterable record_iterable(File file) {
@@ -454,6 +611,18 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                return new AltosReplayReader(states.iterator(), file);
        }
 
+       static boolean process_graph(File file) {
+               AltosStateIterable states = record_iterable(file);
+               if (states == null)
+                       return false;
+               try {
+                       new TeleGPSGraphUI(states, file);
+               } catch (Exception e) {
+                       return false;
+               }
+               return true;
+       }
+
        static boolean process_replay(File file) {
                AltosReplayReader new_reader = replay_file(file);
                if (new_reader == null)
@@ -517,7 +686,7 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                                } else {
                                        double lat = Double.parseDouble(args[i+1]);
                                        double lon = Double.parseDouble(args[i+2]);
-                                       AltosSiteMap.prefetchMaps(lat, lon);
+                                       AltosUIMap.prefetch_maps(lat, lon);
                                        i += 2;
                                }
                        } else if (args[i].equals("--replay"))
@@ -537,10 +706,11 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                        else {
                                File file = new File(args[i]);
                                switch (process) {
+                               case process_none:
                                case process_graph:
-                                       ++errors;
+                                       if (!process_graph(file))
+                                               ++errors;
                                        break;
-                               case process_none:
                                case process_replay:
                                        if (!process_replay(file))
                                                ++errors;
@@ -562,7 +732,15 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                }
                if (errors != 0)
                        System.exit(errors);
-               if (!any_created)
-                       new TeleGPS();
+               if (number_of_windows == 0) {
+                       java.util.List<AltosDevice> devices = AltosUSBDevice.list(AltosLib.product_basestation);
+                       if (devices != null)
+                               for (AltosDevice device : devices) {
+                                       new TeleGPS(device);
+                                       any_created = true;
+                               }
+                       if (number_of_windows == 0)
+                               new TeleGPS();
+               }
        }
 }