altosuilib: Rewrite map GUI bits
[fw/altos] / telegps / TeleGPS.java
index d30d8dc53e632b6e98d4a75ec295275501fab4e3..2503d53e4d92b47d658868976c6fbb27f6156196 100644 (file)
@@ -26,7 +26,10 @@ import java.util.*;
 import org.altusmetrum.altoslib_4.*;
 import org.altusmetrum.altosuilib_2.*;
 
-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;
 
@@ -64,7 +67,7 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
 
        JTabbedPane             pane;
 
-       AltosSiteMap            sitemap;
+       AltosUIMap              map;
        TeleGPSInfo             gps_info;
        AltosInfoTable          info_table;
 
@@ -126,13 +129,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,7 +167,7 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
        }
 
        void load_maps() {
-               new AltosSiteMapPreload(this);
+               new AltosUIMapPreload(this);
        }
 
        void disconnect() {
@@ -242,6 +246,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() {
@@ -335,19 +349,28 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
 
        public void set_reader(AltosFlightReader reader) {
                setTitle(String.format("TeleGPS %s", reader.name));
-               thread = new AltosDisplayThread(this, voice(), this, reader);
+               thread = new TeleGPSDisplayThread(this, voice(), this, reader);
                thread.start();
        }
 
        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() {
                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) {
@@ -415,9 +438,9 @@ 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", map);
+               displays.add(map);
 
                gps_info = new TeleGPSInfo();
                pane.add("Info", gps_info);
@@ -430,6 +453,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,7 +465,7 @@ public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFo
                pack();
                setVisible(true);
 
-               ++number_of_windows;
+               add_window();
 
                status_update = new TeleGPSStatusUpdate(telegps_status);
 
@@ -479,6 +503,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 +578,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 +598,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 +624,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();
                }
        }