JAL-244 Fixed idWidth labels in headless mode for non-wrap alignments
[jalview.git] / src / jalview / gui / AnnotationLabels.java
index ffc565c..26276ae 100755 (executable)
@@ -20,6 +20,7 @@
  */
 package jalview.gui;
 
+import java.awt.Canvas;
 import java.awt.Color;
 import java.awt.Cursor;
 import java.awt.Dimension;
@@ -135,6 +136,8 @@ public class AnnotationLabels extends JPanel
 
   private boolean resizePanel = false;
 
+  private int annotationIdWidth = -1;
+
   /**
    * Creates a new AnnotationLabels object
    * 
@@ -1198,36 +1201,41 @@ public class AnnotationLabels extends JPanel
     {
       Graphics2D g2d = (Graphics2D) g;
       Graphics dummy = g2d.create();
-      int annotationIdWidth = drawLabels(dummy, clip, width, false);
+      int newAnnotationIdWidth = drawLabels(dummy, clip, width, false,
+              null);
       Dimension d = ap.calculateDefaultAlignmentIdWidth();
       int alignmentIdWidth = d.width;
       if (!iwa.manuallyAdjusted())
       {
         // If no manual adjustment to ID column with has been made then adjust
         // width match widest of alignment or annotation id widths
-        width = Math.max(alignmentIdWidth, annotationIdWidth);
+        width = Math.max(alignmentIdWidth, newAnnotationIdWidth);
       }
-      else if (annotationIdWidth > givenWidth
-              && annotationIdWidth > alignmentIdWidth)
+      else if (newAnnotationIdWidth != annotationIdWidth
+              && newAnnotationIdWidth > givenWidth
+              && newAnnotationIdWidth > alignmentIdWidth)
       {
         // otherwise if the annotation id width has become larger than the
         // current id width, increase
-        width = annotationIdWidth;
+        width = newAnnotationIdWidth;
+        annotationIdWidth = newAnnotationIdWidth;
       }
       // set the width if it's changed
       if (width != ap.av.getIdWidth())
       {
         iwa.setWidth(width);
-        ap.validateAnnotationDimensions(false);
       }
     }
-    drawLabels(g, clip, width, true);
+    drawLabels(g, clip, width, true, null);
   }
 
   /**
    * Render the full set of annotation Labels for the alignment at the given
-   * cursor. If actuallyDraw is false then no actual drawing will occur, but the
-   * widest label width will be returned.
+   * cursor. If actuallyDraw is false or g is null then no actual drawing will
+   * occur, but the widest label width will be returned. If g is null then
+   * fmetrics must be supplied.
+   * 
+   * Returns the width of the annotation labels.
    * 
    * @param g
    *          Graphics2D instance (needed for font scaling)
@@ -1236,21 +1244,27 @@ public class AnnotationLabels extends JPanel
    *          rendered
    * @param width
    *          Width for scaling labels
+   * @param fmetrics
+   *          FontMetrics if Graphics object g is null
    */
   public int drawLabels(Graphics g, boolean clip, int width,
-          boolean actuallyDraw)
+          boolean actuallyDraw, FontMetrics fmetrics)
   {
     int actualWidth = 0;
-    if (av.getFont().getSize() < 10)
+    if (actuallyDraw && g != null)
     {
-      g.setFont(font);
-    }
-    else
-    {
-      g.setFont(av.getFont());
+      if (av.getFont().getSize() < 10)
+      {
+        g.setFont(font);
+      }
+      else
+      {
+        g.setFont(av.getFont());
+      }
     }
 
-    FontMetrics fm = g.getFontMetrics(g.getFont());
+    FontMetrics fm = fmetrics == null ? g.getFontMetrics(g.getFont())
+            : fmetrics;
     if (actuallyDraw)
     {
       g.setColor(Color.white);
@@ -1265,12 +1279,13 @@ public class AnnotationLabels extends JPanel
     SequenceI lastSeqRef = null;
     String lastLabel = null;
     AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
-    int fontHeight = g.getFont().getSize();
+    int fontHeight = g != null ? g.getFont().getSize()
+            : fm.getFont().getSize();
     int y = 0;
     int x = 0;
     int graphExtras = 0;
     int offset = 0;
-    Font baseFont = g.getFont();
+    Font baseFont = g != null ? g.getFont() : fm.getFont();
     FontMetrics baseMetrics = fm;
     int ofontH = fontHeight;
     int sOffset = 0;
@@ -1334,7 +1349,7 @@ public class AnnotationLabels extends JPanel
             continue;
           }
         }
-        if (actuallyDraw)
+        if (actuallyDraw && g != null)
         {
           g.setColor(Color.black);
         }
@@ -1417,10 +1432,15 @@ public class AnnotationLabels extends JPanel
               s = ((float) fontHeight) / (float) ofontH;
               Font f = baseFont
                       .deriveFont(AffineTransform.getScaleInstance(s, s));
-              g.setFont(f);
-              fm = g.getFontMetrics();
-              graphExtras = (aa[i].height - (groupSize * (fontHeight + 8)))
-                      / 2;
+              Canvas c = new Canvas();
+              fm = c.getFontMetrics(f);
+              if (actuallyDraw && g != null)
+              {
+                g.setFont(f);
+                // fm = g.getFontMetrics();
+                graphExtras = (aa[i].height
+                        - (groupSize * (fontHeight + 8))) / 2;
+              }
             }
           }
           if (visible)
@@ -1431,7 +1451,7 @@ public class AnnotationLabels extends JPanel
               {
                 labelWidth = fm.stringWidth(aa[gg].label) + 3;
                 x = width - labelWidth;
-                if (actuallyDraw)
+                if (actuallyDraw && g != null)
                 {
                   g.drawString(aa[gg].label, x, y - graphExtras);
 
@@ -1450,13 +1470,16 @@ public class AnnotationLabels extends JPanel
               }
             }
           }
-          g.setFont(baseFont);
+          if (actuallyDraw && g != null)
+          {
+            g.setFont(baseFont);
+          }
           fm = baseMetrics;
           fontHeight = ofontH;
         }
         else
         {
-          if (actuallyDraw)
+          if (actuallyDraw && g != null)
           {
             if (vertBar)
             {
@@ -1479,7 +1502,7 @@ public class AnnotationLabels extends JPanel
 
     if (!resizePanel && dragEvent != null && aa != null)
     {
-      if (actuallyDraw)
+      if (actuallyDraw && g != null)
       {
         g.setColor(Color.lightGray);
         g.drawString(
@@ -1492,7 +1515,7 @@ public class AnnotationLabels extends JPanel
 
     if (!av.getWrapAlignment() && ((aa == null) || (aa.length < 1)))
     {
-      if (actuallyDraw)
+      if (actuallyDraw && g != null)
       {
         g.drawString(MessageManager.getString("label.right_click"), 2, 8);
         g.drawString(MessageManager.getString("label.to_add_annotation"), 2,