JAL-4409 Small refactoring of ArgParserUtils to allow processFiles to work on drag...
authorBen Soares <b.soares@dundee.ac.uk>
Wed, 5 Jun 2024 19:39:27 +0000 (20:39 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Wed, 5 Jun 2024 19:39:27 +0000 (20:39 +0100)
src/jalview/gui/Desktop.java
src/jalview/util/ArgParserUtils.java

index 4c30a06..addbb38 100644 (file)
@@ -123,6 +123,7 @@ import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceI;
 import jalview.gui.ImageExporter.ImageWriterI;
 import jalview.gui.QuitHandler.QResponse;
+import jalview.io.AppletFormatAdapter;
 import jalview.io.BackupFiles;
 import jalview.io.DataSourceType;
 import jalview.io.FileFormat;
@@ -134,12 +135,15 @@ import jalview.io.FormatAdapter;
 import jalview.io.IdentifyFile;
 import jalview.io.JalviewFileChooser;
 import jalview.io.JalviewFileView;
+import jalview.io.NewickFile;
 import jalview.io.exceptions.ImageOutputException;
 import jalview.jbgui.GSplitFrame;
 import jalview.jbgui.GStructureViewer;
 import jalview.project.Jalview2XML;
 import jalview.structure.StructureSelectionManager;
 import jalview.urls.IdOrgSettings;
+import jalview.util.ArgParserUtils;
+import jalview.util.ArgParserUtils.BaseInfo;
 import jalview.util.BrowserLauncher;
 import jalview.util.ChannelProperties;
 import jalview.util.ImageMaker.TYPE;
@@ -1239,6 +1243,17 @@ public class Desktop extends jalview.jbgui.GDesktop
     {
       try
       {
+        // map list to list of strings
+        List<String> filenames = new ArrayList<>();
+        for (int i = 0; i < files.size(); i++)
+        {
+          filenames.add(files.get(i).toString());
+        }
+
+        // processFilenames will take likely associated files OUT of the list of
+        // filenames and return them in a BaseInfo object in the Map
+        Map<String, BaseInfo> baseInfoMap = ArgParserUtils
+                .processFilenames(filenames, false, files);
         for (int i = 0; i < files.size(); i++)
         {
           // BH 2018 File or String
@@ -1262,7 +1277,45 @@ public class Desktop extends jalview.jbgui.GDesktop
           {
             Platform.cacheFileData((File) file);
           }
-          new FileLoader().LoadFile(null, file, protocol, format);
+          // new FileLoader().LoadFile(null, file, protocol, format);
+          AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(fileName,
+                  protocol, format);
+          // now we add any associated files to the alignframe
+          if (baseInfoMap.containsKey(fileName) && baseInfoMap.get(fileName)
+                  .getAssociatedFilesMap() != null)
+          {
+            BaseInfo bi = baseInfoMap.get(fileName);
+            if (bi.getAssociatedFilesMap().containsKey(Arg.FEATURES))
+            {
+              String featuresfile = bi.getAssociatedFilesMap()
+                      .get(Arg.FEATURES);
+              af.parseFeaturesFile(featuresfile,
+                      AppletFormatAdapter.checkProtocol(featuresfile));
+            }
+            if (bi.getAssociatedFilesMap().containsKey(Arg.ANNOTATIONS))
+            {
+              String annotationsfile = bi.getAssociatedFilesMap()
+                      .get(Arg.ANNOTATIONS);
+              af.loadJalviewDataFile(annotationsfile, null, null, null);
+            }
+            if (bi.getAssociatedFilesMap().containsKey(Arg.TREE))
+            {
+              String treefile = bi.getAssociatedFilesMap().get(Arg.TREE);
+              try
+              {
+                NewickFile nf = new NewickFile(treefile,
+                        AppletFormatAdapter.checkProtocol(treefile));
+                af.getViewport().setCurrentTree(
+                        af.showNewickTree(nf, treefile).getTree());
+              } catch (IOException e)
+              {
+                jalview.bin.Console.warn("Couldn't add tree " + treefile,
+                        e);
+              }
+
+            }
+
+          }
 
         }
       } catch (Exception ex)
index fabbc76..435ed8a 100644 (file)
@@ -23,8 +23,24 @@ public class ArgParserUtils
 
   private static Set<String> treeExtensions = null;
 
+  public static List<Arg> argSet = null;
+
+  public static Map<Arg, Set<String>> argExtensionsMap = null;
+
   public static void preProcessArgs(List<String> filenames)
   {
+    processFilenames(filenames, true);
+  }
+
+  public static Map<String, BaseInfo> processFilenames(
+          List<String> filenames, boolean addArgs)
+  {
+    return processFilenames(filenames, addArgs, null);
+  }
+
+  public static Map<String, BaseInfo> processFilenames(
+          List<String> filenames, boolean addArgs, List<Object> files)
+  {
     // Running through the arguments to look for '-arg' or '--arg' should
     // already have happened, not doing it again.
     if (alignmentExtensions == null)
@@ -34,16 +50,6 @@ public class ArgParserUtils
 
     Set<String> filesSet = new HashSet<>(filenames);
 
-    List<Arg> argSet = new ArrayList<>();
-    argSet.add(Arg.ANNOTATIONS);
-    argSet.add(Arg.FEATURES);
-    argSet.add(Arg.TREE);
-
-    Map<Arg, Set<String>> argExtensionsMap = new HashMap<>();
-    argExtensionsMap.put(Arg.ANNOTATIONS, annotationsExtensions);
-    argExtensionsMap.put(Arg.FEATURES, featuresExtensions);
-    argExtensionsMap.put(Arg.TREE, treeExtensions);
-
     Map<String, BaseInfo> baseInfoMap = new HashMap<>();
 
     // we make a copy to run through, and delete associated filenames from the
@@ -72,7 +78,15 @@ public class ArgParserUtils
             if (filesSet.contains(possibleFile))
             {
               bi.putAssociatedFile(arg, possibleFile);
+              int filePos = filenames.indexOf(possibleFile);
               filenames.remove(possibleFile);
+
+              // also remove File/String object from files if given
+              if (files != null && files.get(filePos) != null
+                      && possibleFile.equals(files.get(filePos).toString()))
+              {
+                files.remove(filePos);
+              }
               break;
             }
           }
@@ -82,31 +96,37 @@ public class ArgParserUtils
       }
     }
 
-    // now we go through the saved associated files and add them back in to the
-    // right places with the appropriate argument
-    for (String filename : baseInfoMap.keySet())
+    if (addArgs)
     {
-      BaseInfo bi = baseInfoMap.get(filename);
-
-      int pos = filenames.indexOf(filename);
-      if (bi.associatedFiles != null)
+      // now we go through the saved associated files and add them back in to
+      // the
+      // right places with the appropriate argument
+      for (String filename : baseInfoMap.keySet())
       {
-        for (Arg a : bi.associatedFiles.keySet())
+        BaseInfo bi = baseInfoMap.get(filename);
+
+        int pos = filenames.indexOf(filename);
+        if (bi.associatedFiles != null)
         {
-          String associatedFile = bi.associatedFiles.get(a);
-          if (associatedFile == null)
+          for (Arg a : bi.associatedFiles.keySet())
           {
-            // shouldn't happen!
-            continue;
+            String associatedFile = bi.associatedFiles.get(a);
+            if (associatedFile == null)
+            {
+              // shouldn't happen!
+              continue;
+            }
+            filenames.add(pos + 1, a.argString());
+            filenames.add(pos + 2,
+                    HttpUtils.equivalentJalviewUrl(associatedFile));
           }
-          filenames.add(pos + 1, a.argString());
-          filenames.add(pos + 2,
-                  HttpUtils.equivalentJalviewUrl(associatedFile));
         }
+        // add an --open arg to separate from other files
+        filenames.add(pos, Arg.OPEN.argString());
       }
-      // add an --open arg to separate from other files
-      filenames.add(pos, Arg.OPEN.argString());
     }
+
+    return baseInfoMap;
   }
 
   private static void setValidExtensions()
@@ -147,26 +167,42 @@ public class ArgParserUtils
     {
       treeExtensions.add(ext);
     }
-  }
-}
-
-class BaseInfo
-{
-  String filename;
 
-  Map<Arg, String> associatedFiles = null;
+    argSet = new ArrayList<>();
+    argSet.add(Arg.ANNOTATIONS);
+    argSet.add(Arg.FEATURES);
+    argSet.add(Arg.TREE);
 
-  BaseInfo(String filename)
-  {
-    this.filename = filename;
+    argExtensionsMap = new HashMap<>();
+    argExtensionsMap.put(Arg.ANNOTATIONS, annotationsExtensions);
+    argExtensionsMap.put(Arg.FEATURES, featuresExtensions);
+    argExtensionsMap.put(Arg.TREE, treeExtensions);
   }
 
-  void putAssociatedFile(Arg a, String file)
+  public static class BaseInfo
   {
-    if (associatedFiles == null)
+    String filename;
+
+    Map<Arg, String> associatedFiles = null;
+
+    BaseInfo(String filename)
+    {
+      this.filename = filename;
+    }
+
+    void putAssociatedFile(Arg a, String file)
+    {
+      if (associatedFiles == null)
+      {
+        associatedFiles = new HashMap<>();
+      }
+      associatedFiles.put(a, file);
+    }
+
+    public Map<Arg, String> getAssociatedFilesMap()
     {
-      associatedFiles = new HashMap<>();
+      return associatedFiles;
     }
-    associatedFiles.put(a, file);
   }
-}
+
+}
\ No newline at end of file