JAL-629 fix opening files
authorBen Soares <b.soares@dundee.ac.uk>
Thu, 8 Dec 2022 17:29:38 +0000 (17:29 +0000)
committerBen Soares <b.soares@dundee.ac.uk>
Thu, 8 Dec 2022 17:29:38 +0000 (17:29 +0000)
src/jalview/gui/Desktop.java

index f2af0c0..16603df 100644 (file)
@@ -107,6 +107,7 @@ import jalview.io.DataSourceType;
 import jalview.io.FileFormat;
 import jalview.io.FileFormatException;
 import jalview.io.FileFormatI;
+import jalview.io.FileFormats;
 import jalview.io.FileLoader;
 import jalview.io.FormatAdapter;
 import jalview.io.IdentifyFile;
@@ -1185,9 +1186,29 @@ public class Desktop extends jalview.jbgui.GDesktop
       public void run()
       {
         File selectedFile = chooser.getSelectedFile();
-        FileFormatI format = chooser.getSelectedFormat();
         Cache.setProperty("LAST_DIRECTORY", selectedFile.getParent());
-        viewport.addFile(selectedFile, format);
+
+        FileFormatI format = chooser.getSelectedFormat();
+
+        /*
+         * Call IdentifyFile to verify the file contains what its extension implies.
+         * Skip this step for dynamically added file formats, because IdentifyFile does
+         * not know how to recognise them.
+         */
+        if (FileFormats.getInstance().isIdentifiable(format))
+        {
+          try
+          {
+            format = new IdentifyFile().identify(selectedFile,
+                    DataSourceType.FILE);
+          } catch (FileFormatException e)
+          {
+            // format = null; //??
+          }
+        }
+
+        new FileLoader().LoadFile(viewport, selectedFile,
+                DataSourceType.FILE, format);
       }
     });
     chooser.showOpenDialog(this);