JAL-3085 spike - will need to cherrypick from this branch
[jalview.git] / src / jalview / util / ImageMaker.java
index 10ea153..ed16e11 100755 (executable)
@@ -31,6 +31,8 @@ import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;
 import java.awt.image.BufferedImage;
+import java.awt.print.PageFormat;
+import java.awt.print.Paper;
 import java.io.File;
 import java.io.FileOutputStream;
 
@@ -40,12 +42,35 @@ import org.jfree.graphics2d.svg.SVGGraphics2D;
 import org.jfree.graphics2d.svg.SVGHints;
 import org.jibble.epsgraphics.EpsGraphics2D;
 
+import gnu.jpdf.PDFJob;
+
 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 PDF_EXTENSION = "pdf";
+
+  public static final String PDF_DESCRIPTION = "Portable  Document Format";
+
+  public static final String HTML_EXTENSION = "html";
+
+  public static final String HTML_DESCRIPTION = "Hypertext Markup Language";
+
   EpsGraphics2D pg;
 
   SVGGraphics2D g2;
 
+
   Graphics graphics;
 
   FileOutputStream out;
@@ -54,11 +79,21 @@ public class ImageMaker
 
   TYPE type;
 
+  private IProgressIndicator pIndicator;
+
+  private long pSessionId;
+
+  private boolean headless;
+
+  private PDFJob pdfjob;
+
   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()),
+    PDF("PDF","PDF",getPDFChooser());
 
     private JalviewFileChooser chooser;
 
@@ -94,17 +129,14 @@ public class ImageMaker
           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)
     {
-      if (pIndicator != null && !headless)
-      {
-        pIndicator.setProgressBar(
-                MessageManager.formatMessage(
-                        "status.waiting_for_user_to_select_output_file",
-                        type.name), pSessionId);
-      }
+      setProgressMessage(MessageManager.formatMessage(
+              "status.waiting_for_user_to_select_output_file", type.name));
       JalviewFileChooser chooser;
       chooser = type.getChooser();
       chooser.setFileView(new jalview.io.JalviewFileView());
@@ -114,18 +146,14 @@ public class ImageMaker
 
       if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
       {
-        jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
-                .getSelectedFile().getParent());
+        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);
-        }
+        setProgressMessage(MessageManager.formatMessage(
+                "status.cancelled_image_export_operation", type.name));
       }
     }
 
@@ -134,10 +162,9 @@ public class ImageMaker
       try
       {
         out = new FileOutputStream(file);
-        pIndicator.setProgressBar(null, pSessionId);
-        pIndicator.setProgressBar(MessageManager.formatMessage(
-                "status.exporting_alignment_as_x_file", type.getName()),
-                pSessionId);
+        setProgressMessage(null);
+        setProgressMessage(MessageManager.formatMessage(
+                "status.exporting_alignment_as_x_file", type.getName()));
         if (type == TYPE.SVG)
         {
           setupSVG(width, height, fileTitle);
@@ -150,19 +177,17 @@ public class ImageMaker
         {
           setupPNG(width, height);
         }
-        if (pIndicator != null && !headless)
+        else if (type == TYPE.PDF)
         {
-          pIndicator.setProgressBar(
-MessageManager.formatMessage(
-                  "status.export_complete", type.getName()),
-                  pSessionId);
+          setupPDF(width, height, fileTitle);
         }
+
       } catch (Exception ex)
       {
         System.out.println("Error creating " + type.getName() + " file.");
 
-        pIndicator.setProgressBar(MessageManager.formatMessage(
-                "info.error_creating_file", type.getName()), pSessionId);
+        setProgressMessage(MessageManager
+                .formatMessage("info.error_creating_file", type.getName()));
       }
     }
   }
@@ -189,10 +214,14 @@ MessageManager.formatMessage(
         out.close();
         break;
       case PNG:
-        ImageIO.write(bi, "png", out);
+        ImageIO.write(bi, PNG_EXTENSION, out);
         out.flush();
         out.close();
         break;
+      case PDF:
+        graphics = null;
+        pdfjob.end();
+        pdfjob = null;
       }
     } catch (Exception ex)
     {
@@ -218,6 +247,8 @@ MessageManager.formatMessage(
 
       if (renderStyle == null || eps.cancelled)
       {
+        setProgressMessage(MessageManager.formatMessage(
+                "status.cancelled_image_export_operation", "EPS"));
         return;
       }
     }
@@ -237,11 +268,57 @@ MessageManager.formatMessage(
       pg.setAccurateTextMode(accurateText);
 
       graphics = pg;
+      setProgressMessage(MessageManager
+              .formatMessage("status.export_complete", type.getName()));
     } catch (Exception ex)
     {
     }
   }
 
+  void setupPDF(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
+    {
+      pdfjob = new PDFJob(out, title);
+      
+      Paper paper = new Paper();
+      paper.setSize(width, height);
+      PageFormat pf = new PageFormat();
+      pf.setPaper(paper);
+      graphics = pdfjob.getGraphics(pf);
+      setProgressMessage(MessageManager
+              .formatMessage("status.export_complete", type.getName()));
+    } catch (Exception ex)
+    {
+    }
+  }
   void setupPNG(int width, int height)
   {
     bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
@@ -249,6 +326,8 @@ MessageManager.formatMessage(
     Graphics2D ig2 = (Graphics2D) graphics;
     ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
             RenderingHints.VALUE_ANTIALIAS_ON);
+    setProgressMessage(MessageManager
+            .formatMessage("status.export_complete", type.getName()));
 
   }
 
@@ -272,6 +351,8 @@ MessageManager.formatMessage(
 
       if (renderStyle == null || svgOption.cancelled)
       {
+        setProgressMessage(MessageManager.formatMessage(
+                "status.cancelled_image_export_operation", "SVG"));
         return;
       }
     }
@@ -282,6 +363,8 @@ MessageManager.formatMessage(
               SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
     }
 
+    setProgressMessage(MessageManager
+            .formatMessage("status.export_complete", type.getName()));
     graphics = g2;
   }
 
@@ -291,11 +374,16 @@ MessageManager.formatMessage(
     {
       return null;
     }
-    return new jalview.io.JalviewFileChooser(
-            jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
-            new String[] { "png" },
-            new String[] { "Portable network graphics" },
-            "Portable network graphics");
+    return new JalviewFileChooser(PNG_EXTENSION, PNG_DESCRIPTION);
+  }
+
+  static JalviewFileChooser getPDFChooser()
+  {
+    if (Jalview.isHeadlessMode())
+    {
+      return null;
+    }
+    return new JalviewFileChooser(PDF_EXTENSION, PDF_DESCRIPTION);
   }
 
   static JalviewFileChooser getEPSChooser()
@@ -304,11 +392,15 @@ MessageManager.formatMessage(
     {
       return null;
     }
-    return new jalview.io.JalviewFileChooser(
-            jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
-            new String[] { "eps" },
-            new String[] { "Encapsulated Postscript" },
-            "Encapsulated Postscript");
+    return new JalviewFileChooser(EPS_EXTENSION, EPS_DESCRIPTION);
+  }
+
+  private void setProgressMessage(String message)
+  {
+    if (pIndicator != null && !headless)
+    {
+      pIndicator.setProgressBar(message, pSessionId);
+    }
   }
 
   static JalviewFileChooser getSVGChooser()
@@ -317,10 +409,6 @@ MessageManager.formatMessage(
     {
       return null;
     }
-    return new jalview.io.JalviewFileChooser(
-            jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
-            new String[] { "svg" },
-            new String[] { "Scalable Vector Graphics" },
-            "Scalable Vector Graphics");
+    return new JalviewFileChooser(SVG_EXTENSION, SVG_DESCRIPTION);
   }
 }