From: kiramt Date: Fri, 17 Nov 2017 09:21:10 +0000 (+0000) Subject: JAL-2759 Convert reveal to use cursor X-Git-Tag: Release_2_10_4~55^2~1^2~70 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=44be050a48b372e5e29c74a0b838c73269501e0e;p=jalview.git JAL-2759 Convert reveal to use cursor --- diff --git a/src/jalview/datamodel/HiddenColumns.java b/src/jalview/datamodel/HiddenColumns.java index 8a67741..050603e 100644 --- a/src/jalview/datamodel/HiddenColumns.java +++ b/src/jalview/datamodel/HiddenColumns.java @@ -289,55 +289,9 @@ public class HiddenColumns { try { - /* LOCK.readLock().lock(); - int result = hiddenColumn; - 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]; - } - } - - if (region != null && hiddenColumn >= region[0] - && hiddenColumn <= region[1]) - { - // 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 - { - return result - (hiddenColumn - region[0] + 1); - } - } - } - return result; // return the shifted position after removing hidden - // columns. - } finally - { - LOCK.readLock().unlock(); - }*/ - LOCK.readLock().lock(); int result = hiddenColumn; - // int[] region = null; + if (hiddenColumns != null) { int index = cursor.findRegionForColumn(hiddenColumn); @@ -367,7 +321,6 @@ public class HiddenColumns } } } - System.out.println(hiddenColumn + " " + result); return result; // return the shifted position after removing hidden // columns. } finally @@ -989,31 +942,32 @@ public class HiddenColumns try { LOCK.writeLock().lock(); - Iterator it = new RegionsIterator(start, start, hiddenColumns, - cursor); - while (it.hasNext()) + + if (hiddenColumns != null) { - int[] region = it.next(); - if (start == region[0]) + int regionIndex = cursor.findRegionForColumn(start); + + if (regionIndex != -1 && regionIndex != hiddenColumns.size()) { - for (int j = region[0]; j < region[1] + 1; j++) + // regionIndex is the region which either contains start + // or lies to the right of start + int[] region = hiddenColumns.get(regionIndex); + if (start == region[0]) { - sel.addElement(j); + for (int j = region[0]; j < region[1] + 1; j++) + { + sel.addElement(j); + } + hiddenColumns.remove(regionIndex); + + if (hiddenColumns.isEmpty()) + { + hiddenColumns = null; + } + cursor.updateForDeletedRegion(hiddenColumns); } - it.remove(); - break; - } - else if (start < region[0]) - { - break; // passed all possible matching regions } } - - if (hiddenColumns.size() == 0) - { - hiddenColumns = null; - } - cursor.resetCursor(hiddenColumns); } finally { LOCK.writeLock().unlock(); @@ -1330,14 +1284,16 @@ public class HiddenColumns if (hiddenColumns != null) { int regionindex = cursor.findRegionForColumn(adjres - 1); - if (hiddenColumns.get(regionindex)[1] == adjres - 1) + if (regionindex < hiddenColumns.size() + && hiddenColumns.get(regionindex)[1] == adjres - 1) { reveal = hiddenColumns.get(regionindex); } else { regionindex = cursor.findRegionForColumn(adjres + 1); - if (hiddenColumns.get(regionindex)[0] == adjres + 1) + if (regionindex < hiddenColumns.size() + && hiddenColumns.get(regionindex)[0] == adjres + 1) { reveal = hiddenColumns.get(regionindex); } diff --git a/src/jalview/datamodel/HiddenColumnsCursor.java b/src/jalview/datamodel/HiddenColumnsCursor.java index b40afdf..2d579b0 100644 --- a/src/jalview/datamodel/HiddenColumnsCursor.java +++ b/src/jalview/datamodel/HiddenColumnsCursor.java @@ -43,22 +43,15 @@ public class HiddenColumnsCursor /** * Set the cursor to a position * - * @param first - * absolute position of first hidden column - * @param last - * absolute position of last hidden column - * @param index - * index of last visited region - * @param hiddenCount - * number of hidden columns before last visited region + * @param hiddenCols */ protected void resetCursor(List hiddenCols) { synchronized (this) { + hiddenColumns = hiddenCols; if ((hiddenCols != null) && (!hiddenCols.isEmpty())) { - hiddenColumns = hiddenCols; firstColumn = hiddenColumns.get(0)[0]; regionIndex = 0; hiddenSoFar = 0; @@ -66,6 +59,31 @@ public class HiddenColumnsCursor } } + /** + * Delete the region the cursor is currently at. Avoids having to reset the + * cursor just because we deleted a region. + * + * @param hiddenCols + */ + protected void updateForDeletedRegion(List hiddenCols) + { + + if ((hiddenCols != null) && (!hiddenCols.isEmpty())) + { + // if there is a region to the right of the current region, + // nothing changes; otherwise + // we deleted the last region (index=hiddenCols.size()-1) + // or the index was at the end of the alignment (index=hiddenCols.size()) + if (regionIndex >= hiddenColumns.size() - 1) + { + // deleted last region, index is now end of alignment + regionIndex = hiddenCols.size(); + } + } + + hiddenColumns = hiddenCols; + } + protected void updateCursor(int index, int hiddenCount) { synchronized (this) diff --git a/test/jalview/datamodel/HiddenColumnsTest.java b/test/jalview/datamodel/HiddenColumnsTest.java index 320a199..fbeaa71 100644 --- a/test/jalview/datamodel/HiddenColumnsTest.java +++ b/test/jalview/datamodel/HiddenColumnsTest.java @@ -774,6 +774,9 @@ public class HiddenColumnsTest result = hc.getRegionWithEdgeAtRes(8); assertEquals(14, result[0]); assertEquals(15, result[1]); + + result = hc.getRegionWithEdgeAtRes(16); + assertNull(result); } @Test(groups = "Functional")