From 3e428f447c03ff771ab6a82632acc274f640aeb5 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Wed, 5 Jun 2024 20:39:27 +0100 Subject: [PATCH] JAL-4409 Small refactoring of ArgParserUtils to allow processFiles to work on drag-and-dropped files list. --- src/jalview/gui/Desktop.java | 55 +++++++++++++++- src/jalview/util/ArgParserUtils.java | 120 ++++++++++++++++++++++------------ 2 files changed, 132 insertions(+), 43 deletions(-) diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index 4c30a06..addbb38 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -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 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 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) diff --git a/src/jalview/util/ArgParserUtils.java b/src/jalview/util/ArgParserUtils.java index fabbc76..435ed8a 100644 --- a/src/jalview/util/ArgParserUtils.java +++ b/src/jalview/util/ArgParserUtils.java @@ -23,8 +23,24 @@ public class ArgParserUtils private static Set treeExtensions = null; + public static List argSet = null; + + public static Map> argExtensionsMap = null; + public static void preProcessArgs(List filenames) { + processFilenames(filenames, true); + } + + public static Map processFilenames( + List filenames, boolean addArgs) + { + return processFilenames(filenames, addArgs, null); + } + + public static Map processFilenames( + List filenames, boolean addArgs, List 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 filesSet = new HashSet<>(filenames); - List argSet = new ArrayList<>(); - argSet.add(Arg.ANNOTATIONS); - argSet.add(Arg.FEATURES); - argSet.add(Arg.TREE); - - Map> argExtensionsMap = new HashMap<>(); - argExtensionsMap.put(Arg.ANNOTATIONS, annotationsExtensions); - argExtensionsMap.put(Arg.FEATURES, featuresExtensions); - argExtensionsMap.put(Arg.TREE, treeExtensions); - Map 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 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 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 getAssociatedFilesMap() { - associatedFiles = new HashMap<>(); + return associatedFiles; } - associatedFiles.put(a, file); } -} + +} \ No newline at end of file -- 1.7.10.2