JAL-1858 scrollToWrappedVisible preserve any horizontal offset of view
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 6 Jul 2017 16:38:18 +0000 (17:38 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 6 Jul 2017 16:38:18 +0000 (17:38 +0100)
src/jalview/viewmodel/ViewportRanges.java
test/jalview/viewmodel/ViewportRangesTest.java

index c2bcf3f..f14b10e 100644 (file)
@@ -459,18 +459,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;
   }
 
   /**
index 636f8dd..e942d22 100644 (file)
@@ -1,6 +1,7 @@
 package jalview.viewmodel;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertTrue;
 
 import jalview.analysis.AlignmentGenerator;
@@ -347,17 +348,41 @@ public class ViewportRangesTest {
   @Test(groups = { "Functional" })
   public void testScrollToWrappedVisible()
   {
-    ViewportRanges vr = new ViewportRanges(al);
+    AlignmentI al2 = gen.generate(60, 30, 1, 5, 5);
+
+    ViewportRanges vr = new ViewportRanges(al2);
+
+    // start with viewport on 5-14
     vr.setViewportStartAndWidth(5, 10);
+    assertEquals(vr.getStartRes(), 5);
+    assertEquals(vr.getEndRes(), 14);
+
+    // scroll to 12 - no change
+    assertFalse(vr.scrollToWrappedVisible(12));
+    assertEquals(vr.getStartRes(), 5);
 
-    vr.scrollToWrappedVisible(0);
+    // scroll to 2 - back to 0-9
+    assertTrue(vr.scrollToWrappedVisible(2));
     assertEquals(vr.getStartRes(), 0);
+    assertEquals(vr.getEndRes(), 9);
 
-    vr.scrollToWrappedVisible(10);
-    assertEquals(vr.getStartRes(), 10);
+    // scroll to 9 - no change
+    assertFalse(vr.scrollToWrappedVisible(9));
+    assertEquals(vr.getStartRes(), 0);
 
-    vr.scrollToWrappedVisible(15);
+    // scroll to 12 - moves to 10-19
+    assertTrue(vr.scrollToWrappedVisible(12));
     assertEquals(vr.getStartRes(), 10);
+    assertEquals(vr.getEndRes(), 19);
+
+    vr.setStartRes(13);
+    assertEquals(vr.getStartRes(), 13);
+    assertEquals(vr.getEndRes(), 22);
+
+    // scroll to 45 - jumps to 43-52
+    assertTrue(vr.scrollToWrappedVisible(45));
+    assertEquals(vr.getStartRes(), 43);
+    assertEquals(vr.getEndRes(), 52);
   }
 
   // leave until JAL-2388 is merged and we can do without viewport