java: Bump java library versions for next release
[fw/altos] / telegps / TeleGPS.java
index d30d8dc53e632b6e98d4a75ec295275501fab4e3..f765ee23e0d1a616917496abb8ec35de57e24cf2 100644 (file)
@@ -23,10 +23,13 @@ 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",
@@ -48,7 +51,7 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
        }
 
        AltosFlightReader       reader;
-       AltosDisplayThread      thread;
+       TeleGPSDisplayThread    thread;
 
        JMenuBar                menu_bar;
 
@@ -56,58 +59,61 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
        JMenu                   monitor_menu;
        JMenu                   device_menu;
        AltosFreqList           frequencies;
+       ActionListener          frequency_listener;
 
        Container               bag;
 
        TeleGPSStatus           telegps_status;
        TeleGPSStatusUpdate     status_update;
+       javax.swing.Timer       status_timer;
 
        JTabbedPane             pane;
 
-       AltosSiteMap            sitemap;
+       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 },
        };
 
        /* Device menu */
        final static String     download_command = "download";
-       final static String     export_command = "export";
-       final static String     graph_command = "graph";
        final static String     configure_command = "configure";
        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 },
        };
 
@@ -126,13 +132,14 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                        display.reset();
        }
 
-       public void set_font() {
+       public void font_size_changed(int font_size) {
                for (AltosFlightDisplay display : displays)
-                       display.set_font();
+                       display.font_size_changed(font_size);
        }
 
-       public void font_size_changed(int font_size) {
-               set_font();
+       public void units_changed(boolean imperial_units) {
+               for (AltosFlightDisplay display : displays)
+                       display.units_changed(imperial_units);
        }
 
        public void show(AltosState state, AltosListenerState listener_state) {
@@ -163,13 +170,20 @@ 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();
        }
 
        void connect(AltosDevice device) {
@@ -177,8 +191,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(),
@@ -208,7 +221,7 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                }
        }
 
-       void monitor() {
+       void connect() {
                AltosDevice     device = AltosDeviceUIDialog.show(this,
                                                                  AltosLib.product_basestation);
                if (device == null)
@@ -242,6 +255,16 @@ 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() {
@@ -271,8 +294,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())) {
@@ -307,12 +330,12 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                }
        }
 
-       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 {
@@ -322,32 +345,59 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                                        }
                                        reader.save_frequency();
                                }
-                       });
+                       };
+
+               frequencies.addActionListener(frequency_listener);
+               frequencies.set_product("Monitor");
+               frequencies.set_serial(serial);
+               frequencies.set_frequency(AltosUIPreferences.frequency(serial));
+
                menu_bar.add(frequencies);
+               menu_bar.repaint();
        }
 
-       void remove_frequency_menu() {
-               if (frequencies != null) {
-                       menu_bar.remove(frequencies);
-                       frequencies = null;
-               }
+       void disable_frequency_menu() {
+               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) {
+       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);
        }
 
        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) {
@@ -385,6 +435,8 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                file_menu = make_menu("File", file_menu_entries);
                monitor_menu = make_menu("Monitor", monitor_menu_entries);
                device_menu = make_menu("Device", device_menu_entries);
+               frequencies = new AltosFreqList();
+
                displays = new LinkedList<AltosFlightDisplay>();
 
                int serial = -1;
@@ -415,14 +467,18 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                c.gridwidth = 2;
                bag.add(pane, c);
 
-               sitemap = new AltosSiteMap();
-               pane.add("Site Map", sitemap);
-               displays.add(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);
@@ -430,6 +486,7 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
 
                AltosUIPreferences.register_font_listener(this);
+               AltosPreferences.register_units_listener(this);
 
                addWindowListener(new WindowAdapter() {
                                @Override
@@ -441,16 +498,12 @@ 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) {
@@ -479,6 +532,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)
@@ -542,7 +607,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"))
@@ -562,10 +627,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;
@@ -587,14 +653,14 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                }
                if (errors != 0)
                        System.exit(errors);
-               if (!any_created) {
+               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 (!any_created)
+                       if (number_of_windows == 0)
                                new TeleGPS();
                }
        }