JAL-1551 JAL-962 formatting and code change ripples from changing HiddenColumns ...
[jalview.git] / src / jalview / datamodel / ColumnSelection.java
index 5518588..f1c6cc4 100644 (file)
@@ -1,26 +1,31 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
  * Copyright (C) 2014 The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  * Jalview is free software: you can redistribute it and/or
  * modify it under the terms of the GNU General Public License 
- * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
  *  
  * Jalview is distributed in the hope that it will be useful, but 
  * WITHOUT ANY WARRANTY; without even the implied warranty 
  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
  * PURPOSE.  See the GNU General Public License for more details.
  * 
- * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
  * The Jalview Authors are detailed in the 'AUTHORS' file.
  */
 package jalview.datamodel;
 
-import java.util.*;
+import jalview.util.ShiftList;
 
-import jalview.util.*;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Vector;
 
 /**
  * NOTE: Columns are zero based.
@@ -30,7 +35,7 @@ public class ColumnSelection
   Vector selected = new Vector();
 
   // Vector of int [] {startCol, endCol}
-  Vector hiddenColumns;
+  Vector<int[]> hiddenColumns;
 
   /**
    * Add a column to the selection
@@ -184,9 +189,9 @@ public class ColumnSelection
    * @param left
    *          shift in edit (+ve for removal, or -ve for inserts)
    */
-  public Vector compensateForEdit(int start, int change)
+  public List<int[]> compensateForEdit(int start, int change)
   {
-    Vector deletedHiddenColumns = null;
+    List<int[]> deletedHiddenColumns = null;
     for (int i = 0; i < size(); i++)
     {
       int temp = columnAt(i);
@@ -199,14 +204,14 @@ public class ColumnSelection
 
     if (hiddenColumns != null)
     {
-      deletedHiddenColumns = new Vector();
+      deletedHiddenColumns = new ArrayList<int[]>();
       int hSize = hiddenColumns.size();
       for (int i = 0; i < hSize; i++)
       {
-        int[] region = (int[]) hiddenColumns.elementAt(i);
+        int[] region = hiddenColumns.elementAt(i);
         if (region[0] > start && start + change > region[1])
         {
-          deletedHiddenColumns.addElement(hiddenColumns.elementAt(i));
+          deletedHiddenColumns.add(region);
 
           hiddenColumns.removeElementAt(i);
           i--;
@@ -258,7 +263,7 @@ public class ColumnSelection
     {
       for (int i = 0; i < hiddenColumns.size(); i++)
       {
-        int[] region = (int[]) hiddenColumns.elementAt(i);
+        int[] region = hiddenColumns.elementAt(i);
         if (region[0] >= start)
         {
           region[0] -= change;
@@ -479,7 +484,7 @@ public class ColumnSelection
    *          int
    * @return Vector
    */
-  public Vector getHiddenColumns()
+  public List<int[]> getHiddenColumns()
   {
     return hiddenColumns;
   }
@@ -498,7 +503,7 @@ public class ColumnSelection
     {
       for (int i = 0; i < hiddenColumns.size(); i++)
       {
-        int[] region = (int[]) hiddenColumns.elementAt(i);
+        int[] region = hiddenColumns.elementAt(i);
         if (result >= region[0])
         {
           result += region[1] - region[0] + 1;
@@ -526,7 +531,7 @@ public class ColumnSelection
       int[] region;
       do
       {
-        region = (int[]) hiddenColumns.elementAt(index++);
+        region = hiddenColumns.elementAt(index++);
         if (hiddenColumn > region[1])
         {
           result -= region[1] + 1 - region[0];
@@ -552,7 +557,7 @@ public class ColumnSelection
       int gaps = 0;
       do
       {
-        int[] region = (int[]) hiddenColumns.elementAt(index);
+        int[] region = hiddenColumns.elementAt(index);
         if (hiddenRegion == 0)
         {
           return region[0];
@@ -583,7 +588,7 @@ public class ColumnSelection
       int index = 0;
       do
       {
-        int[] region = (int[]) hiddenColumns.elementAt(index);
+        int[] region = hiddenColumns.elementAt(index);
         if (alPos < region[0])
         {
           return region[0];
@@ -611,7 +616,7 @@ public class ColumnSelection
       int index = hiddenColumns.size() - 1;
       do
       {
-        int[] region = (int[]) hiddenColumns.elementAt(index);
+        int[] region = hiddenColumns.elementAt(index);
         if (alPos > region[1])
         {
           return region[1];
@@ -647,7 +652,7 @@ public class ColumnSelection
 
     for (int i = 0; i < hiddenColumns.size(); i++)
     {
-      int[] region = (int[]) hiddenColumns.elementAt(i);
+      int[] region = hiddenColumns.elementAt(i);
       if (start <= region[1] && end >= region[0])
       {
         hiddenColumns.removeElementAt(i);
@@ -714,7 +719,7 @@ public class ColumnSelection
     {
       for (int i = 0; i < hiddenColumns.size(); i++)
       {
-        int[] region = (int[]) hiddenColumns.elementAt(i);
+        int[] region = hiddenColumns.elementAt(i);
         for (int j = region[0]; j < region[1] + 1; j++)
         {
           addElement(j);
@@ -729,7 +734,7 @@ public class ColumnSelection
   {
     for (int i = 0; i < hiddenColumns.size(); i++)
     {
-      int[] region = (int[]) hiddenColumns.elementAt(i);
+      int[] region = hiddenColumns.elementAt(i);
       if (res == region[0])
       {
         for (int j = region[0]; j < region[1] + 1; j++)
@@ -750,14 +755,16 @@ public class ColumnSelection
   public boolean isVisible(int column)
   {
     if (hiddenColumns != null)
+    {
       for (int i = 0; i < hiddenColumns.size(); i++)
       {
-        int[] region = (int[]) hiddenColumns.elementAt(i);
+        int[] region = hiddenColumns.elementAt(i);
         if (column >= region[0] && column <= region[1])
         {
           return false;
         }
       }
+    }
 
     return true;
   }
@@ -785,7 +792,7 @@ public class ColumnSelection
         for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++)
         {
           int[] rh, cp;
-          rh = (int[]) copy.hiddenColumns.elementAt(i);
+          rh = copy.hiddenColumns.elementAt(i);
           if (rh != null)
           {
             cp = new int[rh.length];
@@ -814,7 +821,7 @@ public class ColumnSelection
       for (i = 0; i < iSize; i++)
       {
         StringBuffer visibleSeq = new StringBuffer();
-        Vector regions = getHiddenColumns();
+        List<int[]> regions = getHiddenColumns();
 
         int blockStart = start, blockEnd = end;
         int[] region;
@@ -822,7 +829,7 @@ public class ColumnSelection
 
         for (int j = 0; j < regions.size(); j++)
         {
-          region = (int[]) regions.elementAt(j);
+          region = regions.get(j);
           hideStart = region[0];
           hideEnd = region[1];
 
@@ -879,7 +886,7 @@ public class ColumnSelection
     if (hiddenColumns != null && hiddenColumns.size() > 0)
     {
       Vector visiblecontigs = new Vector();
-      Vector regions = getHiddenColumns();
+      List<int[]> regions = getHiddenColumns();
 
       int vstart = start;
       int[] region;
@@ -887,7 +894,7 @@ public class ColumnSelection
 
       for (int j = 0; vstart < end && j < regions.size(); j++)
       {
-        region = (int[]) regions.elementAt(j);
+        region = regions.get(j);
         hideStart = region[0];
         hideEnd = region[1];
 
@@ -965,14 +972,14 @@ public class ColumnSelection
       // then mangle the alignmentAnnotation annotation array
       Vector annels = new Vector();
       Annotation[] els = null;
-      Vector regions = getHiddenColumns();
+      List<int[]> regions = getHiddenColumns();
       int blockStart = start, blockEnd = end;
       int[] region;
       int hideStart, hideEnd, w = 0;
 
       for (int j = 0; j < regions.size(); j++)
       {
-        region = (int[]) regions.elementAt(j);
+        region = regions.get(j);
         hideStart = region[0];
         hideEnd = region[1];
 
@@ -1016,7 +1023,9 @@ public class ColumnSelection
         w += els.length;
       }
       if (w == 0)
+      {
         return;
+      }
       Enumeration e = annels.elements();
       alignmentAnnotation.annotations = new Annotation[w];
       w = 0;
@@ -1256,4 +1265,37 @@ public class ColumnSelection
       }
     }
   }
+
+  /**
+   * 
+   * @return true if there are columns marked
+   */
+  public boolean hasSelectedColumns()
+  {
+    return (selected != null && selected.size() > 0);
+  }
+
+  /**
+   * 
+   * @return true if there are columns hidden
+   */
+  public boolean hasHiddenColumns()
+  {
+    return hiddenColumns != null && hiddenColumns.size() > 0;
+  }
+
+  /**
+   * mark the columns corresponding to gap characters as hidden in the column
+   * selection
+   * 
+   * @param sr
+   */
+  public void hideInsertionsFor(SequenceI sr)
+  {
+    List<int[]> inserts = sr.getInsertions();
+    for (int[] r : inserts)
+    {
+      hideColumns(r[0], r[1]);
+    }
+  }
 }