From 2ae1f76a9c13f23ba553500fea0e58ec920830e5 Mon Sep 17 00:00:00 2001 From: kiramt Date: Mon, 13 Nov 2017 09:52:52 +0000 Subject: [PATCH] JAL-2759 convert findColumnPosition to use cursor --- src/jalview/datamodel/HiddenColumns.java | 51 ++++++++++-------------- src/jalview/datamodel/HiddenColumnsCursor.java | 4 +- test/jalview/datamodel/HiddenColumnsTest.java | 3 ++ 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/jalview/datamodel/HiddenColumns.java b/src/jalview/datamodel/HiddenColumns.java index 084ea55..8c1a0fb 100644 --- a/src/jalview/datamodel/HiddenColumns.java +++ b/src/jalview/datamodel/HiddenColumns.java @@ -291,40 +291,33 @@ public class HiddenColumns { LOCK.readLock().lock(); int result = hiddenColumn; - int[] region = null; + // int[] region = null; if (hiddenColumns != null) { - Iterator it = new RegionsIterator(0, - hiddenColumn, hiddenColumns, cursor); - while (it.hasNext()) - { - region = it.next(); - if (hiddenColumn > region[1]) - { - result -= region[1] + 1 - region[0]; - } - } + int index = cursor.findRegionForColumn(hiddenColumn); + int hiddenBeforeCol = cursor.getHiddenSoFar(); + + // just subtract hidden cols count - this works fine if column is + // visible + result = hiddenColumn - hiddenBeforeCol; - if (region != null && hiddenColumn >= region[0] - && hiddenColumn <= region[1]) + // now check in case column is hidden - it will be in the returned + // hidden region + if (index < hiddenColumns.size()) { - // Here the hidden column is within a region, so - // we want to return the position of region[0]-1, adjusted for any - // earlier hidden columns. - // Calculate the difference between the actual hidden col position - // and region[0]-1, and then subtract from result to convert result - // from the adjusted hiddenColumn value to the adjusted region[0]-1 - // value. - - // However, if the region begins at 0 we cannot return region[0]-1 - // just return 0 - if (region[0] == 0) - { - return 0; - } - else + int[] region = hiddenColumns.get(index); + if (hiddenColumn >= region[0] && hiddenColumn <= region[1]) { - return result - (hiddenColumn - region[0] + 1); + // actually col is hidden, return region[0]-1 + // unless region[0]==0 in which case return 0 + if (region[0] == 0) + { + result = 0; + } + else + { + result = region[0] - 1 - hiddenBeforeCol; + } } } } diff --git a/src/jalview/datamodel/HiddenColumnsCursor.java b/src/jalview/datamodel/HiddenColumnsCursor.java index 7be6cca..9efcc0b 100644 --- a/src/jalview/datamodel/HiddenColumnsCursor.java +++ b/src/jalview/datamodel/HiddenColumnsCursor.java @@ -59,12 +59,12 @@ public class HiddenColumnsCursor } } - private synchronized int getIndex() + protected synchronized int getIndex() { return regionIndex; } - private synchronized int getHiddenSoFar() + protected synchronized int getHiddenSoFar() { return hiddenSoFar; } diff --git a/test/jalview/datamodel/HiddenColumnsTest.java b/test/jalview/datamodel/HiddenColumnsTest.java index 3147aeb..320a199 100644 --- a/test/jalview/datamodel/HiddenColumnsTest.java +++ b/test/jalview/datamodel/HiddenColumnsTest.java @@ -119,6 +119,9 @@ public class HiddenColumnsTest HiddenColumns cs3 = new HiddenColumns(); cs3.hideColumns(0, 4); assertEquals(0, cs3.findColumnPosition(2)); + + // check that column after the last hidden region doesn't crash + assertEquals(46, cs2.findColumnPosition(65)); } @Test(groups = { "Functional" }) -- 1.7.10.2