JAL-4290 Fixed one test
[jalview.git] / src / jalview / gui / AlignmentPanel.java
index 3127731..358560b 100644 (file)
@@ -42,6 +42,7 @@ import java.io.FileWriter;
 import java.io.PrintWriter;
 import java.util.List;
 
+import javax.swing.BoundedRangeModel;
 import javax.swing.SwingUtilities;
 
 import jalview.analysis.AnnotationSorter;
@@ -256,8 +257,10 @@ public class AlignmentPanel extends GAlignmentPanel implements
 
   /**
    * Calculates the width of the alignment labels based on the displayed names
-   * and any bounds on label width set in preferences. The calculated width is
-   * also set as a property of the viewport.
+   * and any bounds on label width set in preferences.
+   * 
+   * The calculated width is set as a property of the viewport and the layout is
+   * updated.
    * 
    * @return Dimension giving the maximum width of the alignment label panel
    *         that should be used.
@@ -294,10 +297,21 @@ public class AlignmentPanel extends GAlignmentPanel implements
 
   public Dimension calculateDefaultAlignmentIdWidth()
   {
+    return calculateIdWidth(-1, false, false);
+  }
+
+  /**
+   * pre 2.11.3 Id width calculation - used when importing old projects only
+   * 
+   * @return
+   */
+  public int getLegacyIdWidth()
+  {
     int afwidth = (alignFrame != null ? alignFrame.getWidth() : 300);
     int idWidth = Math.min(afwidth - 200, 2 * afwidth / 3);
     int maxwidth = Math.max(IdwidthAdjuster.MIN_ID_WIDTH, idWidth);
-    return calculateIdWidth(-1, false, false);
+    Dimension w = calculateIdWidthOrLegacy(true, maxwidth, false, false);
+    return w.width;
   }
 
   /**
@@ -317,20 +331,42 @@ public class AlignmentPanel extends GAlignmentPanel implements
   {
     return calculateIdWidth(maxwidth, true, false);
   }
+
   /**
    * Calculate the width of the alignment labels based on the displayed names
    * and any bounds on label width set in preferences.
    * 
    * @param maxwidth
    *          -1 or maximum width allowed for IdWidth
-   * @param includeAnnotations - when true includes width of any additional marks in annotation id panel 
-   * @param visibleOnly -  
+   * @param includeAnnotations
+   *          - when true includes width of any additional marks in annotation
+   *          id panel
+   * @param visibleOnly
+   *          - when true, ignore label widths for hidden annotation rows
    * @return Dimension giving the maximum width of the alignment label panel
    *         that should be used.
    */
   public Dimension calculateIdWidth(int maxwidth,
           boolean includeAnnotations, boolean visibleOnly)
   {
+    return calculateIdWidthOrLegacy(false, maxwidth, includeAnnotations,
+            visibleOnly);
+  }
+
+  /**
+   * legacy mode or post 2.11.3 ID width calculation
+   * 
+   * @param legacy
+   *          - uses annotation labels, not rendered label width (excludes
+   *          additional decorators)
+   * @param maxwidth
+   * @param includeAnnotations
+   * @param visibleOnly
+   * @return
+   */
+  private Dimension calculateIdWidthOrLegacy(boolean legacy, int maxwidth,
+          boolean includeAnnotations, boolean visibleOnly)
+  {
     Container c = new Container();
 
     FontMetrics fm = c.getFontMetrics(
@@ -352,10 +388,29 @@ public class AlignmentPanel extends GAlignmentPanel implements
     // Also check annotation label widths
     if (includeAnnotations && al.getAlignmentAnnotation() != null)
     {
-      AnnotationLabels aal = getAlabels();
-      int stringWidth = aal.drawLabels(null, false, idWidth, false, false,
-              fm, !visibleOnly);
-      idWidth = Math.max(idWidth, stringWidth);
+      fm = c.getFontMetrics(getAlabels().getFont());
+
+      if (!legacy || Jalview.isHeadlessMode())
+      {
+        AnnotationLabels aal = getAlabels();
+        int stringWidth = aal.drawLabels(null, false, idWidth, false, false,
+                fm, !visibleOnly);
+        idWidth = Math.max(idWidth, stringWidth);
+      }
+      else
+      {
+        for (i = 0; i < al.getAlignmentAnnotation().length; i++)
+        {
+          AlignmentAnnotation aa = al.getAlignmentAnnotation()[i];
+          if (visibleOnly && !aa.visible)
+          {
+            continue;
+          }
+          String label = aa.label;
+          int stringWidth = fm.stringWidth(label);
+          idWidth = Math.max(idWidth, stringWidth);
+        }
+      }
     }
 
     int w = maxwidth < 0 ? idWidth : Math.min(maxwidth, idWidth);
@@ -562,7 +617,8 @@ public class AlignmentPanel extends GAlignmentPanel implements
     // this is called after loading new annotation onto alignment
     if (alignFrame.getHeight() == 0)
     {
-      jalview.bin.Console.outPrintln("NEEDS FIXING");
+      jalview.bin.Console.error(
+              "adjustAnnotationHeight called with zero height alignment window");
     }
     validateAnnotationDimensions(true);
     addNotify();
@@ -595,7 +651,7 @@ public class AlignmentPanel extends GAlignmentPanel implements
     Dimension e = idPanel.getSize();
     int idWidth = e.width;
     boolean manuallyAdjusted = this.getIdPanel().getIdCanvas()
-            .manuallyAdjusted();
+            .isManuallyAdjusted();
     annotationScroller.setPreferredSize(new Dimension(
             manuallyAdjusted ? idWidth : annotationScroller.getWidth(),
             annotationHeight));
@@ -734,6 +790,22 @@ public class AlignmentPanel extends GAlignmentPanel implements
   }
 
   /**
+   * Answers true if the panel has no horizontal scrollbar, or the scrollbar is
+   * at its rightmost position, else false.
+   * 
+   * @return
+   */
+  boolean isScrolledFullyRight()
+  {
+    if (hscroll == null)
+    {
+      return true;
+    }
+    BoundedRangeModel model = hscroll.getModel();
+    return (model.getExtent() + model.getValue() >= model.getMaximum());
+  }
+
+  /**
    * Respond to adjustment event when horizontal or vertical scrollbar is
    * changed
    * 
@@ -996,13 +1068,13 @@ public class AlignmentPanel extends GAlignmentPanel implements
     final int idWidth, idWidthForGui;
     // otherwise calculate it
     idWidth = getVisibleIdWidth(false);
-//    if (getIdPanel()!=null && getIdPanel().getWidth()>0)
-//    {
-//      // use the current IdPanel's width, if its set and non-zero
-//      idWidthForGui = getIdPanel().getWidth();
-//    } else {
-//      idWidthForGui=0;
-//    }
+    // if (getIdPanel()!=null && getIdPanel().getWidth()>0)
+    // {
+    // // use the current IdPanel's width, if its set and non-zero
+    // idWidthForGui = getIdPanel().getWidth();
+    // } else {
+    // idWidthForGui=0;
+    // }
     /*
      * Get the horizontal offset to where we draw the sequences.
      * This is idWidth if using a single Graphics context, else zero.
@@ -1052,7 +1124,7 @@ public class AlignmentPanel extends GAlignmentPanel implements
     final int alignmentDrawnHeight = (endSeq - startSeq) * charHeight + 3;
 
     alignmentGraphics.setColor(Color.white);
-    alignmentGraphics.fillRect(0, 0, pageWidth, pageHeight+scaleHeight);
+    alignmentGraphics.fillRect(0, 0, pageWidth, pageHeight + scaleHeight);
 
     /*
      * draw the Scale at horizontal offset, then reset to top left (0, 0)
@@ -1070,9 +1142,9 @@ public class AlignmentPanel extends GAlignmentPanel implements
     IdCanvas idCanvas = getIdPanel().getIdCanvas();
     List<SequenceI> selection = av.getSelectionGroup() == null ? null
             : av.getSelectionGroup().getSequences(null);
-    
+
     idCanvas.drawIds((Graphics2D) idGraphics, av, startSeq, endSeq - 1,
-            selection, false,idWidth);
+            selection, false, idWidth);
 
     idGraphics.setFont(av.getFont());
     idGraphics.translate(0, -scaleHeight);
@@ -1219,7 +1291,7 @@ public class AlignmentPanel extends GAlignmentPanel implements
     // see if rendering offscreen - check preferences and calc width accordingly
     if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
     {
-      return calculateIdWidth(-1,true,true).width;
+      return calculateIdWidth(-1, true, true).width;
     }
     Integer idwidth = onscreen ? null
             : Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH");
@@ -1229,7 +1301,7 @@ public class AlignmentPanel extends GAlignmentPanel implements
     }
 
     int w = getIdPanel().getWidth();
-    w = this.calculateIdWidth(-1, true, true).width;
+    w = calculateIdWidth(-1, true, true).width;
     return (w > 0 ? w : calculateIdWidth().width);
   }
 
@@ -1307,7 +1379,7 @@ public class AlignmentPanel extends GAlignmentPanel implements
         // need to obtain default alignment width and then add in any
         // additional allowance for id margin
         // this duplicates the calculation in getWrappedHeight but adjusts for
-        // offscreen idWith
+        // offscreen idWidth
         width = alignFrame.getWidth() - vscroll.getPreferredSize().width
                 - alignFrame.getInsets().left - alignFrame.getInsets().right
                 - getVisibleIdWidth() + getVisibleIdWidth(false);
@@ -1857,5 +1929,4 @@ public class AlignmentPanel extends GAlignmentPanel implements
       overviewPanel = null;
     }
   }
-
 }