altosui: Add ability to create CSV file from telem or eeprom files
[fw/altos] / ao-tools / altosui / AltosLogfileChooser.java
index 72d21fc81db5b341eca170e18d660c7dc247c20b..36b51de65c993bece80378fb3d11b2d0bafb5f24 100644 (file)
@@ -28,21 +28,52 @@ import java.text.*;
 import java.util.prefs.*;
 
 import altosui.AltosPreferences;
+import altosui.AltosReader;
+import altosui.AltosEepromReader;
+import altosui.AltosTelemetryReader;
 
 public class AltosLogfileChooser extends JFileChooser {
        JFrame  frame;
+       String  filename;
+       File    file;
 
-       public File runDialog() {
+       public String filename() {
+               return filename;
+       }
+
+       public File file() {
+               return file;
+       }
+
+       public AltosReader runDialog() {
                int     ret;
 
                ret = showOpenDialog(frame);
-               if (ret == APPROVE_OPTION)
-                       return getSelectedFile();
+               if (ret == APPROVE_OPTION) {
+                       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;
        }
 
        public AltosLogfileChooser(JFrame in_frame) {
-               in_frame = frame;
+               frame = in_frame;
                setDialogTitle("Select Flight Record File");
                setFileFilter(new FileNameExtensionFilter("Flight data file",
                                                          "eeprom",