JAL-2370 fix bug when hiding a range including two or more hidden ranges
[jalview.git] / src / jalview / datamodel / ColumnSelection.java
index 7ef2a68..c467f4a 100644 (file)
@@ -45,19 +45,52 @@ public class ColumnSelection
     /*
      * list of selected columns (ordered by selection order, not column order)
      */
-    private List<Integer> order = new ArrayList<Integer>();
+    private List<Integer> order;
+
+    /*
+     * an unmodifiable view of the selected columns list
+     */
+    private List<Integer> _uorder;
 
     /**
      * bitfield for column selection - allows quick lookup
      */
-    private BitSet selected = new BitSet();
+    private BitSet selected;
+
+    /**
+     * Constructor
+     */
+    IntList()
+    {
+      order = new ArrayList<Integer>();
+      _uorder = Collections.unmodifiableList(order);
+      selected = new BitSet();
+    }
+
+    /**
+     * Copy constructor
+     * 
+     * @param other
+     */
+    IntList(IntList other)
+    {
+      this();
+      if (other != null)
+      {
+        int j = other.size();
+        for (int i = 0; i < j; i++)
+        {
+          add(other.elementAt(i));
+        }
+      }
+    }
 
     /**
      * adds a new column i to the selection - only if i is not already selected
      * 
      * @param i
      */
-    public void add(int i)
+    void add(int i)
     {
       if (!selected.get(i))
       {
@@ -66,13 +99,13 @@ public class ColumnSelection
       }
     }
 
-    public void clear()
+    void clear()
     {
       order.clear();
       selected.clear();
     }
 
-    public void remove(int col)
+    void remove(int col)
     {
 
       Integer colInt = new Integer(col);
@@ -87,22 +120,27 @@ public class ColumnSelection
       }
     }
 
-    public boolean contains(Integer colInt)
+    boolean contains(Integer colInt)
     {
       return selected.get(colInt);
     }
 
-    public boolean isEmpty()
+    boolean isEmpty()
     {
       return order.isEmpty();
     }
 
-    public List<Integer> getList()
+    /**
+     * Returns a read-only view of the selected columns list
+     * 
+     * @return
+     */
+    List<Integer> getList()
     {
-      return order;
+      return _uorder;
     }
 
-    public int size()
+    int size()
     {
       return order.size();
     }
@@ -113,7 +151,7 @@ public class ColumnSelection
      * @param i
      * @return
      */
-    public int elementAt(int i)
+    int elementAt(int i)
     {
       return order.get(i);
     }
@@ -156,7 +194,7 @@ public class ColumnSelection
      * @param change
      *          - delta for shift
      */
-    public void compensateForEdits(int start, int change)
+    void compensateForEdits(int start, int change)
     {
       BitSet mask = new BitSet();
       for (int i = 0; i < order.size(); i++)
@@ -175,17 +213,17 @@ public class ColumnSelection
       selected.or(mask);
     }
 
-    public boolean isSelected(int column)
+    boolean isSelected(int column)
     {
       return selected.get(column);
     }
 
-    public int getMaxColumn()
+    int getMaxColumn()
     {
       return selected.length() - 1;
     }
 
-    public int getMinColumn()
+    int getMinColumn()
     {
       return selected.get(0) ? 0 : selected.nextSetBit(0);
     }
@@ -193,7 +231,7 @@ public class ColumnSelection
     /**
      * @return a series of selection intervals along the range
      */
-    public List<int[]> getRanges()
+    List<int[]> getRanges()
     {
       List<int[]> rlist = new ArrayList<int[]>();
       if (selected.isEmpty())
@@ -288,14 +326,18 @@ public class ColumnSelection
   }
 
   /**
-   * Returns a list of selected columns. The list contains no duplicates but is
-   * not necessarily ordered. It also may include columns hidden from the
-   * current view. This returns a copy of the actual list, and changes to the
-   * copy will not affect the selection.
+   * Returns a read-only view of the (possibly empty) list of selected columns
+   * <p>
+   * The list contains no duplicates but is not necessarily ordered. It also may
+   * include columns hidden from the current view. To modify (for example sort)
+   * the list, you should first make a copy.
+   * <p>
+   * The list is not thread-safe: iterating over it could result in
+   * ConcurrentModificationException if it is modified by another thread.
    */
   public List<Integer> getSelected()
   {
-    return new ArrayList<Integer>(selection.getList());
+    return selection.getList();
   }
 
   /**
@@ -824,6 +866,24 @@ public class ColumnSelection
          */
         region[0] = Math.min(region[0], start);
         region[1] = Math.max(region[1], end);
+
+        /*
+         * also update or remove any subsequent ranges 
+         * that are overlapped
+         */
+        while (i < hiddenColumns.size() - 1)
+        {
+          int[] nextRegion = hiddenColumns.get(i + 1);
+          if (nextRegion[0] > end + 1)
+          {
+            /*
+             * gap to next hidden range - no more to update
+             */
+            break;
+          }
+          region[1] = Math.max(nextRegion[1], end);
+          hiddenColumns.remove(i + 1);
+        }
         return;
       }
     }
@@ -949,14 +1009,7 @@ public class ColumnSelection
   {
     if (copy != null)
     {
-      if (copy.selection != null)
-      {
-        selection = new IntList();
-        for (int i = 0, j = copy.selection.size(); i < j; i++)
-        {
-          selection.add(copy.selection.elementAt(i));
-        }
-      }
+      selection = new IntList(copy.selection);
       if (copy.hiddenColumns != null)
       {
         hiddenColumns = new Vector<int[]>(copy.hiddenColumns.size());
@@ -1113,9 +1166,9 @@ public class ColumnSelection
    */
   public int[] locateVisibleBoundsOfSequence(SequenceI seq)
   {
-    int fpos=seq.getStart(),lpos= seq.getEnd();
+    int fpos = seq.getStart(), lpos = seq.getEnd();
     int start = 0;
-    
+
     if (hiddenColumns == null || hiddenColumns.size() == 0)
     {
       int ifpos = seq.findIndex(fpos) - 1, ilpos = seq.findIndex(lpos) - 1;