From e54df488cd82761bfb0e9372aeae2acc5a1324d7 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Tue, 18 Jul 2017 13:02:15 +0100 Subject: [PATCH] JAL-147 corrected calculation of vertical scroll position and max --- src/jalview/appletgui/AlignmentPanel.java | 9 ++++--- src/jalview/gui/AlignmentPanel.java | 9 ++++--- src/jalview/viewmodel/ViewportRanges.java | 32 +++++++++++++++++++++--- test/jalview/viewmodel/ViewportRangesTest.java | 31 +++++++++++++++++++++++ 4 files changed, 72 insertions(+), 9 deletions(-) diff --git a/src/jalview/appletgui/AlignmentPanel.java b/src/jalview/appletgui/AlignmentPanel.java index edf6ad4..f8f31b2 100644 --- a/src/jalview/appletgui/AlignmentPanel.java +++ b/src/jalview/appletgui/AlignmentPanel.java @@ -994,11 +994,14 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, private void setScrollingForWrappedPanel(int topLeftColumn) { int scrollPosition = vpRanges.getWrappedScrollPosition(topLeftColumn); - int maxScroll = vpRanges.getWrappedScrollPosition(vpRanges - .getVisibleAlignmentWidth() - 1); + int maxScroll = vpRanges.getWrappedMaxScroll(topLeftColumn); + /* + * a scrollbar's value can be set to at most (maximum-extent) + * so we add extent (1) to the maxScroll value + */ vscroll.setUnitIncrement(1); - vscroll.setValues(scrollPosition, 1, 0, maxScroll); + vscroll.setValues(scrollPosition, 1, 0, maxScroll + 1); } protected Panel sequenceHolderPanel = new Panel(); diff --git a/src/jalview/gui/AlignmentPanel.java b/src/jalview/gui/AlignmentPanel.java index 9d21a6c..a732527 100644 --- a/src/jalview/gui/AlignmentPanel.java +++ b/src/jalview/gui/AlignmentPanel.java @@ -902,11 +902,14 @@ public class AlignmentPanel extends GAlignmentPanel implements private void setScrollingForWrappedPanel(int topLeftColumn) { int scrollPosition = vpRanges.getWrappedScrollPosition(topLeftColumn); - int maxScroll = vpRanges.getWrappedScrollPosition(vpRanges - .getVisibleAlignmentWidth() - 1); + int maxScroll = vpRanges.getWrappedMaxScroll(topLeftColumn); + /* + * a scrollbar's value can be set to at most (maximum-extent) + * so we add extent (1) to the maxScroll value + */ vscroll.setUnitIncrement(1); - vscroll.setValues(scrollPosition, 1, 0, maxScroll); + vscroll.setValues(scrollPosition, 1, 0, maxScroll + 1); } /** diff --git a/src/jalview/viewmodel/ViewportRanges.java b/src/jalview/viewmodel/ViewportRanges.java index 7c6b7ab..10cf583 100644 --- a/src/jalview/viewmodel/ViewportRanges.java +++ b/src/jalview/viewmodel/ViewportRanges.java @@ -563,14 +563,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. * *
    * 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
    * 
@@ -587,8 +586,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; + } } diff --git a/test/jalview/viewmodel/ViewportRangesTest.java b/test/jalview/viewmodel/ViewportRangesTest.java index 86f0d62..70a3687 100644 --- a/test/jalview/viewmodel/ViewportRangesTest.java +++ b/test/jalview/viewmodel/ViewportRangesTest.java @@ -699,6 +699,37 @@ public class ViewportRangesTest { assertEquals(vrsmall.getViewportWidth(), 7); assertEquals(vrsmall.getStartRes(), 0); } + + @Test(groups = { "Functional" }) + public void testGetWrappedMaxScroll() + { + // generate an ungapped alignment of width 140 + int alignmentWidth = 140; + AlignmentI al2 = gen.generate(alignmentWidth, 15, 1, 0, 5); + ViewportRanges vr = new ViewportRanges(al2); + vr.setStartEndRes(0, 39); + int width = vr.getViewportWidth(); // 40 + int partWidth = alignmentWidth % width; // 20 + + /* + * there are 3 * 40 remainder 20 residues + * number of widths depends on offset (scroll right) + * 4 widths (maxScroll = 3) if offset by 0 or more than 19 columns + * 5 widths (maxScroll = 4) if 1 <= offset <= 19 + */ + for (int col = 0; col < alignmentWidth; col++) + { + int offset = col % width; + if (offset > 0 && offset < partWidth) + { + assertEquals(vr.getWrappedMaxScroll(col), 4, "col " + col); + } + else + { + assertEquals(vr.getWrappedMaxScroll(col), 3, "col " + col); + } + } + } } // mock listener for property change events -- 1.7.10.2