X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2FImageMaker.java;h=7840261350343f5a104ef3eb75a63c441213a96c;hb=3d0101179759ef157b088ea135423cd909512d9f;hp=1c6b92b55bd30a67fc312e85e1fc48e66da011f1;hpb=59d682209891099d46b960509907c79e3fb276fe;p=jalview.git diff --git a/src/jalview/util/ImageMaker.java b/src/jalview/util/ImageMaker.java index 1c6b92b..7840261 100755 --- a/src/jalview/util/ImageMaker.java +++ b/src/jalview/util/ImageMaker.java @@ -1,71 +1,148 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8) - * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors * * This file is part of Jalview. * * Jalview is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. * * Jalview is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License along with Jalview. If not, see . + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. */ package jalview.util; -import java.io.*; -import javax.imageio.*; +import jalview.bin.Jalview; +import jalview.gui.EPSOptions; +import jalview.gui.IProgressIndicator; +import jalview.gui.SVGOptions; +import jalview.io.JalviewFileChooser; -import java.awt.*; -import java.awt.image.*; +import java.awt.Component; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.FileOutputStream; -import org.jibble.epsgraphics.*; -import jalview.gui.*; -import jalview.io.*; +import javax.imageio.ImageIO; + +import org.jfree.graphics2d.svg.SVGGraphics2D; +import org.jfree.graphics2d.svg.SVGHints; +import org.jibble.epsgraphics.EpsGraphics2D; public class ImageMaker { - public static final int EPS = 0; + public static final String SVG_DESCRIPTION = "Scalable Vector Graphics"; + + public static final String SVG_EXTENSION = "svg"; + + public static final String EPS_DESCRIPTION = "Encapsulated Postscript"; + + public static final String EPS_EXTENSION = "eps"; - public static final int PNG = 1; + public static final String PNG_EXTENSION = "png"; - int type = -1; + public static final String PNG_DESCRIPTION = "Portable network graphics"; + + public static final String HTML_EXTENSION = "html"; + + public static final String HTML_DESCRIPTION = "Hypertext Markup Language"; EpsGraphics2D pg; + SVGGraphics2D g2; + Graphics graphics; FileOutputStream out; BufferedImage bi; - public ImageMaker(Component parent, int type, String title, int width, - int height, File file, String EPStitle) + TYPE type; + + private IProgressIndicator pIndicator; + + private long pSessionId; + + private boolean headless; + + public enum TYPE { - this.type = type; + EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()), + PNG("PNG", MessageManager.getString("label.png_image"), + getPNGChooser()), + SVG("SVG", "SVG", getSVGChooser()); + + private JalviewFileChooser chooser; + private String name; + + private String label; + + TYPE(String name, String label, JalviewFileChooser chooser) + { + this.name = name; + this.label = label; + this.chooser = chooser; + } + + public String getName() + { + return name; + } + + public JalviewFileChooser getChooser() + { + return chooser; + } + + public String getLabel() + { + return label; + } + + } + + public ImageMaker(Component parent, TYPE type, String title, int width, + int height, File file, String fileTitle, + IProgressIndicator pIndicator, long pSessionId, boolean headless) + { + this.pIndicator = pIndicator; + this.type = type; + this.pSessionId = pSessionId; + this.headless = headless; if (file == null) { + setProgressMessage(MessageManager.formatMessage( + "status.waiting_for_user_to_select_output_file", type.name)); JalviewFileChooser chooser; - chooser = type == EPS ? getEPSChooser() : getPNGChooser(); - + chooser = type.getChooser(); chooser.setFileView(new jalview.io.JalviewFileView()); chooser.setDialogTitle(title); - chooser.setToolTipText("Save"); - + chooser.setToolTipText(MessageManager.getString("action.save")); int value = chooser.showSaveDialog(parent); if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION) { - jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser - .getSelectedFile().getParent()); - + jalview.bin.Cache.setProperty("LAST_DIRECTORY", + chooser.getSelectedFile().getParent()); file = chooser.getSelectedFile(); } + else + { + setProgressMessage(MessageManager.formatMessage( + "status.cancelled_image_export_operation", type.name)); + } } if (file != null) @@ -73,19 +150,28 @@ public class ImageMaker try { out = new FileOutputStream(file); - - if (type == EPS) + setProgressMessage(null); + setProgressMessage(MessageManager.formatMessage( + "status.exporting_alignment_as_x_file", type.getName())); + if (type == TYPE.SVG) { - setupEPS(width, height, EPStitle); + setupSVG(width, height, fileTitle); } - else + else if (type == TYPE.EPS) + { + setupEPS(width, height, fileTitle); + } + else if (type == TYPE.PNG) { setupPNG(width, height); } + } catch (Exception ex) { - System.out.println("Error creating " - + (type == EPS ? "EPS" : "PNG") + " file."); + System.out.println("Error creating " + type.getName() + " file."); + + setProgressMessage(MessageManager + .formatMessage("info.error_creating_file", type.getName())); } } } @@ -95,15 +181,6 @@ public class ImageMaker return graphics; } - void setupPNG(int width, int height) - { - bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); - graphics = bi.getGraphics(); - Graphics2D ig2 = (Graphics2D) graphics; - ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - RenderingHints.VALUE_ANTIALIAS_ON); - } - public void writeImage() { try @@ -114,8 +191,15 @@ public class ImageMaker pg.flush(); pg.close(); break; + case SVG: + String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument(); + out.write(svgData.getBytes()); + out.flush(); + out.close(); + break; case PNG: - ImageIO.write(bi, "png", out); + ImageIO.write(bi, PNG_EXTENSION, out); + out.flush(); out.close(); break; } @@ -143,6 +227,8 @@ public class ImageMaker if (renderStyle == null || eps.cancelled) { + setProgressMessage(MessageManager.formatMessage( + "status.cancelled_image_export_operation", "EPS")); return; } } @@ -155,31 +241,101 @@ public class ImageMaker try { pg = new EpsGraphics2D(title, out, 0, 0, width, height); - Graphics2D ig2 = (Graphics2D) pg; + Graphics2D ig2 = pg; ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); pg.setAccurateTextMode(accurateText); graphics = pg; + setProgressMessage(MessageManager + .formatMessage("status.export_complete", type.getName())); } catch (Exception ex) { } } - JalviewFileChooser getPNGChooser() + void setupPNG(int width, int height) { - return new jalview.io.JalviewFileChooser( - jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[] - { "png" }, new String[] - { "Portable network graphics" }, "Portable network graphics"); + bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + graphics = bi.getGraphics(); + Graphics2D ig2 = (Graphics2D) graphics; + ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + setProgressMessage(MessageManager + .formatMessage("status.export_complete", type.getName())); + } - JalviewFileChooser getEPSChooser() + void setupSVG(int width, int height, String title) { - return new jalview.io.JalviewFileChooser( - jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[] - { "eps" }, new String[] - { "Encapsulated Postscript" }, "Encapsulated Postscript"); + + g2 = new SVGGraphics2D(width, height); + Graphics2D ig2 = g2; + + String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING", + "Prompt each time"); + + // If we need to prompt, and if the GUI is visible then + // Prompt for EPS rendering style + if (renderStyle.equalsIgnoreCase("Prompt each time") + && !(System.getProperty("java.awt.headless") != null && System + .getProperty("java.awt.headless").equals("true"))) + { + SVGOptions svgOption = new SVGOptions(); + renderStyle = svgOption.getValue(); + + if (renderStyle == null || svgOption.cancelled) + { + setProgressMessage(MessageManager.formatMessage( + "status.cancelled_image_export_operation", "SVG")); + return; + } + } + + if (renderStyle.equalsIgnoreCase("Lineart")) + { + ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE, + SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR); + } + + setProgressMessage(MessageManager + .formatMessage("status.export_complete", type.getName())); + graphics = g2; + } + + static JalviewFileChooser getPNGChooser() + { + if (Jalview.isHeadlessMode()) + { + return null; + } + return new JalviewFileChooser(PNG_EXTENSION, PNG_DESCRIPTION); + } + + static JalviewFileChooser getEPSChooser() + { + if (Jalview.isHeadlessMode()) + { + return null; + } + return new JalviewFileChooser(EPS_EXTENSION, EPS_DESCRIPTION); + } + + private void setProgressMessage(String message) + { + if (pIndicator != null && !headless) + { + pIndicator.setProgressBar(message, pSessionId); + } + } + + static JalviewFileChooser getSVGChooser() + { + if (Jalview.isHeadlessMode()) + { + return null; + } + return new JalviewFileChooser(SVG_EXTENSION, SVG_DESCRIPTION); } }