altosui: refactor logfile chooser dialog to share more code
authorKeith Packard <keithp@keithp.com>
Tue, 24 Aug 2010 06:15:05 +0000 (23:15 -0700)
committerKeith Packard <keithp@keithp.com>
Tue, 24 Aug 2010 06:15:05 +0000 (23:15 -0700)
Move file opening logic into logfile chooser as it can be shared that way.

Signed-off-by: Keith Packard <keithp@keithp.com>
ao-tools/altosui/AltosLogfileChooser.java
ao-tools/altosui/AltosUI.java

index 72d21fc81db5b341eca170e18d660c7dc247c20b..3e9e48922b2f1268642755fbb5899b306feba6ad 100644 (file)
@@ -28,16 +28,42 @@ import java.text.*;
 import java.util.prefs.*;
 
 import altosui.AltosPreferences;
 import java.util.prefs.*;
 
 import altosui.AltosPreferences;
+import altosui.AltosReader;
+import altosui.AltosEepromReader;
+import altosui.AltosTelemetryReader;
 
 public class AltosLogfileChooser extends JFileChooser {
        JFrame  frame;
 
 public class AltosLogfileChooser extends JFileChooser {
        JFrame  frame;
+       String  filename;
 
 
-       public File runDialog() {
+       public String filename() {
+               return filename;
+       }
+
+       public AltosReader runDialog() {
                int     ret;
 
                ret = showOpenDialog(frame);
                int     ret;
 
                ret = showOpenDialog(frame);
-               if (ret == APPROVE_OPTION)
-                       return getSelectedFile();
+               if (ret == APPROVE_OPTION) {
+                       File file = getSelectedFile();
+                       if (file == null)
+                               return null;
+                       filename = file.getName();
+                       try {
+                               FileInputStream in;
+
+                               in = new FileInputStream(file);
+                               if (filename.endsWith("eeprom"))
+                                       return new AltosEepromReader(in);
+                               else
+                                       return new AltosTelemetryReader(in);
+                       } catch (FileNotFoundException fe) {
+                               JOptionPane.showMessageDialog(frame,
+                                                             filename,
+                                                             "Cannot open file",
+                                                             JOptionPane.ERROR_MESSAGE);
+                       }
+               }
                return null;
        }
 
                return null;
        }
 
index 64f96cddce15139cfdf00e696bc90e9242eb2941..8a4c753fefd6a04f021edd5f9014ee40b36866d7 100644 (file)
@@ -532,26 +532,10 @@ public class AltosUI extends JFrame {
        private void Replay() {
                AltosLogfileChooser chooser = new AltosLogfileChooser(
                        AltosUI.this);
        private void Replay() {
                AltosLogfileChooser chooser = new AltosLogfileChooser(
                        AltosUI.this);
-
-               File file = chooser.runDialog();
-
-               if (file != null) {
-                       String  filename = file.getName();
-                       try {
-                               FileInputStream replay = new FileInputStream(file);
-                               DisplayThread   thread;
-                               if (filename.endsWith("eeprom"))
-                                   thread = new ReplayEepromThread(replay, filename);
-                               else
-                                   thread = new ReplayTelemetryThread(replay, filename);
-                               run_display(thread);
-                       } catch (FileNotFoundException ee) {
-                               JOptionPane.showMessageDialog(AltosUI.this,
-                                                             filename,
-                                                             "Cannot open telemetry file",
-                                                             JOptionPane.ERROR_MESSAGE);
-                       }
-               }
+               AltosReader reader = chooser.runDialog();
+               if (reader != null)
+                       run_display(new ReplayThread(reader,
+                                                    chooser.filename()));
        }
 
        /* Connect to TeleMetrum, either directly or through
        }
 
        /* Connect to TeleMetrum, either directly or through