JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / jalview / util / ImageMaker.java
index 59244eb..0cd017f 100755 (executable)
-package jalview.util;\r
-\r
-import java.awt.event.ActionEvent;\r
-import java.awt.RenderingHints;\r
-import javax.imageio.ImageIO;\r
-import org.jibble.epsgraphics.EpsGraphics2D;\r
-import jalview.gui.EPSOptions;\r
-import java.awt.Graphics2D;\r
-import java.io.*;\r
-import java.awt.image.BufferedImage;\r
-import java.awt.Graphics;\r
-import jalview.io.*;\r
-import java.awt.*;\r
-\r
-\r
-public class ImageMaker\r
-{\r
-  public static final int EPS = 0;\r
-  public static final int PNG = 1;\r
-  int type = -1;\r
-\r
-  EpsGraphics2D pg;\r
-  Graphics graphics;\r
-  FileOutputStream out;\r
-  BufferedImage bi;\r
-\r
-  public ImageMaker(Component parent, int type, String title,\r
-                           int width, int height, File file, String EPStitle)\r
-  {\r
-    this.type = type;\r
-\r
-    if (file == null)\r
-    {\r
-      JalviewFileChooser chooser;\r
-      chooser = type == EPS ? getEPSChooser() : getPNGChooser();\r
-\r
-      chooser.setFileView(new jalview.io.JalviewFileView());\r
-      chooser.setDialogTitle(title);\r
-      chooser.setToolTipText("Save");\r
-\r
-      int value = chooser.showSaveDialog(parent);\r
-\r
-      if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)\r
-      {\r
-        jalview.bin.Cache.setProperty("LAST_DIRECTORY",\r
-                                      chooser.getSelectedFile().getParent());\r
-\r
-        file = chooser.getSelectedFile();\r
-      }\r
-    }\r
-\r
-    if(file!=null)\r
-    {\r
-      try\r
-      {\r
-        out = new FileOutputStream(file);\r
-\r
-        if (type == EPS)\r
-          setupEPS(width, height, EPStitle);\r
-        else\r
-          setupPNG(width, height);\r
-      }\r
-      catch (Exception ex)\r
-      {\r
-        System.out.println("Error creating " + (type == EPS ? "EPS" : "PNG") +\r
-                           " file.");\r
-      }\r
-    }\r
-  }\r
-\r
-  public Graphics getGraphics()\r
-  {\r
-    return graphics;\r
-  }\r
-\r
-\r
-  void setupPNG(int width, int height)\r
-  {\r
-        bi = new BufferedImage(width, height,\r
-                BufferedImage.TYPE_INT_RGB);\r
-        graphics  = bi.getGraphics();\r
-        Graphics2D ig2 = (Graphics2D) graphics;\r
-        ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,\r
-                                 RenderingHints.VALUE_ANTIALIAS_ON);\r
-  }\r
-\r
-  public void writeImage()\r
-  {\r
-    try{\r
-      switch(type)\r
-      {\r
-        case EPS:\r
-          pg.flush();\r
-          pg.close();\r
-          break;\r
-        case PNG:\r
-          ImageIO.write(bi, "png", out);\r
-          out.close();\r
-          break;\r
-      }\r
-    }\r
-    catch (Exception ex)\r
-    {\r
-        ex.printStackTrace();\r
-    }\r
-  }\r
-\r
-  void setupEPS(int width, int height, String title)\r
-  {\r
-    boolean accurateText = true;\r
-\r
-    String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",\r
-        "Prompt each time");\r
-\r
-    // If we need to prompt, and if the GUI is visible then\r
-    // Prompt for EPS rendering style\r
-    if (renderStyle.equalsIgnoreCase("Prompt each time")\r
-        && !\r
-        (System.getProperty("java.awt.headless") != null\r
-         && System.getProperty("java.awt.headless").equals("true")))\r
-    {\r
-      EPSOptions eps = new EPSOptions();\r
-      renderStyle = eps.getValue();\r
-\r
-      if (renderStyle == null || eps.cancelled)\r
-        return;\r
-    }\r
-\r
-    if (renderStyle.equalsIgnoreCase("text"))\r
-    {\r
-      accurateText = false;\r
-    }\r
-\r
-    try   {\r
-      pg = new EpsGraphics2D(title, out, 0, 0, width,\r
-                                           height);\r
-      Graphics2D ig2 = (Graphics2D) pg;\r
-      ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,\r
-                           RenderingHints.VALUE_ANTIALIAS_ON);\r
-\r
-      pg.setAccurateTextMode(accurateText);\r
-\r
-      graphics = pg;\r
-    }\r
-    catch (Exception ex) {  }\r
-  }\r
-\r
-\r
-  JalviewFileChooser getPNGChooser()\r
-  {\r
-    return new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(\r
-        "LAST_DIRECTORY"), new String[]\r
-                                             {"png"},\r
-                                             new String[]\r
-                                             {"Portable network graphics"},\r
-                                             "Portable network graphics");\r
-  }\r
-\r
-  JalviewFileChooser getEPSChooser()\r
-  {\r
-    return new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(\r
-        "LAST_DIRECTORY"), new String[]\r
-                                             {"eps"},\r
-                                             new String[]\r
-                                             {"Encapsulated Postscript"},\r
-                                             "Encapsulated Postscript");\r
-  }\r
-}\r
+/*
+ * 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.
+ *  
+ * 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 <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+package jalview.util;
+
+import jalview.io.JalviewFileChooser;
+
+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 java.io.IOException;
+
+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 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 String PNG_EXTENSION = "png";
+
+  public static final String PNG_DESCRIPTION = "Portable  network graphics";
+
+  EpsGraphics2D pg;
+
+  Graphics graphics;
+
+  FileOutputStream out;
+
+  BufferedImage bi;
+
+  TYPE type;
+
+  public enum TYPE
+  {
+    EPS("EPS", MessageManager.getString("label.eps_file"), EPS_EXTENSION,
+            EPS_DESCRIPTION),
+    PNG("PNG", MessageManager.getString("label.png_image"), PNG_EXTENSION,
+            PNG_DESCRIPTION),
+    SVG("SVG", "SVG", SVG_EXTENSION, SVG_DESCRIPTION);
+
+    public final String name;
+
+    public final String label;
+
+    public final String extension;
+
+    public final String description;
+
+    TYPE(String name, String label, String ext, String desc)
+    {
+      this.name = name;
+      this.label = label;
+      this.extension = ext;
+      this.description = desc;
+    }
+
+    public String getName()
+    {
+      return name;
+    }
+
+    public JalviewFileChooser getFileChooser()
+    {
+      return new JalviewFileChooser(extension, description);
+    }
+
+    public String getLabel()
+    {
+      return label;
+    }
+
+  }
+
+  /**
+   * Constructor configures the graphics context ready for writing to
+   * 
+   * @param imageType
+   * @param width
+   * @param height
+   * @param file
+   * @param fileTitle
+   * @param useLineart
+   * @throws IOException
+   */
+  public ImageMaker(TYPE imageType, int width, int height, File file,
+          String fileTitle, boolean useLineart) throws IOException
+  {
+    this.type = imageType;
+
+    out = new FileOutputStream(file);
+    switch (imageType)
+    {
+    case SVG:
+      setupSVG(width, height, useLineart);
+      break;
+    case EPS:
+      setupEPS(width, height, fileTitle, useLineart);
+      break;
+    case PNG:
+      setupPNG(width, height);
+      break;
+    default:
+    }
+  }
+
+  public Graphics getGraphics()
+  {
+    return graphics;
+  }
+
+  /**
+   * For SVG or PNG, writes the generated graphics data to the file output
+   * stream. For EPS, flushes the output graphics (which is written to file as
+   * it is generated).
+   */
+  public void writeImage()
+  {
+    try
+    {
+      switch (type)
+      {
+      case EPS:
+        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_EXTENSION, out);
+        out.flush();
+        out.close();
+        break;
+      }
+    } catch (Exception ex)
+    {
+      ex.printStackTrace();
+    }
+  }
+
+  /**
+   * Sets up a graphics object for the PNG image to be written on
+   * 
+   * @param width
+   * @param height
+   */
+  protected 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);
+  }
+
+  /**
+   * A helper method to configure the SVG output graphics, with choice of Text
+   * or Lineart character rendering
+   * 
+   * @param width
+   * @param height
+   * @param useLineart
+   *          true for Lineart character rendering, false for Text
+   */
+  protected void setupSVG(int width, int height, boolean useLineart)
+  {
+    SVGGraphics2D g2 = new SVGGraphics2D(width, height);
+    if (useLineart)
+    {
+      g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
+              SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
+    }
+    graphics = g2;
+  }
+
+  /**
+   * A helper method that sets up the EPS graphics output with user choice of
+   * Text or Lineart character rendering
+   * 
+   * @param width
+   * @param height
+   * @param title
+   * @param useLineart
+   *          true for Lineart character rendering, false for Text
+   * @throws IOException
+   */
+  protected void setupEPS(int width, int height, String title,
+          boolean useLineart) throws IOException
+  {
+    pg = new EpsGraphics2D(title, out, 0, 0, width, height);
+    Graphics2D ig2 = pg;
+    ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+            RenderingHints.VALUE_ANTIALIAS_ON);
+    pg.setAccurateTextMode(useLineart);
+    graphics = pg;
+  }
+}