From f789d3fe25fd4785dcba27d5d39275959109eb5f Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 6 Jul 2017 17:38:18 +0100 Subject: [PATCH] JAL-1858 scrollToWrappedVisible preserve any horizontal offset of view --- src/jalview/viewmodel/ViewportRanges.java | 36 ++++++++++++++++++++---- test/jalview/viewmodel/ViewportRangesTest.java | 35 +++++++++++++++++++---- 2 files changed, 60 insertions(+), 11 deletions(-) diff --git a/src/jalview/viewmodel/ViewportRanges.java b/src/jalview/viewmodel/ViewportRanges.java index c2bcf3f..f14b10e 100644 --- a/src/jalview/viewmodel/ViewportRanges.java +++ b/src/jalview/viewmodel/ViewportRanges.java @@ -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; } /** diff --git a/test/jalview/viewmodel/ViewportRangesTest.java b/test/jalview/viewmodel/ViewportRangesTest.java index 636f8dd..e942d22 100644 --- a/test/jalview/viewmodel/ViewportRangesTest.java +++ b/test/jalview/viewmodel/ViewportRangesTest.java @@ -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 -- 1.7.10.2