From 964c6de914dd7b2ef51feeb0d374cca23742f3ef Mon Sep 17 00:00:00 2001 From: Jim Procter Date: Fri, 8 Apr 2016 16:18:15 +0100 Subject: [PATCH] JAL-2050 JAL-1722 new getSelectedRange() method to quickly hide selected columns --- src/jalview/datamodel/ColumnSelection.java | 39 ++++++++++++++++++++--- test/jalview/datamodel/ColumnSelectionTest.java | 26 +++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/src/jalview/datamodel/ColumnSelection.java b/src/jalview/datamodel/ColumnSelection.java index 0d2d3bf..c23b772 100644 --- a/src/jalview/datamodel/ColumnSelection.java +++ b/src/jalview/datamodel/ColumnSelection.java @@ -184,6 +184,26 @@ public class ColumnSelection { return selected.get(0) ? 0 : selected.nextSetBit(0); } + + /** + * @return a series of selection intervals along the range + */ + public List getRanges() + { + List rlist = new ArrayList(); + if (selected.isEmpty()) + { + return rlist; + } + int next = selected.nextSetBit(0), clear = -1; + while (next != -1) + { + clear = selected.nextClearBit(next); + rlist.add(new int[] { next, clear - 1 }); + next = selected.nextSetBit(clear); + } + return rlist; + } } IntList selected = new IntList(); @@ -255,6 +275,15 @@ public class ColumnSelection } /** + * @return list of int arrays containing start and end column position for + * runs of selected columns ordered from right to left. + */ + public List getSelectedRanges() + { + return selected.getRanges(); + } + + /** * * @param col * index to search for in column selection @@ -712,10 +741,12 @@ public class ColumnSelection public void hideSelectedColumns() { - while (!selected.isEmpty()) - { - int column = selected.elementAt(0); - hideColumns(column); + synchronized (selected) { + for (int[] selregions:selected.getRanges()) + { + hideColumns(selregions[0], selregions[1]); + } + selected.clear(); } } diff --git a/test/jalview/datamodel/ColumnSelectionTest.java b/test/jalview/datamodel/ColumnSelectionTest.java index 0f08ceb..0245b15 100644 --- a/test/jalview/datamodel/ColumnSelectionTest.java +++ b/test/jalview/datamodel/ColumnSelectionTest.java @@ -244,6 +244,32 @@ public class ColumnSelectionTest } /** + * Test the method that gets runs of selected columns ordered by column. If + * this fails, HideSelectedColumns may also fail + */ + @Test(groups = { "Functional" }) + public void testgetSelectedRanges() + { + ColumnSelection cs = new ColumnSelection(); + int[] sel = { 2, 3, 4, 7, 8, 9, 20, 21, 22 }; + for (int col : sel) + { + cs.addElement(col); + } + List range; + range = cs.getSelectedRanges(); + assertEquals(3, range.size()); + assertEquals("[2, 4]", Arrays.toString(range.get(0))); + assertEquals("[7, 9]", Arrays.toString(range.get(1))); + assertEquals("[20, 22]", Arrays.toString(range.get(2))); + cs.addElement(0); + cs.addElement(1); + range = cs.getSelectedRanges(); + assertEquals(3, range.size()); + assertEquals("[0, 4]", Arrays.toString(range.get(0))); + } + + /** * Test the method that reveals a range of hidden columns given the start * column of the range */ -- 1.7.10.2