JAL-1975 updated image export pipeline to produce more informative messages.
[jalview.git] / src / jalview / util / ImageMaker.java
index 1953a60..b7aa4ca 100755 (executable)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  */
 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;
@@ -34,18 +37,11 @@ import java.io.FileOutputStream;
 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 int PNG = 1;
-  //
-  // public static final int SVG = 2;
-  //
-  // int type = -1;
-
   EpsGraphics2D pg;
 
   SVGGraphics2D g2;
@@ -94,31 +90,43 @@ 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.type = type;
 
     if (file == null)
     {
+      if (pIndicator != null && !headless)
+      {
+        pIndicator.setProgressBar(
+                MessageManager.formatMessage(
+                        "status.waiting_for_user_to_select_output_file",
+                        type.name), pSessionId);
+      }
       JalviewFileChooser chooser;
-      // chooser = type == EPS ? getEPSChooser() : getPNGChooser();
       chooser = type.getChooser();
-
       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
+      {
+        if (pIndicator != null && !headless)
+        {
+          pIndicator.setProgressBar(MessageManager.formatMessage(
+                  "status.cancelled_image_export_operation", type.name),
+                  pSessionId);
+        }
+      }
     }
 
     if (file != null)
@@ -138,11 +146,19 @@ public class ImageMaker
         {
           setupPNG(width, height);
         }
-      
+        if (pIndicator != null && !headless)
+        {
+          pIndicator.setProgressBar(
+MessageManager.formatMessage(
+                  "status.export_complete", type.getName()),
+                  pSessionId);
+        }
       } catch (Exception ex)
       {
-        System.out.println("Error creating "
- + type.getName() + " file.");
+        System.out.println("Error creating " + type.getName() + " file.");
+
+        pIndicator.setProgressBar(MessageManager.formatMessage(
+                "info.error_creating_file", type.getName()), pSessionId);
       }
     }
   }
@@ -152,8 +168,6 @@ public class ImageMaker
     return graphics;
   }
 
-
-
   public void writeImage()
   {
     try
@@ -165,12 +179,14 @@ public class ImageMaker
         pg.close();
         break;
       case SVG:
-        String svg = ((SVGGraphics2D) getGraphics()).getSVGDocument();
-        out.write(svg.getBytes());
+        String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
+        out.write(svgData.getBytes());
+        out.flush();
         out.close();
         break;
       case PNG:
         ImageIO.write(bi, "png", out);
+        out.flush();
         out.close();
         break;
       }
@@ -229,35 +245,78 @@ public class ImageMaker
     Graphics2D ig2 = (Graphics2D) graphics;
     ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
             RenderingHints.VALUE_ANTIALIAS_ON);
+
   }
 
   void setupSVG(int width, int height, String title)
   {
+
     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)
+      {
+        return;
+      }
+    }
+
+    if (renderStyle.equalsIgnoreCase("lineart"))
+    {
+      ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
+              SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
+    }
+
     graphics = g2;
   }
 
   static JalviewFileChooser getPNGChooser()
   {
+    if (Jalview.isHeadlessMode())
+    {
+      return null;
+    }
     return new jalview.io.JalviewFileChooser(
-            jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
-            { "png" }, new String[]
-            { "Portable network graphics" }, "Portable network graphics");
+            jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
+            new String[] { "png" },
+            new String[] { "Portable network graphics" },
+            "Portable network graphics");
   }
 
   static JalviewFileChooser getEPSChooser()
   {
+    if (Jalview.isHeadlessMode())
+    {
+      return null;
+    }
     return new jalview.io.JalviewFileChooser(
-            jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
-            { "eps" }, new String[]
-            { "Encapsulated Postscript" }, "Encapsulated Postscript");
+            jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
+            new String[] { "eps" },
+            new String[] { "Encapsulated Postscript" },
+            "Encapsulated Postscript");
   }
 
   static JalviewFileChooser getSVGChooser()
   {
+    if (Jalview.isHeadlessMode())
+    {
+      return null;
+    }
     return new jalview.io.JalviewFileChooser(
-            jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
-            { "svg" }, new String[]
-            { "Scalable Vector Graphics" }, "Scalable Vector Graphics");
+            jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
+            new String[] { "svg" },
+            new String[] { "Scalable Vector Graphics" },
+            "Scalable Vector Graphics");
   }
 }