JAL-2050 JAL-1722 new getSelectedRange() method to quickly hide selected columns
[jalview.git] / src / jalview / datamodel / ColumnSelection.java
index a5d2ddd..c23b772 100644 (file)
@@ -170,6 +170,11 @@ public class ColumnSelection
       selected.or(mask);
     }
 
+    public boolean isSelected(int column)
+    {
+      return selected.get(column);
+    }
+
     public int getMaxColumn()
     {
       return selected.length() - 1;
@@ -179,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<int[]> getRanges()
+    {
+      List<int[]> rlist = new ArrayList<int[]>();
+      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();
@@ -250,15 +275,24 @@ 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<int[]> getSelectedRanges()
+  {
+    return selected.getRanges();
+  }
+
+  /**
    * 
    * @param col
    *          index to search for in column selection
    * 
-   * @return true if Integer(col) is in selection.
+   * @return true if col is selected
    */
   public boolean contains(int col)
   {
-    return selected.contains(new Integer(col));
+    return (col > -1) ? selected.isSelected(col) : false;
   }
 
   /**
@@ -618,6 +652,10 @@ public class ColumnSelection
 
   /**
    * Use this method to determine where the next hiddenRegion starts
+   * 
+   * @param hiddenRegion
+   *          index of hidden region (counts from 0)
+   * @return column number in visible view
    */
   public int findHiddenRegionPosition(int hiddenRegion)
   {
@@ -637,7 +675,7 @@ public class ColumnSelection
         gaps += region[1] + 1 - region[0];
         result = region[1] + 1;
         index++;
-      } while (index < hiddenRegion + 1);
+      } while (index <= hiddenRegion);
 
       result -= gaps;
     }
@@ -703,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();
     }
 
   }