X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fbin%2FCommands.java;h=5d3b50d69414d0721327d32ab3c52148929fe109;hb=ca160187a050f6d4e50158cd5b51f75c83a7179e;hp=737c9eb94a9903d46e8f65c4713de5d56b166f67;hpb=d79e3747b8ce78b1ac7aa83402e31b7e5dccfe1b;p=jalview.git diff --git a/src/jalview/bin/Commands.java b/src/jalview/bin/Commands.java index 737c9eb..5d3b50d 100644 --- a/src/jalview/bin/Commands.java +++ b/src/jalview/bin/Commands.java @@ -1,5 +1,6 @@ package jalview.bin; +import java.awt.Color; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; @@ -46,8 +47,10 @@ import jalview.io.NewickFile; import jalview.io.exceptions.ImageOutputException; import jalview.schemes.ColourSchemeI; import jalview.schemes.ColourSchemeProperty; +import jalview.structure.StructureCommandI; 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; @@ -142,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)) @@ -438,6 +445,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(); @@ -672,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); @@ -679,69 +700,106 @@ public class Commands .getColourScheme(af); this.colourAlignFrame(af, imageColour); - List 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"); + ///// + // custom image background colour - 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) == '#') + bgcolour = ColorUtils.parseColourString(bgcolourstring); + if (bgcolour == null) { - bgcolour = "[x" + bgcolour.substring(1) + "]"; + Console.warn( + "Background colour string '" + bgcolourstring + + "' not recognised -- using default"); } - extraCommands.add("background " + bgcolour); } - // TODO MAKE THIS VIEWER INDEPENDENT!! - switch (StructureViewer.getViewerType()) + JalviewStructureDisplayI sview = structureViewer + .getJalviewStructureDisplay(); + + File sessionToRestore = null; + + List extraCommands = new ArrayList<>(); + + if (extraCommands.size() > 0 || bgcolour != null) { - case JMOL: - JalviewStructureDisplayI sview = structureViewer - .getJalviewStructureDisplay(); - if (sview instanceof AppJmol) + try { - AppJmol jmol = (AppJmol) sview; - 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, extraCommands); - Console.debug("Finished Rendering image to " - + structureImageFile); - - } catch (ImageOutputException ioexc) - { - addError("Unexpected error whilst exporting image to " - + structureImageFile, ioexc); - isError = true; - continue; - } + sessionToRestore = sview.saveSession(); + } catch (Throwable t) + { + Console.warn( + "Unable to save temporary session file before custom structure view export operation."); + } + } + + //// + // 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 + { + // 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) + { + Console.debug("Restoring session from " + + sessionToRestore); + + sview.getBinding().restoreSession(sessionToRestore.getAbsolutePath()); } - break; - default: - addWarn("Cannot export image for structure viewer " - + structureViewer.getViewerType() + " 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); } } } + argParser.setStructureFilename(null); } } }