From a5928e2c1b9e2cea8d9c43f3d03f9cce2604f3b2 Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Wed, 15 Mar 2023 00:48:45 +0000 Subject: [PATCH] JAL-629 Commands more objecty, less classy. FileLoader with sync option (or rather default async flag). Tests adjusted. --- src/jalview/bin/Commands.java | 82 ++++++++++++++++--------------- src/jalview/bin/Jalview.java | 16 +++++- src/jalview/bin/Launcher.java | 2 + src/jalview/gui/AlignViewport.java | 10 +++- src/jalview/gui/Desktop.java | 1 + src/jalview/io/FileLoader.java | 37 ++++++++++---- src/jalview/util/AWTConsole.java | 1 + test/jalview/bin/CommandsTest.java | 46 +++++++++++------ test/jalview/bin/argparser/argfile0.txt | 2 +- test/jalview/bin/argparser/test2.fa | 2 +- test/jalview/bin/argparser/test3.fa | 2 + 11 files changed, 131 insertions(+), 70 deletions(-) create mode 100644 test/jalview/bin/argparser/test3.fa diff --git a/src/jalview/bin/Commands.java b/src/jalview/bin/Commands.java index 001e32e..26ce8c3 100644 --- a/src/jalview/bin/Commands.java +++ b/src/jalview/bin/Commands.java @@ -50,52 +50,59 @@ public class Commands { Desktop desktop; - private static boolean headless; + private boolean headless; - private static ArgParser argParser; + private ArgParser argParser; private Map afMap; - private static boolean commandArgsProvided = false; + private boolean commandArgsProvided = false; - public static boolean commandArgsProvided() + private boolean argsWereParsed = false; + + public Commands(ArgParser argparser, boolean headless) { - return commandArgsProvided; + this(Desktop.instance, argparser, headless); } - public static boolean processArgs(ArgParser ap, boolean h) + public Commands(Desktop d, ArgParser argparser, boolean h) { - argParser = ap; + argParser = argparser; headless = h; - boolean argsWereParsed = true; - if (headless) + desktop = d; + afMap = new HashMap(); + if (argparser != null) { - System.setProperty("java.awt.headless", "true"); + processArgs(argparser, headless); } + } + + private boolean processArgs(ArgParser argparser, boolean h) + { + argParser = argparser; + headless = h; + boolean theseArgsWereParsed = false; if (argParser != null && argParser.linkedIds() != null) { for (String id : argParser.linkedIds()) { ArgValuesMap avm = argParser.linkedArgs(id); - Commands cmds = new Commands(); + theseArgsWereParsed = true; if (id == null) { - cmds.processUnlinked(id); - argsWereParsed &= cmds.wereParsed(); + theseArgsWereParsed &= processUnlinked(id); } else { - cmds.processLinked(id); - argsWereParsed &= cmds.wereParsed(); + theseArgsWereParsed &= processLinked(id); } - cmds.processImages(id); - argsWereParsed &= cmds.wereParsed(); + theseArgsWereParsed &= processImages(id); // close ap if (avm.getBoolean(Arg.CLOSE)) { - AlignFrame af = cmds.afMap.get(id); + AlignFrame af = afMap.get(id); if (af != null) { af.closeMenuItem_actionPerformed(true); @@ -111,39 +118,31 @@ public class Commands return true; } // carry on with jalview.bin.Jalview + argsWereParsed = theseArgsWereParsed; return argsWereParsed; } - boolean argsWereParsed = true; // set false as soon as an arg is found - - private boolean wereParsed() + public boolean commandArgsProvided() { - return argsWereParsed; - } - - public Commands() - { - this(Desktop.instance); + return commandArgsProvided; } - public Commands(Desktop d) + public boolean argsWereParsed() { - this.desktop = d; - afMap = new HashMap(); + return argsWereParsed; } - protected void processUnlinked(String id) + protected boolean processUnlinked(String id) { - processLinked(id); + return processLinked(id); } - protected void processLinked(String id) + protected boolean processLinked(String id) { + boolean theseArgsWereParsed = false; ArgValuesMap avm = argParser.linkedArgs(id); if (avm == null) - return; - else - argsWereParsed = false; + return true; /* // script to execute after all loading is completed one way or another @@ -169,7 +168,7 @@ public class Commands if (openFile == null) continue; - argsWereParsed = true; + theseArgsWereParsed = true; if (first) { first = false; @@ -365,7 +364,7 @@ public class Commands { Console.debug( "Opening '" + openFile + "' in existing alignment frame"); - af.getCurrentView().addFile(new File(openFile), format); + af.getCurrentView().addFile(new File(openFile), format, false); } Console.debug("Command " + Arg.OPEN + " executed successfully!"); @@ -553,9 +552,11 @@ public class Commands Console.info("Changed colour " + acg.toString()); } } + + return theseArgsWereParsed; } - protected void processImages(String id) + protected boolean processImages(String id) { ArgValuesMap avm = argParser.linkedArgs(id); AlignFrame af = afMap.get(id); @@ -563,7 +564,7 @@ public class Commands if (af == null) { Console.warn("Did not have an alignment window for id=" + id); - return; + return false; } if (avm.containsArg(Arg.IMAGE)) @@ -614,6 +615,7 @@ public class Commands } } } + return true; } private SequenceI getSpecifiedSequence(AlignFrame af, SubVals subId) diff --git a/src/jalview/bin/Jalview.java b/src/jalview/bin/Jalview.java index b5def09..a728bc8 100755 --- a/src/jalview/bin/Jalview.java +++ b/src/jalview/bin/Jalview.java @@ -126,6 +126,8 @@ public class Jalview private Desktop desktop; + protected Commands cmds; + public static AlignFrame currentAlignFrame; static @@ -641,12 +643,16 @@ public class Jalview } } // Run Commands from cli - boolean commandsSuccess = Commands.processArgs(argparser, headlessArg); + cmds = new Commands(argparser, headlessArg); + boolean commandsSuccess = cmds.argsWereParsed(); if (commandsSuccess) { Console.info("Successfully completed commands"); if (headlessArg) + { + System.out.println("#### EXITING"); System.exit(0); + } } else { @@ -971,7 +977,7 @@ public class Jalview if (!Platform.isJS() && !headless && file == null && Cache.getDefault("SHOW_STARTUP_FILE", true) - && !Commands.commandArgsProvided()) + && !cmds.commandArgsProvided()) // don't open the startup file if command line args have been processed // (&& !Commands.commandArgsProvided()) /** @@ -1572,6 +1578,7 @@ public class Jalview public void quit() { // System.exit will run the shutdownHook first + System.out.println("Quitting now. Bye!"); System.exit(0); } @@ -1584,4 +1591,9 @@ public class Jalview { Jalview.currentAlignFrame = currentAlignFrame; } + + protected Commands getCommands() + { + return cmds; + } } diff --git a/src/jalview/bin/Launcher.java b/src/jalview/bin/Launcher.java index a55146d..9e1fb51 100644 --- a/src/jalview/bin/Launcher.java +++ b/src/jalview/bin/Launcher.java @@ -208,6 +208,8 @@ public class Launcher if (Boolean.parseBoolean(System.getProperty("launcherstop", "false"))) { + System.out.println( + "System property 'launcherstop' is set and not 'false'. Exiting."); System.exit(0); } try diff --git a/src/jalview/gui/AlignViewport.java b/src/jalview/gui/AlignViewport.java index 90f627e..3b7420f 100644 --- a/src/jalview/gui/AlignViewport.java +++ b/src/jalview/gui/AlignViewport.java @@ -340,7 +340,7 @@ public class AlignViewport extends AlignmentViewport viewStyle.setFontName(font.getName()); viewStyle.setFontStyle(font.getStyle()); viewStyle.setFontSize(font.getSize()); - + validCharWidth = true; } @@ -745,6 +745,11 @@ public class AlignViewport extends AlignmentViewport */ public void addFile(File file, FileFormatI format) { + addFile(file, format, true); + } + + public void addFile(File file, FileFormatI format, boolean async) + { DataSourceType protocol = AppletFormatAdapter.checkProtocol(file); if (format == null) @@ -769,7 +774,8 @@ public class AlignViewport extends AlignmentViewport } } - new FileLoader().LoadFile(this, file, DataSourceType.FILE, format); + new FileLoader().LoadFile(this, file, DataSourceType.FILE, format, + async); } public void addFile(File file) diff --git a/src/jalview/gui/Desktop.java b/src/jalview/gui/Desktop.java index 7b4af59..ba7665b 100644 --- a/src/jalview/gui/Desktop.java +++ b/src/jalview/gui/Desktop.java @@ -1436,6 +1436,7 @@ public class Desktop extends jalview.jbgui.GDesktop // this will run the shutdownHook but QuitHandler.getQuitResponse() should // not run a second time if gotQuitResponse flag has been set (i.e. user // confirmed quit of some kind). + System.out.println("Desktop exiting."); System.exit(0); } diff --git a/src/jalview/io/FileLoader.java b/src/jalview/io/FileLoader.java index 449c685..971aba3 100755 --- a/src/jalview/io/FileLoader.java +++ b/src/jalview/io/FileLoader.java @@ -96,32 +96,51 @@ public class FileLoader implements Runnable public void LoadFile(AlignViewport viewport, Object file, DataSourceType protocol, FileFormatI format) { + LoadFile(viewport, file, protocol, format, true); + } + + public void LoadFile(AlignViewport viewport, Object file, + DataSourceType protocol, FileFormatI format, boolean async) + { this.viewport = viewport; if (file instanceof File) { this.selectedFile = (File) file; file = selectedFile.getPath(); } - LoadFile(file.toString(), protocol, format); + LoadFile(file.toString(), protocol, format, async); } public void LoadFile(String file, DataSourceType protocol, FileFormatI format) { + LoadFile(file, protocol, format, true); + } + + public void LoadFile(String file, DataSourceType protocol, + FileFormatI format, boolean async) + { this.file = file; this.protocol = protocol; this.format = format; - final Thread loader = new Thread(this); - - SwingUtilities.invokeLater(new Runnable() + if (async) { - @Override - public void run() + final Thread loader = new Thread(this); + + SwingUtilities.invokeLater(new Runnable() { - loader.start(); - } - }); + @Override + public void run() + { + loader.start(); + } + }); + } + else + { + this.run(); + } } /** diff --git a/src/jalview/util/AWTConsole.java b/src/jalview/util/AWTConsole.java index 9286794..ab049a3 100644 --- a/src/jalview/util/AWTConsole.java +++ b/src/jalview/util/AWTConsole.java @@ -174,6 +174,7 @@ public class AWTConsole extends WindowAdapter } catch (Exception e) { } + System.out.println("Window closing. Bye!"); System.exit(0); } diff --git a/test/jalview/bin/CommandsTest.java b/test/jalview/bin/CommandsTest.java index 9c763e1..65162f3 100644 --- a/test/jalview/bin/CommandsTest.java +++ b/test/jalview/bin/CommandsTest.java @@ -3,6 +3,7 @@ package jalview.bin; import java.util.Date; import org.testng.Assert; +import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @@ -30,34 +31,49 @@ public class CommandsTest JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION); } + @AfterMethod(alwaysRun = true) + public void tearDown() + { + Desktop.instance.closeAll_actionPerformed(null); + } + @Test(groups = "Functional", dataProvider = "cmdLines") public void commandsOpenTest(String cmdLine, String sequence) { String[] args = cmdLine.split("\\s+"); Jalview.main(args); - ArgParser argparser = new ArgParser(args); - boolean commandsSuccess = Commands.processArgs(argparser, false); - Assert.assertTrue(commandsSuccess, "Overall commandsSuccess is false"); + Commands cmds = Jalview.getInstance().getCommands(); + Assert.assertNotNull(cmds); + Assert.assertTrue(cmds.commandArgsProvided(), + "Commands were not provided in the args"); + Assert.assertTrue(cmds.argsWereParsed(), + "Overall command parse and operation is false"); if (sequence != null) { - AlignFrame[] afs = Desktop.getAlignFrames(); - boolean found = false; - ALIGNFRAME: for (AlignFrame af : afs) + Assert.assertTrue(lookForSequenceName(sequence), + "Sequence '" + sequence + + "' was not found in opened alignment files: " + + cmdLine); + } + + System.out.println("##### GOT TO END"); + } + + public static boolean lookForSequenceName(String sequenceName) + { + AlignFrame[] afs = Desktop.getAlignFrames(); + for (AlignFrame af : afs) + { + for (String name : af.getViewport().getAlignment().getSequenceNames()) { - for (String name : af.getViewport().getAlignment() - .getSequenceNames()) + if (sequenceName.equals(name)) { - if (sequence.equals(name)) - { - found = true; - break ALIGNFRAME; - } + return true; } } - Assert.assertTrue(found, "Sequence '" + sequence - + "' was not found in opened alignment files: " + cmdLine); } + return false; } @DataProvider(name = "cmdLines") diff --git a/test/jalview/bin/argparser/argfile0.txt b/test/jalview/bin/argparser/argfile0.txt index 8ef7264..b27ceab 100644 --- a/test/jalview/bin/argparser/argfile0.txt +++ b/test/jalview/bin/argparser/argfile0.txt @@ -1,2 +1,2 @@ --open=test/jalview/bin/argparser/test1.fa ---open=test/jalview/bin/argparser/test2.fa +--open=test/jalview/bin/argparser/test3.fa diff --git a/test/jalview/bin/argparser/test2.fa b/test/jalview/bin/argparser/test2.fa index a51dd78..fbd15c3 100644 --- a/test/jalview/bin/argparser/test2.fa +++ b/test/jalview/bin/argparser/test2.fa @@ -1,2 +1,2 @@ >TEST2 -BBBB +LLLL diff --git a/test/jalview/bin/argparser/test3.fa b/test/jalview/bin/argparser/test3.fa new file mode 100644 index 0000000..ea05599 --- /dev/null +++ b/test/jalview/bin/argparser/test3.fa @@ -0,0 +1,2 @@ +>TEST2 +AAARG -- 1.7.10.2