JAL-4265 Change to use JmolCommands
authorBen Soares <b.soares@dundee.ac.uk>
Tue, 19 Sep 2023 21:36:19 +0000 (22:36 +0100)
committerBen Soares <b.soares@dundee.ac.uk>
Tue, 19 Sep 2023 21:36:19 +0000 (22:36 +0100)
src/jalview/bin/Commands.java
src/jalview/ext/jmol/JmolCommands.java
src/jalview/gui/AppJmol.java

index 737c9eb..592ac85 100644 (file)
@@ -1,7 +1,9 @@
 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;
@@ -23,6 +25,8 @@ 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;
@@ -46,6 +50,8 @@ import jalview.io.NewickFile;
 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.FileUtils;
@@ -438,6 +444,7 @@ public class Commands
         commandArgsProvided = true;
         for (ArgValue av : avm.getArgValueList(Arg.STRUCTURE))
         {
+          argParser.setStructureFilename(null);
           String val = av.getValue();
           SubVals subVals = av.getSubVals();
           int argIndex = av.getArgIndex();
@@ -679,33 +686,67 @@ public class Commands
                         .getColourScheme(af);
                 this.colourAlignFrame(af, imageColour);
 
-                List<String> extraCommands = new ArrayList<>();
-                // these don't actually do anything to the output image since we
-                // renderScreenImage
-                // extraCommands.add("set antialiasImages on");
-                // extraCommands.add("set antialiasTranslucent on");
+                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;
+                }
 
-                String bgcolour = avm.getValueFromSubValOrArg(
+                String bgcolourstring = avm.getValueFromSubValOrArg(
                         structureImageArgValue, Arg.BGCOLOUR,
                         structureImageSubVals);
-                if (bgcolour != null && bgcolour.length() > 0)
+                Color bgcolour = null;
+                if (bgcolourstring != null && bgcolourstring.length() > 0)
                 {
-                  if (bgcolour.charAt(0) == '#')
+                  try
                   {
-                    bgcolour = "[x" + bgcolour.substring(1) + "]";
+                    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)
+                  {
+                    Console.warn(
+                            "Background colour string '" + bgcolourstring
+                                    + "' not recognised -- using black.");
+                    bgcolour = Color.black;
                   }
-                  extraCommands.add("background " + bgcolour);
+                  extraCommands.add(sc.setBackgroundColour(bgcolour));
                 }
 
                 // TODO MAKE THIS VIEWER INDEPENDENT!!
-                switch (StructureViewer.getViewerType())
+                switch (viewerType)
                 {
                 case JMOL:
                   JalviewStructureDisplayI sview = structureViewer
                           .getJalviewStructureDisplay();
+                  JmolCommands jc = (JmolCommands) sc;
                   if (sview instanceof AppJmol)
                   {
                     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,
@@ -719,7 +760,7 @@ public class Commands
                       Console.debug(
                               "Rendering image to " + structureImageFile);
                       jmol.makePDBImage(structureImageFile, imageType,
-                              renderer, userBis, extraCommands);
+                              renderer, userBis);
                       Console.debug("Finished Rendering image to "
                               + structureImageFile);
 
@@ -729,19 +770,24 @@ public class Commands
                               + structureImageFile, ioexc);
                       isError = true;
                       continue;
+                    } finally
+                    {
+                      jmb.executeCommand(jc.restoreState(state), false);
                     }
 
                   }
                   break;
                 default:
+                  // this shouldn't happen!
                   addWarn("Cannot export image for structure viewer "
-                          + structureViewer.getViewerType() + " yet");
+                          + viewerType.name() + " yet");
                   continue;
                 }
                 this.colourAlignFrame(af, originalColourScheme);
               }
             }
           }
+          argParser.setStructureFilename(null);
         }
       }
     }
index 19d64f0..e621ba5 100644 (file)
@@ -500,4 +500,14 @@ public class JmolCommands extends StructureCommandsBase
   {
     return null; // not an external viewer
   }
+
+  public StructureCommandI saveState(String label)
+  {
+    return new StructureCommand("save STATE " + label);
+  }
+
+  public StructureCommandI restoreState(String label)
+  {
+    return new StructureCommand("restore STATE " + label);
+  }
 }
index 5b935b7..ba4eb9c 100644 (file)
@@ -434,7 +434,7 @@ public class AppJmol extends StructureViewerBase
     try
     {
       makePDBImage(null, type, null,
-              BitmapImageSizing.defaultBitmapImageSizing(), null);
+              BitmapImageSizing.defaultBitmapImageSizing());
     } catch (ImageOutputException ioex)
     {
       Console.error("Unexpected error whilst writing " + type.toString(),
@@ -443,8 +443,7 @@ public class AppJmol extends StructureViewerBase
   }
 
   public void makePDBImage(File file, ImageMaker.TYPE type, String renderer,
-          BitmapImageSizing userBis, List<String> extraCommands)
-          throws ImageOutputException
+          BitmapImageSizing userBis) throws ImageOutputException
   {
     int width = getWidth();
     int height = getHeight();
@@ -473,24 +472,8 @@ public class AppJmol extends StructureViewerBase
           }
         }
 
-        String saveStateNameTemp = "JalviewSaveStateTemp";
-        jmb.executeCommand(
-                new StructureCommand("save STATE " + saveStateNameTemp),
-                false);
-
-        if (extraCommands != null)
-        {
-          for (String cmd : extraCommands)
-          {
-            jmb.executeCommand(new StructureCommand(cmd), false);
-          }
-        }
-
         jmb.jmolViewer.requestRepaintAndWait("image export");
         jmb.jmolViewer.renderScreenImage(ig2, usewidth, useheight);
-        jmb.executeCommand(
-                new StructureCommand("restore STATE " + saveStateNameTemp),
-                false);
       }
     };
     String view = MessageManager.getString("action.view")