JAL-4265 bail early when attempting to export a view from other viewers than Jmol...
authorJames Procter <j.procter@dundee.ac.uk>
Wed, 20 Sep 2023 13:22:11 +0000 (14:22 +0100)
committerJames Procter <j.procter@dundee.ac.uk>
Wed, 20 Sep 2023 13:22:11 +0000 (14:22 +0100)
src/jalview/bin/Commands.java

index 592ac85..65030a4 100644 (file)
@@ -54,6 +54,7 @@ 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;
@@ -679,6 +680,18 @@ 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 +699,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,
@@ -707,6 +710,9 @@ public class Commands
                 {
                   try
                   {
+                    // 
+                    // FIXME: Why not use ColorUtils.parseColourString(bgcolourstring) - this is consistent and backwards compatible
+                    //
                     if (bgcolourstring.charAt(0) == '#')
                     {
                       bgcolour = Color.decode(bgcolourstring);
@@ -721,69 +727,80 @@ public class Commands
                   {
                     Console.warn(
                             "Background colour string '" + bgcolourstring
-                                    + "' not recognised -- using black.");
-                    bgcolour = Color.black;
+                                    + "' not recognised -- using default");
+                    //bgcolour = Color.black;
                   }
-                  extraCommands.add(sc.setBackgroundColour(bgcolour));
                 }
 
-                // TODO MAKE THIS VIEWER INDEPENDENT!!
-                switch (viewerType)
+                JalviewStructureDisplayI sview = structureViewer
+                        .getJalviewStructureDisplay();
+
+                File sessionToRestore = null;
+                
+                List<StructureCommandI> extraCommands=new ArrayList<>();
+                
+                if (extraCommands.size() > 0 || bgcolour!=null)
                 {
-                case JMOL:
-                  JalviewStructureDisplayI sview = structureViewer
-                          .getJalviewStructureDisplay();
-                  JmolCommands jc = (JmolCommands) sc;
-                  if (sview instanceof AppJmol)
+                  try {
+                    sessionToRestore = sview.saveSession();
+                  } catch (Throwable t)
                   {
-                    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,
+                    Console.warn("Unable to save temporary session file before custom structure view export operation.");
+                  }
+                }
+                
+                ////
+                // Do temporary ops 
+
+                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 {
+                  // We don't expect class cast exception
+                  AppJmol jmol = (AppJmol) sview;
+                  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("Finished Rendering image to "
+                          + structureImageFile);
+    
+                  // RESTORE SESSION AFTER EXPORT IF NEED BE
+                  if (sessionToRestore != null)
+                  {
+                    sview.getBinding().openSession(sessionToRestore.getCanonicalPath());
                   }
-                  break;
-                default:
-                  // this shouldn't happen!
-                  addWarn("Cannot export image for structure viewer "
-                          + viewerType.name() + " yet");
+                } catch (ImageOutputException ioexc)
+                {
+                  addError("Unexpected error whilst exporting image to "
+                          + structureImageFile, ioexc);
+                  isError = true;
+                  continue;
+                }
+                catch (IOException ioexec)
+                {
+                  addError("Unexpected error when restoring structure viewer session after custom view operations.");
+                  isError = true;
                   continue;
                 }
-                this.colourAlignFrame(af, originalColourScheme);
+                finally
+                {
+                  this.colourAlignFrame(af, originalColourScheme);
+                }
               }
             }
           }