JAL-3048 inlined 1-line methods, small refactor of ImageMaker.TYPE
[jalview.git] / src / jalview / util / ImageMaker.java
index fa2533f..493e210 100755 (executable)
  */
 package jalview.util;
 
+import jalview.bin.Cache;
 import jalview.bin.Jalview;
-import jalview.gui.EPSOptions;
 import jalview.gui.IProgressIndicator;
-import jalview.gui.SVGOptions;
+import jalview.gui.LineartOptions;
 import jalview.io.JalviewFileChooser;
 
 import java.awt.Component;
@@ -78,21 +78,26 @@ public class ImageMaker
 
   public enum TYPE
   {
-    EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()),
-    PNG("PNG", MessageManager.getString("label.png_image"), getPNGChooser()),
-    SVG("SVG", "SVG", getSVGChooser());
+    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);
 
-    private JalviewFileChooser chooser;
+    public final String name;
 
-    private String name;
+    public final String label;
 
-    private String label;
+    public final String extension;
 
-    TYPE(String name, String label, JalviewFileChooser chooser)
+    public final String description;
+
+    TYPE(String name, String label, String ext, String desc)
     {
       this.name = name;
       this.label = label;
-      this.chooser = chooser;
+      this.extension = ext;
+      this.description = desc;
     }
 
     public String getName()
@@ -102,7 +107,7 @@ public class ImageMaker
 
     public JalviewFileChooser getChooser()
     {
-      return chooser;
+      return new JalviewFileChooser(extension, description);
     }
 
     public String getLabel()
@@ -112,6 +117,21 @@ public class ImageMaker
 
   }
 
+  /**
+   * Constructor builds the image and writes it to file. If the supplied file
+   * name is null, the user is prompted for the output file.
+   * 
+   * @param parent
+   * @param type
+   * @param title
+   * @param width
+   * @param height
+   * @param file
+   * @param fileTitle
+   * @param pIndicator
+   * @param pSessionId
+   * @param headless
+   */
   public ImageMaker(Component parent, TYPE type, String title, int width,
           int height, File file, String fileTitle,
           IProgressIndicator pIndicator, long pSessionId, boolean headless)
@@ -120,7 +140,7 @@ public class ImageMaker
     this.type = type;
     this.pSessionId = pSessionId;
     this.headless = headless;
-    if (file == null)
+    if (file == null && !Jalview.isHeadlessMode())
     {
       setProgressMessage(MessageManager.formatMessage(
               "status.waiting_for_user_to_select_output_file", type.name));
@@ -131,10 +151,10 @@ public class ImageMaker
       chooser.setToolTipText(MessageManager.getString("action.save"));
       int value = chooser.showSaveDialog(parent);
 
-      if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
+      if (value == JalviewFileChooser.APPROVE_OPTION)
       {
-        jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
-                .getSelectedFile().getParent());
+        Cache.setProperty("LAST_DIRECTORY",
+                chooser.getSelectedFile().getParent());
         file = chooser.getSelectedFile();
       }
       else
@@ -143,7 +163,7 @@ public class ImageMaker
                 "status.cancelled_image_export_operation", type.name));
       }
     }
-
+    // TODO JAL-3048 refactor to method called by constructor or callback
     if (file != null)
     {
       try
@@ -169,8 +189,8 @@ public class ImageMaker
       {
         System.out.println("Error creating " + type.getName() + " file.");
 
-        setProgressMessage(MessageManager.formatMessage(
-                "info.error_creating_file", type.getName()));
+        setProgressMessage(MessageManager
+                .formatMessage("info.error_creating_file", type.getName()));
       }
     }
   }
@@ -212,7 +232,7 @@ public class ImageMaker
   {
     boolean accurateText = true;
 
-    String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
+    String renderStyle = Cache.getDefault("EPS_RENDERING",
             "Prompt each time");
 
     // If we need to prompt, and if the GUI is visible then
@@ -221,7 +241,7 @@ public class ImageMaker
             && !(System.getProperty("java.awt.headless") != null && System
                     .getProperty("java.awt.headless").equals("true")))
     {
-      EPSOptions eps = new EPSOptions();
+      LineartOptions eps = new LineartOptions("EPS_RENDERING", "EPS");
       renderStyle = eps.getValue();
 
       if (renderStyle == null || eps.cancelled)
@@ -247,10 +267,11 @@ public class ImageMaker
       pg.setAccurateTextMode(accurateText);
 
       graphics = pg;
-      setProgressMessage(MessageManager.formatMessage(
-              "status.export_complete", type.getName()));
+      setProgressMessage(MessageManager
+              .formatMessage("status.export_complete", type.getName()));
     } catch (Exception ex)
     {
+      System.err.println("Error writing PNG: " + ex.toString());
     }
   }
 
@@ -261,8 +282,8 @@ public class ImageMaker
     Graphics2D ig2 = (Graphics2D) graphics;
     ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
             RenderingHints.VALUE_ANTIALIAS_ON);
-    setProgressMessage(MessageManager.formatMessage(
-            "status.export_complete", type.getName()));
+    setProgressMessage(MessageManager
+            .formatMessage("status.export_complete", type.getName()));
 
   }
 
@@ -281,7 +302,7 @@ public class ImageMaker
             && !(System.getProperty("java.awt.headless") != null && System
                     .getProperty("java.awt.headless").equals("true")))
     {
-      SVGOptions svgOption = new SVGOptions();
+      LineartOptions svgOption = new LineartOptions("SVG_RENDERING", "SVG");
       renderStyle = svgOption.getValue();
 
       if (renderStyle == null || svgOption.cancelled)
@@ -298,8 +319,8 @@ public class ImageMaker
               SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
     }
 
-    setProgressMessage(MessageManager.formatMessage(
-            "status.export_complete", type.getName()));
+    setProgressMessage(MessageManager
+            .formatMessage("status.export_complete", type.getName()));
     graphics = g2;
   }