JAL-3210 Merge branch 'develop' into trialMerge
[jalview.git] / src / jalview / util / ImageMaker.java
index 7232042..e89596b 100755 (executable)
  */
 package jalview.util;
 
-import jalview.bin.Jalview;
-import jalview.gui.EPSOptions;
-import jalview.gui.IProgressIndicator;
-import jalview.gui.SVGOptions;
 import jalview.io.JalviewFileChooser;
 
-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 java.io.IOException;
 
 import javax.imageio.ImageIO;
 
@@ -54,14 +50,8 @@ public class ImageMaker
 
   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;
@@ -70,27 +60,21 @@ public class ImageMaker
 
   TYPE type;
 
-  private IProgressIndicator pIndicator;
-
-  private long pSessionId;
-
-  private boolean headless;
-
   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),
+    PNG("PNG", MessageManager.getString("label.png_image"), PNG_EXTENSION,
+            PNG_DESCRIPTION),
     SVG("SVG", "SVG", SVG_EXTENSION, SVG_DESCRIPTION);
 
-    private String name;
+    public final String name;
 
-    private String label;
+    public final String label;
 
-    private String extension;
+    public final String extension;
 
-    private String description;
+    public final String description;
 
     TYPE(String name, String label, String ext, String desc)
     {
@@ -117,66 +101,36 @@ public class ImageMaker
 
   }
 
-  public ImageMaker(Component parent, TYPE type, String title, int width,
-          int height, File file, String fileTitle,
-          IProgressIndicator pIndicator, long pSessionId, boolean headless)
+  /**
+   * 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.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.getFileChooser();
-      chooser.setFileView(new jalview.io.JalviewFileView());
-      chooser.setDialogTitle(title);
-      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());
-        file = chooser.getSelectedFile();
-      }
-      else
-      {
-        setProgressMessage(MessageManager.formatMessage(
-                "status.cancelled_image_export_operation", type.name));
-      }
-    }
-
-    if (file != null)
-    {
-      try
-      {
-        out = new FileOutputStream(file);
-        setProgressMessage(null);
-        setProgressMessage(MessageManager.formatMessage(
-                "status.exporting_alignment_as_x_file", type.getName()));
-        if (type == TYPE.SVG)
-        {
-          setupSVG(width, height, fileTitle);
-        }
-        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.getName() + " file.");
-
-        setProgressMessage(MessageManager
-                .formatMessage("info.error_creating_file", type.getName()));
-      }
+    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:
     }
   }
 
@@ -185,6 +139,11 @@ public class ImageMaker
     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
@@ -213,133 +172,60 @@ public class ImageMaker
     }
   }
 
-  void setupEPS(int width, int height, String title)
-  {
-    boolean accurateText = true;
-
-    String renderStyle = jalview.bin.Cache.getDefault("EPS_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")))
-    {
-      EPSOptions eps = new EPSOptions();
-      renderStyle = eps.getValue();
-
-      if (renderStyle == null || eps.cancelled)
-      {
-        setProgressMessage(MessageManager.formatMessage(
-                "status.cancelled_image_export_operation", "EPS"));
-        return;
-      }
-    }
-
-    if (renderStyle.equalsIgnoreCase("text"))
-    {
-      accurateText = false;
-    }
-
-    try
-    {
-      pg = new EpsGraphics2D(title, out, 0, 0, width, height);
-      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)
-    {
-    }
-  }
-
-  void setupPNG(int width, int height)
+  /**
+   * 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);
-    setProgressMessage(MessageManager
-            .formatMessage("status.export_complete", type.getName()));
-
   }
 
-  void setupSVG(int width, int height, String title)
+  /**
+   * 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)
   {
-
-    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")))
+    SVGGraphics2D g2 = new SVGGraphics2D(width, height);
+    if (useLineart)
     {
-      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,
+      g2.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()
+  /**
+   * 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
   {
-    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);
+    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;
   }
 }