JAL-629 JAL-4265 JAL-4286 report the filename for the exported structure image like...
[jalview.git] / src / jalview / bin / Commands.java
index 592ac85..5d3b50d 100644 (file)
@@ -3,7 +3,6 @@ package jalview.bin;
 import java.awt.Color;
 import java.io.File;
 import java.io.IOException;
-import java.lang.reflect.Field;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -25,8 +24,6 @@ import jalview.bin.argparser.SubVals;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.SequenceI;
 import jalview.datamodel.annotations.AlphaFoldAnnotationRowBuilder;
-import jalview.ext.jmol.JalviewJmolBinding;
-import jalview.ext.jmol.JmolCommands;
 import jalview.gui.AlignFrame;
 import jalview.gui.AlignmentPanel;
 import jalview.gui.AppJmol;
@@ -51,9 +48,9 @@ import jalview.io.exceptions.ImageOutputException;
 import jalview.schemes.ColourSchemeI;
 import jalview.schemes.ColourSchemeProperty;
 import jalview.structure.StructureCommandI;
-import jalview.structure.StructureCommandsI;
 import jalview.structure.StructureImportSettings.TFType;
 import jalview.structure.StructureSelectionManager;
+import jalview.util.ColorUtils;
 import jalview.util.FileUtils;
 import jalview.util.HttpUtils;
 import jalview.util.ImageMaker;
@@ -148,10 +145,14 @@ public class Commands
 
     }
 
-    // report errors
-    Console.warn(
-            "The following errors and warnings occurred whilst processing files:\n"
-                    + errorsToString());
+    // report errors - if any
+    String errorsRaised = errorsToString();
+    if (errorsRaised.trim().length() > 0)
+    {
+      Console.warn(
+              "The following errors and warnings occurred whilst processing files:\n"
+                      + errorsRaised);
+    }
     // gui errors reported in Jalview
 
     if (argParser.getBoolean(Arg.QUIT))
@@ -679,6 +680,19 @@ public class Commands
                 BitmapImageSizing userBis = ImageMaker
                         .parseScaleWidthHeightStrings(scale, width, height);
 
+                /////
+                // DON'T TRY TO EXPORT IF VIEWER IS UNSUPPORTED
+                if (viewerType != ViewerType.JMOL)
+                {
+                  addWarn("Cannot export image for structure viewer "
+                          + viewerType.name() + " yet");
+                  continue;
+                }
+
+                /////
+                // Apply the temporary colourscheme to the linked alignment
+                // TODO: enhance for multiple linked alignments.
+
                 String imageColour = avm.getValueFromSubValOrArg(
                         structureImageArgValue, Arg.IMAGECOLOUR,
                         structureImageSubVals);
@@ -686,18 +700,8 @@ public class Commands
                         .getColourScheme(af);
                 this.colourAlignFrame(af, imageColour);
 
-                List<StructureCommandI> extraCommands = new ArrayList<>();
-                StructureCommandsI sc;
-                switch (viewerType)
-                {
-                case JMOL:
-                  sc = new JmolCommands();
-                  break;
-                default:
-                  addWarn("Cannot export image for structure viewer "
-                          + viewerType.name() + " yet");
-                  continue;
-                }
+                /////
+                // custom image background colour
 
                 String bgcolourstring = avm.getValueFromSubValOrArg(
                         structureImageArgValue, Arg.BGCOLOUR,
@@ -705,85 +709,93 @@ public class Commands
                 Color bgcolour = null;
                 if (bgcolourstring != null && bgcolourstring.length() > 0)
                 {
+                  bgcolour = ColorUtils.parseColourString(bgcolourstring);
+                  if (bgcolour == null)
+                  {
+                    Console.warn(
+                            "Background colour string '" + bgcolourstring
+                                    + "' not recognised -- using default");
+                  }
+                }
+
+                JalviewStructureDisplayI sview = structureViewer
+                        .getJalviewStructureDisplay();
+
+                File sessionToRestore = null;
+
+                List<StructureCommandI> extraCommands = new ArrayList<>();
+
+                if (extraCommands.size() > 0 || bgcolour != null)
+                {
                   try
                   {
-                    if (bgcolourstring.charAt(0) == '#')
-                    {
-                      bgcolour = Color.decode(bgcolourstring);
-                    }
-                    else
-                    {
-                      Field field = Color.class.getField(bgcolourstring);
-                      bgcolour = (Color) field.get(null);
-                    }
-                  } catch (IllegalArgumentException | NoSuchFieldException
-                          | SecurityException | IllegalAccessException nfe)
+                    sessionToRestore = sview.saveSession();
+                  } catch (Throwable t)
                   {
                     Console.warn(
-                            "Background colour string '" + bgcolourstring
-                                    + "' not recognised -- using black.");
-                    bgcolour = Color.black;
+                            "Unable to save temporary session file before custom structure view export operation.");
                   }
-                  extraCommands.add(sc.setBackgroundColour(bgcolour));
                 }
 
-                // TODO MAKE THIS VIEWER INDEPENDENT!!
-                switch (viewerType)
+                ////
+                // Do temporary ops
+
+                if (bgcolour != null)
+                {
+                  sview.getBinding().setBackgroundColour(bgcolour);
+                }
+
+                sview.getBinding().executeCommands(extraCommands, false,
+                        "Executing Custom Commands");
+
+                // and export the view as an image
+                boolean success = this.checksBeforeWritingToFile(avm,
+                        subVals, false, structureImageFilename,
+                        "structure image", isError);
+
+                if (!success)
+                {
+                  continue;
+                }
+                Console.debug("Rendering image to " + structureImageFile);
+                //
+                // TODO - extend StructureViewer / Binding with makePDBImage so
+                // we can do this with every viewer
+                //
+
+                try
                 {
-                case JMOL:
-                  JalviewStructureDisplayI sview = structureViewer
-                          .getJalviewStructureDisplay();
-                  JmolCommands jc = (JmolCommands) sc;
-                  if (sview instanceof AppJmol)
+                  // We don't expect class cast exception
+                  AppJmol jmol = (AppJmol) sview;
+                  jmol.makePDBImage(structureImageFile, imageType, renderer,
+                          userBis);
+                  Console.info("Exported structure image to "
+                          + structureImageFile);
+
+                  // RESTORE SESSION AFTER EXPORT IF NEED BE
+                  if (sessionToRestore != null)
                   {
-                    AppJmol jmol = (AppJmol) sview;
-                    JalviewJmolBinding jmb = (JalviewJmolBinding) jmol
-                            .getBinding();
-                    String state = new StringBuilder()
-                            .append("JalviewCommandsStructureState_")
-                            .append(viewerType.name()).toString();
-                    jmb.executeCommand(jc.saveState(state), false);
-                    for (StructureCommandI scmd : extraCommands)
-                    {
-                      jmb.executeCommand(scmd, false);
-                    }
-                    try
-                    {
-                      boolean success = this.checksBeforeWritingToFile(avm,
-                              subVals, false, structureImageFilename,
-                              "structure image", isError);
-                      if (!success)
-                      {
-                        continue;
-                      }
-
-                      Console.debug(
-                              "Rendering image to " + structureImageFile);
-                      jmol.makePDBImage(structureImageFile, imageType,
-                              renderer, userBis);
-                      Console.debug("Finished Rendering image to "
-                              + structureImageFile);
-
-                    } catch (ImageOutputException ioexc)
-                    {
-                      addError("Unexpected error whilst exporting image to "
-                              + structureImageFile, ioexc);
-                      isError = true;
-                      continue;
-                    } finally
-                    {
-                      jmb.executeCommand(jc.restoreState(state), false);
-                    }
+                    Console.debug("Restoring session from "
+                            + sessionToRestore);
+                    
+                    sview.getBinding().restoreSession(sessionToRestore.getAbsolutePath());
 
                   }
-                  break;
-                default:
-                  // this shouldn't happen!
-                  addWarn("Cannot export image for structure viewer "
-                          + viewerType.name() + " yet");
+                } catch (ImageOutputException ioexec)
+                {
+                  addError(
+                          "Unexpected error when restoring structure viewer session after custom view operations.");
+                  isError = true;
                   continue;
+                } finally
+                {
+                  try {
+                    this.colourAlignFrame(af, originalColourScheme);
+                  } catch (Exception t)
+                  {
+                    addError("Unexpected error when restoring colourscheme to alignment after temporary change for export.",t);
+                  }
                 }
-                this.colourAlignFrame(af, originalColourScheme);
               }
             }
           }