telegps: Track graph windows as one of the TeleGPS windows
authorKeith Packard <keithp@keithp.com>
Sat, 7 Jun 2014 18:52:28 +0000 (11:52 -0700)
committerKeith Packard <keithp@keithp.com>
Sat, 7 Jun 2014 19:34:15 +0000 (12:34 -0700)
TeleGPS exits when the number of windows goes to zero; track graphing
windows in addition to the usual flight monitoring windows.

Signed-off-by: Keith Packard <keithp@keithp.com>
telegps/TeleGPS.java
telegps/TeleGPSGraphUI.java

index 5a707547d480e131ee16332d1de2bd731cd23def..7117443632dcfa9553a706be58dba74fefaef2a2 100644 (file)
@@ -355,14 +355,22 @@ public class TeleGPS
 
        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) {
@@ -457,7 +465,7 @@ public class TeleGPS
                pack();
                setVisible(true);
 
-               ++number_of_windows;
+               add_window();
 
                status_update = new TeleGPSStatusUpdate(telegps_status);
 
@@ -495,6 +503,18 @@ public class TeleGPS
                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)
@@ -578,10 +598,11 @@ public class TeleGPS
                        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;
@@ -603,14 +624,14 @@ public class TeleGPS
                }
                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();
                }
        }
index b7c17780d522e91f776ecaa3554ee8114ee12769..84c80815d82875630778c2548e8505c70c992d53 100644 (file)
@@ -21,7 +21,11 @@ import java.io.*;
 import java.util.ArrayList;
 
 import java.awt.*;
+import java.awt.event.*;
 import javax.swing.*;
+import java.io.*;
+import java.util.concurrent.*;
+import java.util.*;
 import org.altusmetrum.altoslib_4.*;
 import org.altusmetrum.altosuilib_2.*;
 
@@ -46,6 +50,12 @@ public class TeleGPSGraphUI extends AltosUIFrame
                }
        }
 
+       private void close() {
+               setVisible(false);
+               dispose();
+               TeleGPS.subtract_window();
+       }
+
        TeleGPSGraphUI(AltosStateIterable states, File file) throws InterruptedException, IOException {
                super(file.getName());
                state = null;
@@ -65,11 +75,23 @@ public class TeleGPSGraphUI extends AltosUIFrame
 
                setContentPane (pane);
 
+               addWindowListener(new WindowAdapter() {
+                               @Override
+                               public void windowClosing(WindowEvent e) {
+                                       close();
+                               }
+                       });
+
                pack();
 
-               setDefaultCloseOperation(DISPOSE_ON_CLOSE);
+               setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
+
+               TeleGPS.add_window();
+
                setVisible(true);
+
                if (state != null)
                        map.centre(state);
+
        }
 }