JAL-3048 all image export (alignment, tree, PCA, Jmol) now via ImageExporter and...
[jalview.git] / src / jalview / gui / AlignmentPanel.java
index 6e46781..58aa645 100644 (file)
@@ -24,12 +24,14 @@ import jalview.analysis.AnnotationSorter;
 import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
 import jalview.bin.Cache;
+import jalview.bin.Jalview;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.SearchResultsI;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
+import jalview.gui.ImageExporter.ImageWriterI;
 import jalview.io.HTMLOutput;
 import jalview.jbgui.GAlignmentPanel;
 import jalview.math.AlignmentDimension;
@@ -82,11 +84,8 @@ public class AlignmentPanel extends GAlignmentPanel implements
 
   private IdPanel idPanel;
 
-  private boolean headless;
-
   IdwidthAdjuster idwidthAdjuster;
 
-  /** DOCUMENT ME!! */
   public AlignFrame alignFrame;
 
   private ScalePanel scalePanel;
@@ -1288,32 +1287,14 @@ public class AlignmentPanel extends GAlignmentPanel implements
    */
   void makeAlignmentImage(ImageMaker.TYPE type, File file)
   {
-    int borderBottomOffset = 5;
-    long pSessionId = System.currentTimeMillis();
-    headless = (System.getProperty("java.awt.headless") != null
-            && System.getProperty("java.awt.headless").equals("true"));
-    if (alignFrame != null && !headless)
-    {
-      if (file != null)
-      {
-        alignFrame.setProgressBar(MessageManager
-                .formatMessage("status.saving_file", new Object[]
-                { type.getLabel() }), pSessionId);
-      }
-    }
-    try
+    final int borderBottomOffset = 5;
+
+    AlignmentDimension aDimension = getAlignmentDimension();
+    // todo use a lambda function in place of callback here?
+    ImageWriterI writer = new ImageWriterI()
     {
-      AlignmentDimension aDimension = getAlignmentDimension();
-      String imageAction = "Create " + type.getName()
-              + " image from alignment";
-      String imageTitle = alignFrame.getTitle();
-
-      ImageMaker im = new ImageMaker(this, type, imageAction,
-              aDimension.getWidth(),
-              aDimension.getHeight() + borderBottomOffset, file, imageTitle,
-              alignFrame, pSessionId, headless);
-      Graphics graphics = im.getGraphics();
-      if (graphics != null)
+      @Override
+      public void exportImage(Graphics graphics) throws Exception
       {
         if (av.getWrapAlignment())
         {
@@ -1325,21 +1306,24 @@ public class AlignmentPanel extends GAlignmentPanel implements
           printUnwrapped(aDimension.getWidth(), aDimension.getHeight(), 0,
                   graphics, graphics);
         }
-        im.writeImage();
       }
-    } catch (OutOfMemoryError err)
-    {
-      // Be noisy here.
-      System.out.println("########################\n" + "OUT OF MEMORY "
-              + file + "\n" + "########################");
-      new OOMWarning("Creating Image for " + file, err);
-      // System.out.println("Create IMAGE: " + err);
-    } catch (Exception ex)
-    {
-      ex.printStackTrace();
-    }
+    };
+
+    String fileTitle = alignFrame.getTitle();
+    ImageExporter exporter = new ImageExporter(writer, alignFrame, type,
+            fileTitle);
+    int imageWidth = aDimension.getWidth();
+    int imageHeight = aDimension.getHeight() + borderBottomOffset;
+    String of = MessageManager.getString("label.alignment");
+    exporter.doExport(file, this, imageWidth, imageHeight, of);
   }
 
+  /**
+   * Calculates and returns a suitable width and height (in pixels) for an
+   * exported image
+   * 
+   * @return
+   */
   public AlignmentDimension getAlignmentDimension()
   {
     int maxwidth = av.getAlignment().getWidth();
@@ -1356,7 +1340,7 @@ public class AlignmentPanel extends GAlignmentPanel implements
     if (av.getWrapAlignment())
     {
       height = getWrappedHeight();
-      if (headless)
+      if (Jalview.isHeadlessMode())
       {
         // need to obtain default alignment width and then add in any
         // additional allowance for id margin