JAL-2574 preparation for Sequence.findFeatures(col1, col2)
[jalview.git] / src / jalview / viewmodel / ViewportRanges.java
index 0583a8d..da39e36 100644 (file)
@@ -20,7 +20,9 @@
  */
 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
@@ -79,7 +81,24 @@ 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.
    * 
    * @param res
    *          residue position
@@ -102,7 +121,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;
@@ -116,6 +135,7 @@ public class ViewportRanges extends ViewportProperties
       startRes = start;
     }
 
+    int oldendres = this.endRes;
     if (end < 0)
     {
       endRes = 0;
@@ -125,37 +145,55 @@ public class ViewportRanges extends ViewportProperties
       endRes = end;
     }
 
-    changeSupport.firePropertyChange("startres", oldres, start);
+    changeSupport.firePropertyChange("startres", oldstartres, startRes);
+    if (oldstartres == startRes)
+    {
+      // 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, endRes);
+    }
   }
 
   /**
-   * Set last residue visible in the viewport
+   * Set last residue visible in the viewport. Fires a property change event.
    * 
    * @param res
    *          residue position
    */
   public void setEndRes(int res)
   {
+    int startres = res;
     int width = getViewportWidth();
-    setStartEndRes(res - width + 1, res);
+    if (startres + width - 1 > al.getWidth() - 1)
+    {
+      startres = al.getWidth() - width;
+    }
+    setStartEndRes(startres - width + 1, startres);
   }
 
   /**
-   * Set the first sequence visible in the viewport
+   * 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 > al.getHeight() - 1)
+    {
+      startseq = al.getHeight() - 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
@@ -164,7 +202,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;
@@ -178,6 +216,7 @@ public class ViewportRanges extends ViewportProperties
       startSeq = start;
     }
 
+    int oldendseq = this.endSeq;
     if (end >= al.getHeight())
     {
       endSeq = al.getHeight() - 1;
@@ -191,11 +230,18 @@ public class ViewportRanges extends ViewportProperties
       endSeq = end;
     }
 
-    changeSupport.firePropertyChange("startseq", oldseq, start);
+    changeSupport.firePropertyChange("startseq", oldstartseq, startSeq);
+    if (oldstartseq == startSeq)
+    {
+      // event won't be fired if start positions are the same
+      // fire in case the end positions changed
+      changeSupport.firePropertyChange("endseq", oldendseq, endSeq);
+    }
   }
 
   /**
-   * 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
@@ -241,7 +287,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
@@ -254,7 +300,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
@@ -266,7 +312,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
@@ -275,12 +322,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
@@ -289,7 +346,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);
   }
 
   /**
@@ -313,7 +379,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
@@ -344,7 +410,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
@@ -376,7 +442,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
@@ -389,4 +456,58 @@ public class ViewportRanges extends ViewportProperties
     setStartRes((res / width) * width);
   }
 
+  /**
+   * Scroll so that (x,y) is visible. Fires a property change event.
+   * 
+   * @param x
+   *          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)
+  {
+    while (y < startSeq)
+    {
+      scrollUp(true);
+    }
+    while (y > endSeq)
+    {
+      scrollUp(false);
+    }
+
+    HiddenColumns hidden = al.getHiddenColumns();
+    while (x < hidden.adjustForHiddenColumns(startRes))
+    {
+      if (!scrollRight(false))
+      {
+        break;
+      }
+    }
+    while (x > hidden.adjustForHiddenColumns(endRes))
+    {
+      if (!scrollRight(true))
+      {
+        break;
+      }
+    }
+  }
+  
+  /**
+   * 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());
+  }
 }