Merge branch 'develop' into features/JAL-2446NCList
[jalview.git] / src / jalview / viewmodel / ViewportRanges.java
index f14b10e..49d0f65 100644 (file)
@@ -40,6 +40,8 @@ public class ViewportRanges extends ViewportProperties
 
   public static final String ENDSEQ = "endseq";
 
+  private boolean wrappedMode = false;
+
   // start residue of viewport
   private int startRes;
 
@@ -129,9 +131,14 @@ public class ViewportRanges extends ViewportProperties
   public void setStartEndRes(int start, int end)
   {
     int oldstartres = this.startRes;
-    if (start > getVisibleAlignmentWidth() - 1)
+
+    /*
+     * if not wrapped, don't leave white space at the right margin
+     */
+    int lastColumn = getVisibleAlignmentWidth() - 1;
+    if (!wrappedMode && (start > lastColumn))
     {
-      startRes = Math.max(getVisibleAlignmentWidth() - 1, 0);
+      startRes = Math.max(lastColumn, 0);
     }
     else if (start < 0)
     {
@@ -147,9 +154,9 @@ public class ViewportRanges extends ViewportProperties
     {
       endRes = 0;
     }
-    else if (end > getVisibleAlignmentWidth() - 1)
+    else if (!wrappedMode && (end > lastColumn))
     {
-      endRes = Math.max(getVisibleAlignmentWidth() - 1, 0);
+      endRes = Math.max(lastColumn, 0);
     }
     else
     {
@@ -166,23 +173,6 @@ public class ViewportRanges extends ViewportProperties
   }
 
   /**
-   * 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();
-    if (startres + width - 1 > getVisibleAlignmentWidth() - 1)
-    {
-      startres = getVisibleAlignmentWidth() - width;
-    }
-    setStartEndRes(startres - width + 1, startres);
-  }
-
-  /**
    * 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.
@@ -214,9 +204,10 @@ public class ViewportRanges extends ViewportProperties
   public void setStartEndSeq(int start, int end)
   {
     int oldstartseq = this.startSeq;
-    if (start > getVisibleAlignmentHeight() - 1)
+    int visibleHeight = getVisibleAlignmentHeight();
+    if (start > visibleHeight - 1)
     {
-      startSeq = Math.max(getVisibleAlignmentHeight() - 1, 0);
+      startSeq = Math.max(visibleHeight - 1, 0);
     }
     else if (start < 0)
     {
@@ -228,9 +219,9 @@ public class ViewportRanges extends ViewportProperties
     }
 
     int oldendseq = this.endSeq;
-    if (end >= getVisibleAlignmentHeight())
+    if (end >= visibleHeight)
     {
-      endSeq = Math.max(getVisibleAlignmentHeight() - 1, 0);
+      endSeq = Math.max(visibleHeight - 1, 0);
     }
     else if (end < 0)
     {
@@ -338,12 +329,18 @@ public class ViewportRanges extends ViewportProperties
     {
       vpstart = 0;
     }
-    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
+
+    /*
+     * if not wrapped, don't leave white space at the right margin
+     */
+    if (!wrappedMode)
     {
-      vpstart = getVisibleAlignmentWidth() - w;
+      if ((w <= getVisibleAlignmentWidth())
+              && (vpstart + w - 1 > getVisibleAlignmentWidth() - 1))
+      {
+        vpstart = getVisibleAlignmentWidth() - w;
+      }
+
     }
     setStartEndRes(vpstart, vpstart + w - 1);
   }
@@ -538,7 +535,15 @@ public class ViewportRanges extends ViewportProperties
    */
   public void pageUp()
   {
-    setViewportStartAndHeight(2 * startSeq - endSeq, getViewportHeight());
+    if (wrappedMode)
+    {
+      setStartRes(Math.max(0, getStartRes() - getViewportWidth()));
+    }
+    else
+    {
+      setViewportStartAndHeight(startSeq - (endSeq - startSeq),
+              getViewportHeight());
+    }
   }
   
   /**
@@ -546,6 +551,94 @@ public class ViewportRanges extends ViewportProperties
    */
   public void pageDown()
   {
-    setViewportStartAndHeight(endSeq, getViewportHeight());
+    if (wrappedMode)
+    {
+      /*
+       * if height is more than width (i.e. not all sequences fit on screen),
+       * increase page down to height
+       */
+      int newStart = getStartRes()
+              + Math.max(getViewportHeight(), getViewportWidth());
+
+      /*
+       * don't page down beyond end of alignment, or if not all
+       * sequences fit in the visible height
+       */
+      if (newStart < getVisibleAlignmentWidth())
+      {
+        setStartRes(newStart);
+      }
+    }
+    else
+    {
+      setViewportStartAndHeight(endSeq, getViewportHeight());
+    }
+  }
+
+  public void setWrappedMode(boolean wrapped)
+  {
+    wrappedMode = wrapped;
+  }
+
+  public boolean isWrappedMode()
+  {
+    return wrappedMode;
+  }
+
+  /**
+   * Answers the vertical scroll position (0..) to set, given the visible column
+   * that is at top left.
+   * 
+   * <pre>
+   * Example:
+   *    viewport width 40 columns (0-39, 40-79, 80-119...)
+   *    column 0 returns scroll position 0
+   *    columns 1-40 return scroll position 1
+   *    columns 41-80 return scroll position 2
+   *    etc
+   * </pre>
+   * 
+   * @param topLeftColumn
+   *          (0..)
+   * @return
+   */
+  public int getWrappedScrollPosition(final int topLeftColumn)
+  {
+    int w = getViewportWidth();
+
+    /*
+     * visible whole widths
+     */
+    int scroll = topLeftColumn / w;
+
+    /*
+     * add 1 for a part width if there is one
+     */
+    scroll += topLeftColumn % w > 0 ? 1 : 0;
+
+    return scroll;
+  }
+
+  /**
+   * Answers the maximum wrapped vertical scroll value, given the column
+   * position (0..) to show at top left of the visible region.
+   * 
+   * @param topLeftColumn
+   * @return
+   */
+  public int getWrappedMaxScroll(int topLeftColumn)
+  {
+    int scrollPosition = getWrappedScrollPosition(topLeftColumn);
+
+    /*
+     * how many more widths could be drawn after this one?
+     */
+    int columnsRemaining = getVisibleAlignmentWidth() - topLeftColumn;
+    int width = getViewportWidth();
+    int widthsRemaining = columnsRemaining / width
+            + (columnsRemaining % width > 0 ? 1 : 0) - 1;
+    int maxScroll = scrollPosition + widthsRemaining;
+
+    return maxScroll;
   }
 }