From: kiramt Date: Fri, 3 Mar 2017 15:00:11 +0000 (+0000) Subject: JAL-2429 Fix to ColumnSelection::findColumnPosition X-Git-Tag: Release_2_10_2~3^2~92^2~55 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=d7b1660e7b10c0370d8aa1cc6f84b98f30fa1f96;p=jalview.git JAL-2429 Fix to ColumnSelection::findColumnPosition --- diff --git a/src/jalview/datamodel/ColumnSelection.java b/src/jalview/datamodel/ColumnSelection.java index 98a7fe2..7459033 100644 --- a/src/jalview/datamodel/ColumnSelection.java +++ b/src/jalview/datamodel/ColumnSelection.java @@ -690,8 +690,8 @@ public class ColumnSelection * left-most visible column will always be returned. * * @param hiddenColumn - * int - * @return int + * the column index in the full alignment including hidden columns + * @return the position of the column in the visible alignment */ public int findColumnPosition(int hiddenColumn) { @@ -708,9 +708,17 @@ public class ColumnSelection result -= region[1] + 1 - region[0]; } } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size())); - if (hiddenColumn > region[0] && hiddenColumn < region[1]) - { - return region[0] + hiddenColumn - result; + + if (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 + + return result - (hiddenColumn - region[0] + 1); } } return result; // return the shifted position after removing hidden columns. diff --git a/test/jalview/datamodel/ColumnSelectionTest.java b/test/jalview/datamodel/ColumnSelectionTest.java index 1d819c9..63d5e56 100644 --- a/test/jalview/datamodel/ColumnSelectionTest.java +++ b/test/jalview/datamodel/ColumnSelectionTest.java @@ -105,9 +105,31 @@ public class ColumnSelectionTest cs.hideColumns(4, 4); assertEquals(4, cs.findColumnPosition(5)); + // hiding column 4 moves column 4 to position 3 + assertEquals(3, cs.findColumnPosition(4)); + // hiding columns 1 and 2 moves column 5 to column 2 cs.hideColumns(1, 2); assertEquals(2, cs.findColumnPosition(5)); + + // check with > 1 hidden column regions + // where some columns are in the hidden regions + ColumnSelection cs2 = new ColumnSelection(); + cs2.hideColumns(5, 10); + cs2.hideColumns(20, 27); + cs2.hideColumns(40, 44); + + // hiding columns 5-10 and 20-27 moves column 8 to column 4 + assertEquals(4, cs2.findColumnPosition(8)); + + // and moves column 24 to 13 + assertEquals(13, cs2.findColumnPosition(24)); + + // and moves column 28 to 14 + assertEquals(14, cs2.findColumnPosition(28)); + + // and moves column 40 to 25 + assertEquals(25, cs2.findColumnPosition(40)); } /**