JAL-2577 Don't allow viewport ranges below 0. Unit tests updated.
[jalview.git] / src / jalview / viewmodel / ViewportRanges.java
index 267b50e..4eb8c95 100644 (file)
@@ -20,8 +20,8 @@
  */
 package jalview.viewmodel;
 
-import jalview.api.AlignViewportI;
 import jalview.datamodel.AlignmentI;
+import jalview.datamodel.HiddenColumns;
 
 /**
  * Slightly less embryonic class which: Supplies and updates viewport properties
@@ -80,6 +80,22 @@ public class ViewportRanges extends ViewportProperties
   }
 
   /**
+   * Get alignment width in cols, excluding hidden cols
+   */
+  public int getVisibleAlignmentWidth()
+  {
+    return al.getWidth() - al.getHiddenColumns().getSize();
+  }
+
+  /**
+   * Get alignment height in rows, excluding hidden rows
+   */
+  public int getVisibleAlignmentHeight()
+  {
+    return al.getHeight();
+  }
+
+  /**
    * Set first residue visible in the viewport, and retain the current width.
    * Fires a property change event.
    * 
@@ -105,9 +121,9 @@ public class ViewportRanges extends ViewportProperties
   public void setStartEndRes(int start, int end)
   {
     int oldstartres = this.startRes;
-    if (start > al.getWidth() - 1)
+    if (start > getVisibleAlignmentWidth() - 1)
     {
-      startRes = al.getWidth() - 1;
+      startRes = Math.max(getVisibleAlignmentWidth() - 1, 0);
     }
     else if (start < 0)
     {
@@ -123,6 +139,10 @@ public class ViewportRanges extends ViewportProperties
     {
       endRes = 0;
     }
+    else if (end > getVisibleAlignmentWidth() - 1)
+    {
+      endRes = Math.max(getVisibleAlignmentWidth() - 1, 0);
+    }
     else
     {
       endRes = end;
@@ -145,27 +165,38 @@ public class ViewportRanges extends ViewportProperties
    */
   public void setEndRes(int res)
   {
+    int startres = res;
     int width = getViewportWidth();
-    setStartEndRes(res - width + 1, res);
+    if (startres + width - 1 > getVisibleAlignmentWidth() - 1)
+    {
+      startres = getVisibleAlignmentWidth() - width;
+    }
+    setStartEndRes(startres - width + 1, startres);
   }
 
   /**
-   * Set the first sequence visible in the viewport. Fires a property change
-   * event.
+   * Set the first sequence visible in the viewport, maintaining the height. If
+   * the viewport would extend past the last sequence, sets the viewport so it
+   * sits at the bottom of the alignment. Fires a property change event.
    * 
    * @param seq
    *          sequence position
    */
   public void setStartSeq(int seq)
   {
+    int startseq = seq;
     int height = getViewportHeight();
-    setStartEndSeq(seq, seq + height - 1);
+    if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
+    {
+      startseq = getVisibleAlignmentHeight() - height;
+    }
+    setStartEndSeq(startseq, startseq + height - 1);
   }
 
   /**
-   * Set start and end sequences at the same time. This method only fires one
-   * event for the two changes, and should be used in preference to separate
-   * calls to setStartSeq and setEndSeq.
+   * Set start and end sequences at the same time. The viewport height may
+   * change. This method only fires one event for the two changes, and should be
+   * used in preference to separate calls to setStartSeq and setEndSeq.
    * 
    * @param start
    *          the start sequence
@@ -175,9 +206,9 @@ public class ViewportRanges extends ViewportProperties
   public void setStartEndSeq(int start, int end)
   {
     int oldstartseq = this.startSeq;
-    if (start > al.getHeight() - 1)
+    if (start > getVisibleAlignmentHeight() - 1)
     {
-      startSeq = al.getHeight() - 1;
+      startSeq = Math.max(getVisibleAlignmentHeight() - 1, 0);
     }
     else if (start < 0)
     {
@@ -189,9 +220,9 @@ public class ViewportRanges extends ViewportProperties
     }
 
     int oldendseq = this.endSeq;
-    if (end >= al.getHeight())
+    if (end >= getVisibleAlignmentHeight())
     {
-      endSeq = al.getHeight() - 1;
+      endSeq = Math.max(getVisibleAlignmentHeight() - 1, 0);
     }
     else if (end < 0)
     {
@@ -299,9 +330,12 @@ public class ViewportRanges extends ViewportProperties
     {
       vpstart = 0;
     }
-    else if (vpstart + w - 1 > al.getWidth() - 1)
+    else if ((w <= getVisibleAlignmentWidth())
+            && (vpstart + w - 1 > getVisibleAlignmentWidth() - 1))
+    // viewport width is less than the full alignment and we are running off the
+    // RHS edge
     {
-      vpstart = al.getWidth() - 1;
+      vpstart = getVisibleAlignmentWidth() - w;
     }
     setStartEndRes(vpstart, vpstart + w - 1);
   }
@@ -323,9 +357,12 @@ public class ViewportRanges extends ViewportProperties
     {
       vpstart = 0;
     }
-    else if (vpstart + h - 1 > al.getHeight() - 1)
+    else if ((h <= getVisibleAlignmentHeight())
+            && (vpstart + h - 1 > getVisibleAlignmentHeight() - 1))
+    // viewport height is less than the full alignment and we are running off
+    // the bottom
     {
-      vpstart = al.getHeight() - h;
+      vpstart = getVisibleAlignmentHeight() - h;
     }
     setStartEndSeq(vpstart, vpstart + h - 1);
   }
@@ -371,7 +408,7 @@ public class ViewportRanges extends ViewportProperties
     }
     else
     {
-      if (endSeq >= al.getHeight() - 1)
+      if (endSeq >= getVisibleAlignmentHeight() - 1)
       {
         return false;
       }
@@ -402,7 +439,7 @@ public class ViewportRanges extends ViewportProperties
     }
     else
     {
-      if (endRes > al.getWidth() - 1)
+      if (endRes >= getVisibleAlignmentWidth() - 1)
       {
         return false;
       }
@@ -435,11 +472,8 @@ public class ViewportRanges extends ViewportProperties
    *          x position in alignment
    * @param y
    *          y position in alignment
-   * @param av
-   *          viewport to be visible in. Here until hidden columns JAL-2388
-   *          merged, then use alignment to get hidden cols
    */
-  public void scrollToVisible(int x, int y, AlignViewportI av)
+  public void scrollToVisible(int x, int y)
   {
     while (y < startSeq)
     {
@@ -450,14 +484,15 @@ public class ViewportRanges extends ViewportProperties
       scrollUp(false);
     }
 
-    while (x < av.getColumnSelection().adjustForHiddenColumns(startRes))
+    HiddenColumns hidden = al.getHiddenColumns();
+    while (x < hidden.adjustForHiddenColumns(startRes))
     {
       if (!scrollRight(false))
       {
         break;
       }
     }
-    while (x > av.getColumnSelection().adjustForHiddenColumns(endRes))
+    while (x > hidden.adjustForHiddenColumns(endRes))
     {
       if (!scrollRight(true))
       {