JAL-2446 merged to spike branch
[jalview.git] / src / jalview / util / ImageMaker.java
index 100ecf5..fa2533f 100755 (executable)
@@ -20,7 +20,9 @@
  */
 package jalview.util;
 
+import jalview.bin.Jalview;
 import jalview.gui.EPSOptions;
+import jalview.gui.IProgressIndicator;
 import jalview.gui.SVGOptions;
 import jalview.io.JalviewFileChooser;
 
@@ -40,6 +42,22 @@ 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";
+
+  public static final String HTML_EXTENSION = "html";
+
+  public static final String HTML_DESCRIPTION = "Hypertext Markup Language";
+
   EpsGraphics2D pg;
 
   SVGGraphics2D g2;
@@ -52,11 +70,17 @@ public class ImageMaker
 
   TYPE type;
 
+  private IProgressIndicator pIndicator;
+
+  private long pSessionId;
+
+  private boolean headless;
+
   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"), getEPSChooser()),
+    PNG("PNG", MessageManager.getString("label.png_image"), getPNGChooser()),
+    SVG("SVG", "SVG", getSVGChooser());
 
     private JalviewFileChooser chooser;
 
@@ -88,14 +112,18 @@ public class ImageMaker
 
   }
 
-
   public ImageMaker(Component parent, TYPE type, String title, int width,
-          int height, File file, String fileTitle)
+          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.getChooser();
       chooser.setFileView(new jalview.io.JalviewFileView());
@@ -109,6 +137,11 @@ public class ImageMaker
                 .getSelectedFile().getParent());
         file = chooser.getSelectedFile();
       }
+      else
+      {
+        setProgressMessage(MessageManager.formatMessage(
+                "status.cancelled_image_export_operation", type.name));
+      }
     }
 
     if (file != null)
@@ -116,6 +149,9 @@ public class ImageMaker
       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);
@@ -128,11 +164,13 @@ public class ImageMaker
         {
           setupPNG(width, height);
         }
-      
+
       } catch (Exception ex)
       {
-        System.out.println("Error creating "
- + type.getName() + " file.");
+        System.out.println("Error creating " + type.getName() + " file.");
+
+        setProgressMessage(MessageManager.formatMessage(
+                "info.error_creating_file", type.getName()));
       }
     }
   }
@@ -142,8 +180,6 @@ public class ImageMaker
     return graphics;
   }
 
-
-
   public void writeImage()
   {
     try
@@ -161,7 +197,7 @@ public class ImageMaker
         out.close();
         break;
       case PNG:
-        ImageIO.write(bi, "png", out);
+        ImageIO.write(bi, PNG_EXTENSION, out);
         out.flush();
         out.close();
         break;
@@ -190,6 +226,8 @@ public class ImageMaker
 
       if (renderStyle == null || eps.cancelled)
       {
+        setProgressMessage(MessageManager.formatMessage(
+                "status.cancelled_image_export_operation", "EPS"));
         return;
       }
     }
@@ -209,6 +247,8 @@ public class ImageMaker
       pg.setAccurateTextMode(accurateText);
 
       graphics = pg;
+      setProgressMessage(MessageManager.formatMessage(
+              "status.export_complete", type.getName()));
     } catch (Exception ex)
     {
     }
@@ -221,6 +261,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()));
 
   }
 
@@ -244,40 +286,55 @@ public class ImageMaker
 
       if (renderStyle == null || svgOption.cancelled)
       {
+        setProgressMessage(MessageManager.formatMessage(
+                "status.cancelled_image_export_operation", "SVG"));
         return;
       }
     }
 
-    if (renderStyle.equalsIgnoreCase("lineart"))
+    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()
   {
-    return new jalview.io.JalviewFileChooser(
-            jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
-            { "png" }, new String[]
-            { "Portable network graphics" }, "Portable network graphics");
+    if (Jalview.isHeadlessMode())
+    {
+      return null;
+    }
+    return new JalviewFileChooser(PNG_EXTENSION, PNG_DESCRIPTION);
   }
 
   static JalviewFileChooser getEPSChooser()
   {
-    return new jalview.io.JalviewFileChooser(
-            jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
-            { "eps" }, new String[]
-            { "Encapsulated Postscript" }, "Encapsulated Postscript");
+    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()
   {
-    return new jalview.io.JalviewFileChooser(
-            jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
-            { "svg" }, new String[]
-            { "Scalable Vector Graphics" }, "Scalable Vector Graphics");
+    if (Jalview.isHeadlessMode())
+    {
+      return null;
+    }
+    return new JalviewFileChooser(SVG_EXTENSION, SVG_DESCRIPTION);
   }
 }