JAL-4367 Limit right and top adjustments
[jalview.git] / src / jalview / gui / AnnotationLabels.java
index 6006026..7c5afcd 100755 (executable)
@@ -793,9 +793,15 @@ public class AnnotationLabels extends JPanel
     {
       Dimension d = ap.annotationScroller.getPreferredSize();
       int dif = evt.getY() - oldY;
+      dif -= dif % ap.av.getCharHeight();
 
-      dif /= ap.av.getCharHeight();
-      dif *= ap.av.getCharHeight();
+      // don't allow setting an annotation panel height larger than visible
+      // (otherwise you can't get back)
+      if (d.height - dif > ap.idPanelHolder.getHeight()
+              - ap.getIdSpaceFillerPanel1().getHeight())
+      {
+        return;
+      }
 
       if ((d.height - dif) > 20)
       {
@@ -1112,7 +1118,7 @@ public class AnnotationLabels extends JPanel
     if (av.hasHiddenColumns())
     {
       Iterator<int[]> it = av.getAlignment().getHiddenColumns()
-              .getVisContigsIterator(0, sq.getLength(), false);
+              .getVisContigsIterator(0, sq.getLength() + 1, false);
       omitHidden = new String[] { sq.getSequenceStringFromIterator(it) };
     }
 
@@ -1168,7 +1174,7 @@ public class AnnotationLabels extends JPanel
               RenderingHints.VALUE_ANTIALIAS_ON);
     }
 
-    drawComponent(g2, true, width);
+    drawComponent(g2, true, width, true);
   }
 
   /**
@@ -1183,7 +1189,7 @@ public class AnnotationLabels extends JPanel
    */
   public void drawComponent(Graphics g, int width)
   {
-    drawComponent(g, false, width);
+    drawComponent(g, false, width, true);
   }
 
   /**
@@ -1198,20 +1204,21 @@ public class AnnotationLabels extends JPanel
    * @param width
    *          Width for scaling labels
    */
-  public void drawComponent(Graphics g, boolean clip, int givenWidth)
+  public void drawComponent(Graphics g, boolean clip, int givenWidth,
+          boolean forGUI)
   {
     int width = givenWidth;
     IdwidthAdjuster iwa = null;
     if (ap != null)
     {
       iwa = ap.idwidthAdjuster;
-      if ((Cache.getDefault(ADJUST_ANNOTATION_LABELS_WIDTH_PREF, true)
-              || Jalview.isHeadlessMode()))
+      if (Cache.getDefault(ADJUST_ANNOTATION_LABELS_WIDTH_PREF, true)
+              || Jalview.isHeadlessMode())
       {
         Graphics2D g2d = (Graphics2D) g;
         Graphics dummy = g2d.create();
         int newAnnotationIdWidth = drawLabels(dummy, clip, width, false,
-                null);
+                forGUI, null, false);
         dummy.dispose();
         Dimension d = ap.calculateDefaultAlignmentIdWidth();
         int alignmentIdWidth = d.width;
@@ -1245,10 +1252,12 @@ public class AnnotationLabels extends JPanel
     }
     else
     {
-      int newAnnotationIdWidth = drawLabels(g, clip, width, false, null);
-      width = Math.max(newAnnotationIdWidth, givenWidth);
+      int newAnnotationIdWidth = drawLabels(g, clip, width, false, forGUI,
+              null, false);
+      width = newAnnotationIdWidth < givenWidth ? givenWidth
+              : Math.min(newAnnotationIdWidth, givenWidth);
     }
-    drawLabels(g, clip, width, true, null);
+    drawLabels(g, clip, width, true, forGUI, null, false);
   }
 
   /**
@@ -1257,20 +1266,29 @@ public class AnnotationLabels extends JPanel
    * 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)
+   *          Graphics2D instance (used for rendering and font scaling if no
+   *          fmetrics supplied)
    * @param clip
    *          - true indicates that only current visible area needs to be
    *          rendered
    * @param width
    *          Width for scaling labels
+   * @param actuallyDraw
+   *          - when false, no graphics are rendered to g0
+   * @param forGUI
+   *          - when false, GUI relevant marks like indicators for dragging
+   *          annotation panel height are not rendered
    * @param fmetrics
    *          FontMetrics if Graphics object g is null
+   * @param includeHidden
+   *          - when true returned width includes labels in hidden row width
+   *          calculation
+   * @return the width of the annotation labels.
    */
   public int drawLabels(Graphics g0, boolean clip, int width,
-          boolean actuallyDraw, FontMetrics fmetrics)
+          boolean actuallyDraw, boolean forGUI, FontMetrics fmetrics,
+          boolean includeHidden)
   {
     if (clip)
     {
@@ -1312,7 +1330,7 @@ public class AnnotationLabels extends JPanel
       g.fillRect(0, 0, getWidth(), getHeight());
 
       if (!Cache.getDefault(RESIZE_MARGINS_MARK_PREF, false)
-              && !av.getWrapAlignment())
+              && !av.getWrapAlignment() && forGUI)
       {
         g.setColor(Color.LIGHT_GRAY);
         g.drawLine(0, HEIGHT_ADJUSTER_HEIGHT / 4, HEIGHT_ADJUSTER_WIDTH / 4,
@@ -1359,7 +1377,7 @@ public class AnnotationLabels extends JPanel
       for (int i = 0; i < aa.length; i++)
       {
         visible = true;
-        if (!aa[i].visible)
+        if (!aa[i].visible && !includeHidden)
         {
           hasHiddenRows = true;
           continue;
@@ -1367,7 +1385,7 @@ public class AnnotationLabels extends JPanel
         olY = y;
         // look ahead to next annotation
         for (nexAA = i + 1; nexAA < aa.length
-                && !aa[nexAA].visible; nexAA++)
+                && (!aa[nexAA].visible && includeHidden); nexAA++)
           ;
         y += aa[i].height;
         if (clip)
@@ -1378,7 +1396,7 @@ public class AnnotationLabels extends JPanel
             {
               if (debugRedraw)
               {
-                System.out.println("before vis: " + i);
+                jalview.bin.Console.outPrintln("before vis: " + i);
               }
               before = true;
             }
@@ -1392,7 +1410,7 @@ public class AnnotationLabels extends JPanel
             {
               if (debugRedraw)
               {
-                System.out.println(
+                jalview.bin.Console.outPrintln(
                         "Scroll offset: " + sOffset + " after vis: " + i);
               }
               after = true;
@@ -1552,7 +1570,8 @@ public class AnnotationLabels extends JPanel
       }
     }
 
-    if (!resizePanel && dragEvent != null && aa != null)
+    if (!resizePanel && dragEvent != null && aa != null && selectedRow > -1
+            && selectedRow < aa.length)
     {
       if (actuallyDraw && g != null)
       {
@@ -1587,4 +1606,9 @@ public class AnnotationLabels extends JPanel
   public void mouseEntered(MouseEvent e)
   {
   }
+
+  public void drawComponentNotGUI(Graphics idGraphics, int idWidth)
+  {
+    drawComponent(idGraphics, false, idWidth, false);
+  }
 }