JAL-2491 Unit tests and updates
[jalview.git] / src / jalview / viewmodel / ViewportRanges.java
index ebacab1..d77baf5 100644 (file)
@@ -81,6 +81,7 @@ public class ViewportRanges extends ViewportProperties
 
   /**
    * Set first residue visible in the viewport, and retain the current width.
+   * Fires a property change event.
    * 
    * @param res
    *          residue position
@@ -103,7 +104,7 @@ public class ViewportRanges extends ViewportProperties
    */
   public void setStartEndRes(int start, int end)
   {
-    int oldres = this.startRes;
+    int oldstartres = this.startRes;
     if (start > al.getWidth() - 1)
     {
       startRes = al.getWidth() - 1;
@@ -117,6 +118,7 @@ public class ViewportRanges extends ViewportProperties
       startRes = start;
     }
 
+    int oldendres = this.endRes;
     if (end < 0)
     {
       endRes = 0;
@@ -126,11 +128,17 @@ public class ViewportRanges extends ViewportProperties
       endRes = end;
     }
 
-    changeSupport.firePropertyChange("startres", oldres, start);
+    changeSupport.firePropertyChange("startres", oldstartres, start);
+    if (oldstartres == start)
+    {
+      // event won't be fired if start positions are same
+      // fire an event for the end positions in case they changed
+      changeSupport.firePropertyChange("endres", oldendres, end);
+    }
   }
 
   /**
-   * Set last residue visible in the viewport
+   * Set last residue visible in the viewport. Fires a property change event.
    * 
    * @param res
    *          residue position
@@ -142,7 +150,8 @@ public class ViewportRanges extends ViewportProperties
   }
 
   /**
-   * Set the first sequence visible in the viewport
+   * Set the first sequence visible in the viewport. Fires a property change
+   * event.
    * 
    * @param seq
    *          sequence position
@@ -165,7 +174,7 @@ public class ViewportRanges extends ViewportProperties
    */
   public void setStartEndSeq(int start, int end)
   {
-    int oldseq = this.startSeq;
+    int oldstartseq = this.startSeq;
     if (start > al.getHeight() - 1)
     {
       startSeq = al.getHeight() - 1;
@@ -179,6 +188,7 @@ public class ViewportRanges extends ViewportProperties
       startSeq = start;
     }
 
+    int oldendseq = this.endSeq;
     if (end >= al.getHeight())
     {
       endSeq = al.getHeight() - 1;
@@ -192,11 +202,18 @@ public class ViewportRanges extends ViewportProperties
       endSeq = end;
     }
 
-    changeSupport.firePropertyChange("startseq", oldseq, start);
+    changeSupport.firePropertyChange("startseq", oldstartseq, start);
+    if (oldstartseq == start)
+    {
+      // event won't be fired if start positions are the same
+      // fire in case the end positions changed
+      changeSupport.firePropertyChange("endseq", oldendseq, end);
+    }
   }
 
   /**
-   * Set the last sequence visible in the viewport
+   * Set the last sequence visible in the viewport. Fires a property change
+   * event.
    * 
    * @param seq
    *          sequence position
@@ -242,7 +259,7 @@ public class ViewportRanges extends ViewportProperties
   /**
    * Set viewport width in residues, without changing startRes. Use in
    * preference to calculating endRes from the width, to avoid out by one
-   * errors!
+   * errors! Fires a property change event.
    * 
    * @param w
    *          width in residues
@@ -255,7 +272,7 @@ public class ViewportRanges extends ViewportProperties
   /**
    * Set viewport height in residues, without changing startSeq. Use in
    * preference to calculating endSeq from the height, to avoid out by one
-   * errors!
+   * errors! Fires a property change event.
    * 
    * @param h
    *          height in sequences
@@ -267,7 +284,8 @@ public class ViewportRanges extends ViewportProperties
 
   /**
    * Set viewport horizontal start position and width. Use in preference to
-   * calculating endRes from the width, to avoid out by one errors!
+   * calculating endRes from the width, to avoid out by one errors! Fires a
+   * property change event.
    * 
    * @param start
    *          start residue
@@ -276,12 +294,22 @@ public class ViewportRanges extends ViewportProperties
    */
   public void setViewportStartAndWidth(int start, int w)
   {
-    setStartEndRes(start, start + w - 1);
+    int vpstart = start;
+    if (vpstart < 0)
+    {
+      vpstart = 0;
+    }
+    else if (vpstart + w - 1 > al.getWidth() - 1)
+    {
+      vpstart = al.getWidth() - 1;
+    }
+    setStartEndRes(vpstart, vpstart + w - 1);
   }
 
   /**
    * Set viewport vertical start position and height. Use in preference to
-   * calculating endSeq from the height, to avoid out by one errors!
+   * calculating endSeq from the height, to avoid out by one errors! Fires a
+   * property change event.
    * 
    * @param start
    *          start sequence
@@ -290,7 +318,16 @@ public class ViewportRanges extends ViewportProperties
    */
   public void setViewportStartAndHeight(int start, int h)
   {
-    setStartEndSeq(start, start + h - 1);
+    int vpstart = start;
+    if (vpstart < 0)
+    {
+      vpstart = 0;
+    }
+    else if (vpstart + h - 1 > al.getHeight() - 1)
+    {
+      vpstart = al.getHeight() - h;
+    }
+    setStartEndSeq(vpstart, vpstart + h - 1);
   }
 
   /**
@@ -314,7 +351,7 @@ public class ViewportRanges extends ViewportProperties
   }
 
   /**
-   * Scroll the viewport range vertically
+   * Scroll the viewport range vertically. Fires a property change event.
    * 
    * @param up
    *          true if scrolling up, false if down
@@ -345,7 +382,7 @@ public class ViewportRanges extends ViewportProperties
   }
 
   /**
-   * Scroll the viewport range horizontally
+   * Scroll the viewport range horizontally. Fires a property change event.
    * 
    * @param right
    *          true if scrolling right, false if left
@@ -377,7 +414,8 @@ public class ViewportRanges extends ViewportProperties
   }
 
   /**
-   * Scroll a wrapped alignment so that the specified residue is visible
+   * Scroll a wrapped alignment so that the specified residue is visible. Fires
+   * a property change event.
    * 
    * @param res
    *          residue position to scroll to
@@ -391,7 +429,7 @@ public class ViewportRanges extends ViewportProperties
   }
 
   /**
-   * Scroll so that (x,y) is visible
+   * Scroll so that (x,y) is visible. Fires a property change event.
    * 
    * @param x
    *          x position in alignment
@@ -427,5 +465,20 @@ public class ViewportRanges extends ViewportProperties
       }
     }
   }
-
+  
+  /**
+   * Adjust sequence position for page up. Fires a property change event.
+   */
+  public void pageUp()
+  {
+    setViewportStartAndHeight(2 * startSeq - endSeq, getViewportHeight());
+  }
+  
+  /**
+   * Adjust sequence position for page down. Fires a property change event.
+   */
+  public void pageDown()
+  {
+    setViewportStartAndHeight(endSeq, getViewportHeight());
+  }
 }