X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FHtmlSvgOutput.java;h=1f828538b2fe13f1c7fb339658f88dd5d3943d20;hb=1e28a196997a1e0b8b74d468bfd3df8ec74c1337;hp=a18418598e08b4ed6329b46d7cfc7ee373b55e3c;hpb=cee0842270af0aed7091576f3b414fb8a4f96e29;p=jalview.git diff --git a/src/jalview/io/HtmlSvgOutput.java b/src/jalview/io/HtmlSvgOutput.java index a184185..1f82853 100644 --- a/src/jalview/io/HtmlSvgOutput.java +++ b/src/jalview/io/HtmlSvgOutput.java @@ -20,25 +20,28 @@ */ package jalview.io; -import jalview.exceptions.NoFileSelectedException; +import jalview.bin.Cache; import jalview.gui.AlignmentPanel; import jalview.gui.LineartOptions; import jalview.gui.OOMWarning; import jalview.math.AlignmentDimension; import jalview.util.MessageManager; +import jalview.util.dialogrunner.RunResponse; import java.awt.Graphics; import java.awt.print.PrinterException; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.util.concurrent.atomic.AtomicBoolean; + +import javax.swing.JOptionPane; import org.jfree.graphics2d.svg.SVGGraphics2D; import org.jfree.graphics2d.svg.SVGHints; public class HtmlSvgOutput extends HTMLOutput { - public HtmlSvgOutput(AlignmentPanel ap) { super(ap); @@ -53,13 +56,14 @@ public class HtmlSvgOutput extends HTMLOutput if (outputFile == null) { outputFile = getOutputFile(); + if (outputFile == null) + { + setProgressMessage(MessageManager.formatMessage( + "status.cancelled_image_export_operation", "HTML")); + return; + } } generatedFile = new File(outputFile); - } catch (NoFileSelectedException e) - { - setProgressMessage(MessageManager.formatMessage( - "status.cancelled_image_export_operation", "HTML")); - return; } catch (Exception e) { setProgressMessage(MessageManager @@ -84,7 +88,7 @@ public class HtmlSvgOutput extends HTMLOutput return ap.printWrappedAlignment(pwidth, pheight, pi, pg[0]); } - private String getHtml(String titleSvg, String alignmentSvg, + String getHtml(String titleSvg, String alignmentSvg, String jsonData, boolean wrapped) { StringBuilder htmlSvg = new StringBuilder(); @@ -225,49 +229,94 @@ public class HtmlSvgOutput extends HTMLOutput } @Override - public File getExportedFile() - { - return generatedFile; - } - - @Override public void run() { try { setProgressMessage(null); setProgressMessage(MessageManager.formatMessage( - "status.exporting_alignment_as_x_file", "HTML")); - AlignmentDimension aDimension = ap.getAlignmentDimension(); - SVGGraphics2D idPanelGraphics = new SVGGraphics2D( - aDimension.getWidth(), aDimension.getHeight()); - SVGGraphics2D alignPanelGraphics = new SVGGraphics2D( - aDimension.getWidth(), aDimension.getHeight()); + "status.exporting_alignment_as_x_file", getDescription())); - String renderStyle = jalview.bin.Cache.getDefault("HTML_RENDERING", + String renderStyle = Cache.getDefault("HTML_RENDERING", "Prompt each time"); + AtomicBoolean textOption = new AtomicBoolean( + !"Lineart".equals(renderStyle)); - // If we need to prompt, and if the GUI is visible then - // Prompt for rendering style - if (renderStyle.equalsIgnoreCase("Prompt each time") && !isHeadless()) + /* + * configure the action to run on OK in the dialog + */ + RunResponse okAction = new RunResponse(JOptionPane.OK_OPTION) { - LineartOptions svgOption = new LineartOptions("HTML_RENDERING", - "HTML"); - renderStyle = svgOption.getValue(); - - if (renderStyle == null || svgOption.cancelled) + @Override + public void run() { - setProgressMessage(MessageManager.formatMessage( - "status.cancelled_image_export_operation", "HTML")); - return; + doOutput(textOption.get()); } + }; + + /* + * Prompt for character rendering style if preference is not set + */ + if (renderStyle.equalsIgnoreCase("Prompt each time") && !isHeadless()) + { + LineartOptions svgOption = new LineartOptions("HTML", textOption); + svgOption.setResponseAction(1, new RunResponse(JOptionPane.NO_OPTION) + { + @Override + public void run() + { + setProgressMessage(MessageManager.formatMessage( + "status.cancelled_image_export_operation", + getDescription())); + } + }); + svgOption.setResponseAction(0, okAction); + svgOption.showDialog(); + /* no code here - JalviewJS cannot execute it */ + } + else + { + /* + * else (if preference set) just do the export action + */ + doOutput(textOption.get()); } + } catch (OutOfMemoryError err) + { + System.out.println("########################\n" + "OUT OF MEMORY " + + generatedFile + "\n" + "########################"); + new OOMWarning("Creating Image for " + generatedFile, err); + } catch (Exception e) + { + e.printStackTrace(); + setProgressMessage(MessageManager + .formatMessage("info.error_creating_file", getDescription())); + } + } - if (renderStyle.equalsIgnoreCase("Lineart")) + /** + * Builds and writes the image to the file specified by field + * generatedFile + * + * @param textCharacters + * true for Text character rendering, false for Lineart + */ + protected void doOutput(boolean textCharacters) + { + try + { + AlignmentDimension aDimension = ap.getAlignmentDimension(); + SVGGraphics2D idPanelGraphics = new SVGGraphics2D( + aDimension.getWidth(), aDimension.getHeight()); + SVGGraphics2D alignPanelGraphics = new SVGGraphics2D( + aDimension.getWidth(), aDimension.getHeight()); + if (!textCharacters) // Lineart selected { - idPanelGraphics.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE, + idPanelGraphics.setRenderingHint( + SVGHints.KEY_DRAW_STRING_TYPE, SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR); - alignPanelGraphics.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE, + alignPanelGraphics.setRenderingHint( + SVGHints.KEY_DRAW_STRING_TYPE, SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR); } if (ap.av.getWrapAlignment()) @@ -277,32 +326,33 @@ public class HtmlSvgOutput extends HTMLOutput } else { - printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0, - idPanelGraphics, alignPanelGraphics); + printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), + 0, idPanelGraphics, alignPanelGraphics); } String idPanelSvgData = idPanelGraphics.getSVGDocument(); String alignPanelSvgData = alignPanelGraphics.getSVGDocument(); String jsonData = getBioJSONData(); - String htmlData = getHtml(idPanelSvgData, alignPanelSvgData, jsonData, - ap.av.getWrapAlignment()); + String htmlData = getHtml(idPanelSvgData, alignPanelSvgData, + jsonData, ap.av.getWrapAlignment()); FileOutputStream out = new FileOutputStream(generatedFile); out.write(htmlData.getBytes()); out.flush(); out.close(); setProgressMessage(MessageManager - .formatMessage("status.export_complete", "HTML")); + .formatMessage("status.export_complete", getDescription())); exportCompleted(); - } catch (OutOfMemoryError err) - { - System.out.println("########################\n" + "OUT OF MEMORY " - + generatedFile + "\n" + "########################"); - new OOMWarning("Creating Image for " + generatedFile, err); } catch (Exception e) { e.printStackTrace(); setProgressMessage(MessageManager - .formatMessage("info.error_creating_file", "HTML")); + .formatMessage("info.error_creating_file", getDescription())); } } + + @Override + protected String getDescription() + { + return "HTML"; + } }