Merge branch 'develop' into features/JAL-2446NCList
[jalview.git] / src / jalview / viewmodel / ViewportRanges.java
index 7c6b7ab..42d490e 100644 (file)
@@ -456,18 +456,42 @@ public class ViewportRanges extends ViewportProperties
   }
 
   /**
-   * Scroll a wrapped alignment so that the specified residue is visible. Fires
-   * a property change event.
+   * Scroll a wrapped alignment so that the specified residue is in the first
+   * repeat of the wrapped view. Fires a property change event. Answers true if
+   * the startRes changed, else false.
    * 
    * @param res
    *          residue position to scroll to
+   * @return
    */
-  public void scrollToWrappedVisible(int res)
+  public boolean scrollToWrappedVisible(int res)
   {
-    // get the start residue of the wrapped row which res is in
-    // and set that as our start residue
+    int oldStartRes = startRes;
     int width = getViewportWidth();
-    setStartRes((res / width) * width);
+
+    if (res >= oldStartRes && res < oldStartRes + width)
+    {
+      return false;
+    }
+
+    boolean up = res < oldStartRes;
+    int widthsToScroll = Math.abs((res - oldStartRes) / width);
+    if (up)
+    {
+      widthsToScroll++;
+    }
+
+    int residuesToScroll = width * widthsToScroll;
+    int newStartRes = up ? oldStartRes - residuesToScroll : oldStartRes
+            + residuesToScroll;
+    if (newStartRes < 0)
+    {
+      newStartRes = 0;
+    }
+
+    setStartRes(newStartRes);
+
+    return true;
   }
 
   /**
@@ -505,7 +529,7 @@ public class ViewportRanges extends ViewportProperties
       }
     }
   }
-  
+
   /**
    * Adjust sequence position for page up. Fires a property change event.
    */
@@ -521,7 +545,7 @@ public class ViewportRanges extends ViewportProperties
               getViewportHeight());
     }
   }
-  
+
   /**
    * Adjust sequence position for page down. Fires a property change event.
    */
@@ -563,14 +587,13 @@ public class ViewportRanges extends ViewportProperties
 
   /**
    * Answers the vertical scroll position (0..) to set, given the visible column
-   * that is at top left. Note that if called with the total visible width of
-   * the alignment, this gives the maximum cursor scroll value.
+   * that is at top left.
    * 
    * <pre>
    * Example:
    *    viewport width 40 columns (0-39, 40-79, 80-119...)
    *    column 0 returns scroll position 0
-   *    columns 0-40 return scroll position 1
+   *    columns 1-40 return scroll position 1
    *    columns 41-80 return scroll position 2
    *    etc
    * </pre>
@@ -587,8 +610,35 @@ public class ViewportRanges extends ViewportProperties
      * 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;
+  }
 }