JAL-2577 Don't allow viewport ranges below 0. Unit tests updated.
[jalview.git] / src / jalview / viewmodel / ViewportRanges.java
index 56c1d39..4eb8c95 100644 (file)
@@ -123,7 +123,7 @@ public class ViewportRanges extends ViewportProperties
     int oldstartres = this.startRes;
     if (start > getVisibleAlignmentWidth() - 1)
     {
-      startRes = getVisibleAlignmentWidth() - 1;
+      startRes = Math.max(getVisibleAlignmentWidth() - 1, 0);
     }
     else if (start < 0)
     {
@@ -139,6 +139,10 @@ public class ViewportRanges extends ViewportProperties
     {
       endRes = 0;
     }
+    else if (end > getVisibleAlignmentWidth() - 1)
+    {
+      endRes = Math.max(getVisibleAlignmentWidth() - 1, 0);
+    }
     else
     {
       endRes = end;
@@ -204,7 +208,7 @@ public class ViewportRanges extends ViewportProperties
     int oldstartseq = this.startSeq;
     if (start > getVisibleAlignmentHeight() - 1)
     {
-      startSeq = getVisibleAlignmentHeight() - 1;
+      startSeq = Math.max(getVisibleAlignmentHeight() - 1, 0);
     }
     else if (start < 0)
     {
@@ -218,7 +222,7 @@ public class ViewportRanges extends ViewportProperties
     int oldendseq = this.endSeq;
     if (end >= getVisibleAlignmentHeight())
     {
-      endSeq = getVisibleAlignmentHeight() - 1;
+      endSeq = Math.max(getVisibleAlignmentHeight() - 1, 0);
     }
     else if (end < 0)
     {
@@ -326,9 +330,12 @@ public class ViewportRanges extends ViewportProperties
     {
       vpstart = 0;
     }
-    else if (vpstart + w - 1 > getVisibleAlignmentWidth() - 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 = getVisibleAlignmentWidth() - 1;
+      vpstart = getVisibleAlignmentWidth() - w;
     }
     setStartEndRes(vpstart, vpstart + w - 1);
   }
@@ -350,7 +357,10 @@ public class ViewportRanges extends ViewportProperties
     {
       vpstart = 0;
     }
-    else if (vpstart + h - 1 > getVisibleAlignmentHeight() - 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 = getVisibleAlignmentHeight() - h;
     }
@@ -429,7 +439,7 @@ public class ViewportRanges extends ViewportProperties
     }
     else
     {
-      if (endRes > getVisibleAlignmentWidth() - 1)
+      if (endRes >= getVisibleAlignmentWidth() - 1)
       {
         return false;
       }