Merge branch 'develop' into releases/Release_2_11_3_Branch
[jalview.git] / src / jalview / bin / Commands.java
index 1d65fc6..2794b02 100644 (file)
@@ -13,6 +13,8 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 
+import javax.swing.SwingUtilities;
+
 import jalview.analysis.AlignmentUtils;
 import jalview.api.structures.JalviewStructureDisplayI;
 import jalview.bin.Jalview.ExitCode;
@@ -194,7 +196,7 @@ public class Commands
     if (avm.containsArg(Arg.APPEND) || avm.containsArg(Arg.OPEN))
     {
       commandArgsProvided = true;
-      long progress = -1;
+      final long progress = System.currentTimeMillis();
 
       boolean first = true;
       boolean progressBarSet = false;
@@ -220,10 +222,18 @@ public class Commands
           first = false;
           if (!headless && desktop != null)
           {
-            desktop.setProgressBar(
-                    MessageManager.getString(
-                            "status.processing_commandline_args"),
-                    progress = System.currentTimeMillis());
+            SwingUtilities.invokeLater(new Runnable()
+            {
+              @Override
+              public void run()
+              {
+                desktop.setProgressBar(
+                        MessageManager.getString(
+                                "status.processing_commandline_args"),
+                        progress);
+
+              }
+            });
             progressBarSet = true;
           }
         }
@@ -365,24 +375,49 @@ public class Commands
           boolean showSSAnnotations = avm.getFromSubValArgOrPref(
                   Arg.SHOWSSANNOTATIONS, av.getSubVals(), null,
                   "STRUCT_FROM_PDB", true);
-          af.setAnnotationsVisibility(showSSAnnotations, true, false);
 
           // Show sequence annotations?
           boolean showAnnotations = avm.getFromSubValArgOrPref(
                   Arg.SHOWANNOTATIONS, av.getSubVals(), null,
                   "SHOW_ANNOTATIONS", true);
-          af.setAnnotationsVisibility(showAnnotations, false, true);
 
-          // show temperature factor annotations?
-          if (avm.getBoolean(Arg.NOTEMPFAC))
+          boolean hideTFrows = (avm.getBoolean(Arg.NOTEMPFAC));
+          final AlignFrame _af = af;
+          // many of jalview's format/layout methods are only thread safe on the
+          // swingworker thread.
+          // all these methods should be on the alignViewController so it can
+          // coordinate such details
+          try
           {
-            // do this better (annotation types?)
-            List<String> hideThese = new ArrayList<>();
-            hideThese.add("Temperature Factor");
-            hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
-            AlignmentUtils.showOrHideSequenceAnnotations(
-                    af.getCurrentView().getAlignment(), hideThese, null,
-                    false, false);
+            SwingUtilities.invokeAndWait(new Runnable()
+            {
+
+              @Override
+              public void run()
+              {
+                _af.setAnnotationsVisibility(showSSAnnotations, true,
+                        false);
+
+                _af.setAnnotationsVisibility(showAnnotations, false, true);
+
+                // show temperature factor annotations?
+                if (hideTFrows)
+                {
+                  // do this better (annotation types?)
+                  List<String> hideThese = new ArrayList<>();
+                  hideThese.add("Temperature Factor");
+                  hideThese.add(AlphaFoldAnnotationRowBuilder.LABEL);
+                  AlignmentUtils.showOrHideSequenceAnnotations(
+                          _af.getCurrentView().getAlignment(), hideThese,
+                          null, false, false);
+                }
+              }
+            });
+          } catch (Exception x)
+          {
+            Console.warn(
+                    "Unexpected exception adjusting annotation row visibility.",
+                    x);
           }
 
           // wrap alignment? do this last for formatting reasons
@@ -408,9 +443,11 @@ public class Commands
         {
           Console.debug(
                   "Opening '" + openFile + "' in existing alignment frame");
+
           DataSourceType dst = HttpUtils.startsWithHttpOrHttps(openFile)
                   ? DataSourceType.URL
                   : DataSourceType.FILE;
+
           FileLoader fileLoader = new FileLoader(!headless);
           fileLoader.LoadFile(af.getCurrentView(), openFile, dst, null,
                   false);
@@ -439,11 +476,14 @@ public class Commands
     // open the structure (from same PDB file or given PDBfile)
     if (!avm.getBoolean(Arg.NOSTRUCTURE))
     {
+
       AlignFrame af = afMap.get(id);
       if (avm.containsArg(Arg.STRUCTURE))
       {
         commandArgsProvided = true;
-        for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
+        for (
+
+        ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
         {
           argParser.setStructureFilename(null);
           String val = av.getValue();
@@ -769,16 +809,17 @@ public class Commands
                   AppJmol jmol = (AppJmol) sview;
                   jmol.makePDBImage(structureImageFile, imageType, renderer,
                           userBis);
-                  Console.debug("Finished Rendering image to "
+                  Console.info("Exported structure image to "
                           + structureImageFile);
 
                   // RESTORE SESSION AFTER EXPORT IF NEED BE
                   if (sessionToRestore != null)
                   {
-                    Console.debug("Restoring session from "
-                            + sessionToRestore);
-                    
-                    sview.getBinding().restoreSession(sessionToRestore.getAbsolutePath());
+                    Console.debug(
+                            "Restoring session from " + sessionToRestore);
+
+                    sview.getBinding().restoreSession(
+                            sessionToRestore.getAbsolutePath());
 
                   }
                 } catch (ImageOutputException ioexec)
@@ -789,11 +830,14 @@ public class Commands
                   continue;
                 } finally
                 {
-                  try {
+                  try
+                  {
                     this.colourAlignFrame(af, originalColourScheme);
                   } catch (Exception t)
                   {
-                    addError("Unexpected error when restoring colourscheme to alignment after temporary change for export.",t);
+                    addError(
+                            "Unexpected error when restoring colourscheme to alignment after temporary change for export.",
+                            t);
                   }
                 }
               }
@@ -806,6 +850,7 @@ public class Commands
 
     if (wrap)
     {
+
       AlignFrame af = afMap.get(id);
       if (af != null)
       {
@@ -817,16 +862,16 @@ public class Commands
     boolean doShading = avm.getBoolean(Arg.TEMPFAC_SHADING);
     if (doShading)
     {
-      AlignFrame af = afMap.get(id);
-      for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
-              .findAnnotation(PDBChain.class.getName().toString()))
-      {
-        AnnotationColourGradient acg = new AnnotationColourGradient(aa,
-                af.alignPanel.av.getGlobalColourScheme(), 0);
-        acg.setSeqAssociated(true);
-        af.changeColour(acg);
-        Console.info("Changed colour " + acg.toString());
-      }
+    AlignFrame af = afMap.get(id);
+    for (AlignmentAnnotation aa : af.alignPanel.getAlignment()
+            .findAnnotation(PDBChain.class.getName().toString()))
+    {
+      AnnotationColourGradient acg = new AnnotationColourGradient(aa,
+              af.alignPanel.av.getGlobalColourScheme(), 0);
+      acg.setSeqAssociated(true);
+      af.changeColour(acg);
+      Console.info("Changed colour " + acg.toString());
+    }
     }
     */
 
@@ -838,21 +883,29 @@ public class Commands
     ArgValuesMap avm = argParser.getLinkedArgs(id);
     AlignFrame af = afMap.get(id);
 
-    if (af == null)
+    if (avm != null && !avm.containsArg(Arg.GROOVY))
     {
-      addWarn("Did not have an alignment window for id=" + id);
+      // nothing to do
       return;
     }
 
+    if (af == null)
+    {
+      addWarn("Groovy script does not have an alignment window.  Proceeding with caution!");
+    }
+
     if (avm.containsArg(Arg.GROOVY))
     {
-      String groovyscript = avm.getValue(Arg.GROOVY);
-      if (groovyscript != null)
+      for (ArgValue groovyAv : avm.getArgValueList(Arg.GROOVY))
       {
-        // Execute the groovy script after we've done all the rendering stuff
-        // and before any images or figures are generated.
-        Console.info("Executing script " + groovyscript);
-        Jalview.getInstance().executeGroovyScript(groovyscript, af);
+        String groovyscript = groovyAv.getValue();
+        if (groovyscript != null)
+        {
+          // Execute the groovy script after we've done all the rendering stuff
+          // and before any images or figures are generated.
+          Console.info("Executing script " + groovyscript);
+          Jalview.getInstance().executeGroovyScript(groovyscript, af);
+        }
       }
     }
   }
@@ -862,9 +915,16 @@ public class Commands
     ArgValuesMap avm = argParser.getLinkedArgs(id);
     AlignFrame af = afMap.get(id);
 
+    if (avm != null && !avm.containsArg(Arg.IMAGE))
+    {
+      // nothing to do
+      return true;
+    }
+
     if (af == null)
     {
-      addWarn("Did not have an alignment window for id=" + id);
+      addWarn("Do not have an alignment window to create image from (id="
+              + id + ").  Not proceeding.");
       return false;
     }
 
@@ -991,9 +1051,16 @@ public class Commands
     ArgValuesMap avm = argParser.getLinkedArgs(id);
     AlignFrame af = afMap.get(id);
 
+    if (avm != null && !avm.containsArg(Arg.OUTPUT))
+    {
+      // nothing to do
+      return true;
+    }
+
     if (af == null)
     {
-      addWarn("Did not have an alignment window for id=" + id);
+      addWarn("Do not have an alignment window (id=" + id
+              + ").  Not proceeding.");
       return false;
     }