apply jalview code style
[jalview.git] / src / jalview / datamodel / ColumnSelection.java
index 9dbaced..a36faf7 100644 (file)
@@ -1,20 +1,19 @@
 /*\r
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)\r
- * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)\r
+ * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle\r
  * \r
- * This program is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU General Public License\r
- * as published by the Free Software Foundation; either version 2\r
- * of the License, or (at your option) any later version.\r
+ * This file is part of Jalview.\r
  * \r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
+ * Jalview is free software: you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License \r
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\r
  * \r
- * You should have received a copy of the GNU General Public License\r
- * along with this program; if not, write to the Free Software\r
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
+ * Jalview is distributed in the hope that it will be useful, but \r
+ * WITHOUT ANY WARRANTY; without even the implied warranty \r
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR \r
+ * PURPOSE.  See the GNU General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.\r
  */\r
 package jalview.datamodel;\r
 \r
@@ -36,7 +35,7 @@ public class ColumnSelection
    * Add a column to the selection\r
    * \r
    * @param col\r
-   *                index of column\r
+   *          index of column\r
    */\r
   public void addElement(int col)\r
   {\r
@@ -59,7 +58,7 @@ public class ColumnSelection
    * removes col from selection\r
    * \r
    * @param col\r
-   *                index of column to be removed\r
+   *          index of column to be removed\r
    */\r
   public void removeElement(int col)\r
   {\r
@@ -75,9 +74,9 @@ public class ColumnSelection
    * removes a range of columns from the selection\r
    * \r
    * @param start\r
-   *                int - first column in range to be removed\r
+   *          int - first column in range to be removed\r
    * @param end\r
-   *                int - last col\r
+   *          int - last col\r
    */\r
   public void removeElements(int start, int end)\r
   {\r
@@ -104,7 +103,7 @@ public class ColumnSelection
   /**\r
    * \r
    * @param col\r
-   *                index to search for in column selection\r
+   *          index to search for in column selection\r
    * \r
    * @return true if Integer(col) is in selection.\r
    */\r
@@ -117,7 +116,7 @@ public class ColumnSelection
    * Column number at position i in selection\r
    * \r
    * @param i\r
-   *                index into selected columns\r
+   *          index into selected columns\r
    * \r
    * @return column number in alignment\r
    */\r
@@ -180,9 +179,9 @@ public class ColumnSelection
    * propagate shift in alignment columns to column selection\r
    * \r
    * @param start\r
-   *                beginning of edit\r
+   *          beginning of edit\r
    * @param left\r
-   *                shift in edit (+ve for removal, or -ve for inserts)\r
+   *          shift in edit (+ve for removal, or -ve for inserts)\r
    */\r
   public Vector compensateForEdit(int start, int change)\r
   {\r
@@ -238,9 +237,9 @@ public class ColumnSelection
    * compensateForEdit - allowing for edits within hidden regions\r
    * \r
    * @param start\r
-   *                beginning of edit\r
+   *          beginning of edit\r
    * @param left\r
-   *                shift in edit (+ve for removal, or -ve for inserts)\r
+   *          shift in edit (+ve for removal, or -ve for inserts)\r
    */\r
   private void compensateForDelEdits(int start, int change)\r
   {\r
@@ -403,7 +402,7 @@ public class ColumnSelection
       }\r
     }\r
     return pruned; // true if any interval was removed or modified by\r
-                    // operations.\r
+    // operations.\r
   }\r
 \r
   private boolean pruneColumnList(Vector deletion, Vector list)\r
@@ -476,7 +475,7 @@ public class ColumnSelection
    * given index.\r
    * \r
    * @param end\r
-   *                int\r
+   *          int\r
    * @return Vector\r
    */\r
   public Vector getHiddenColumns()\r
@@ -488,7 +487,7 @@ public class ColumnSelection
    * Return absolute column index for a visible column index\r
    * \r
    * @param column\r
-   *                int column index in alignment view\r
+   *          int column index in alignment view\r
    * @return alignment column index for column\r
    */\r
   public int adjustForHiddenColumns(int column)\r
@@ -509,11 +508,12 @@ public class ColumnSelection
   }\r
 \r
   /**\r
-   * Use this method to find out where a visible column is in the alignment when\r
-   * hidden columns exist\r
+   * Use this method to find out where a column will appear in the visible\r
+   * alignment when hidden columns exist. If the column is not visible, then the\r
+   * left-most visible column will always be returned.\r
    * \r
    * @param hiddenColumn\r
-   *                int\r
+   *          int\r
    * @return int\r
    */\r
   public int findColumnPosition(int hiddenColumn)\r
@@ -522,21 +522,21 @@ public class ColumnSelection
     if (hiddenColumns != null)\r
     {\r
       int index = 0;\r
-      int gaps = 0;\r
+      int[] region;\r
       do\r
       {\r
-        int[] region = (int[]) hiddenColumns.elementAt(index);\r
+        region = (int[]) hiddenColumns.elementAt(index++);\r
         if (hiddenColumn > region[1])\r
         {\r
           result -= region[1] + 1 - region[0];\r
         }\r
-        index++;\r
-      } while (index < hiddenColumns.size());\r
-\r
-      result -= gaps;\r
+      } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size()));\r
+      if (hiddenColumn > region[0] && hiddenColumn < region[1])\r
+      {\r
+        return region[0] + hiddenColumn - result;\r
+      }\r
     }\r
-\r
-    return result;\r
+    return result; // return the shifted position after removing hidden columns.\r
   }\r
 \r
   /**\r
@@ -573,7 +573,7 @@ public class ColumnSelection
    * hidden columns. In otherwords, the next hidden column.\r
    * \r
    * @param index\r
-   *                int\r
+   *          int\r
    */\r
   public int getHiddenBoundaryRight(int alPos)\r
   {\r
@@ -601,7 +601,7 @@ public class ColumnSelection
    * hidden columns. In otherwords, the previous hidden column.\r
    * \r
    * @param index\r
-   *                int\r
+   *          int\r
    */\r
   public int getHiddenBoundaryLeft(int alPos)\r
   {\r
@@ -679,7 +679,7 @@ public class ColumnSelection
    * specified\r
    * \r
    * @param res\r
-   *                int\r
+   *          int\r
    */\r
   public void hideColumns(int col)\r
   {\r
@@ -867,10 +867,11 @@ public class ColumnSelection
    * return all visible segments between the given start and end boundaries\r
    * \r
    * @param start\r
-   *                (first column inclusive from 0)\r
+   *          (first column inclusive from 0)\r
    * @param end\r
-   *                (last column - not inclusive)\r
-   * @return int[] {i_start, i_end, ..} where intervals lie in start<=i_start<=i_end<end\r
+   *          (last column - not inclusive)\r
+   * @return int[] {i_start, i_end, ..} where intervals lie in\r
+   *         start<=i_start<=i_end<end\r
    */\r
   public int[] getVisibleContigs(int start, int end)\r
   {\r
@@ -940,11 +941,11 @@ public class ColumnSelection
    * sequence associated annotation).\r
    * \r
    * @param start\r
-   *                remove any annotation to the right of this column\r
+   *          remove any annotation to the right of this column\r
    * @param end\r
-   *                remove any annotation to the left of this column\r
+   *          remove any annotation to the left of this column\r
    * @param alignmentAnnotation\r
-   *                the annotation to operate on\r
+   *          the annotation to operate on\r
    */\r
   public void makeVisibleAnnotation(int start, int end,\r
           AlignmentAnnotation alignmentAnnotation)\r
@@ -998,7 +999,7 @@ public class ColumnSelection
       if (end > blockStart)\r
       {\r
         annels.addElement(els = new Annotation[end - blockStart + 1]);\r
-        if ((els.length+blockStart)<= alignmentAnnotation.annotations.length)\r
+        if ((els.length + blockStart) <= alignmentAnnotation.annotations.length)\r
         {\r
           // copy just the visible segment of the annotation row\r
           System.arraycopy(alignmentAnnotation.annotations, blockStart,\r
@@ -1008,7 +1009,8 @@ public class ColumnSelection
         {\r
           // copy to the end of the annotation row\r
           System.arraycopy(alignmentAnnotation.annotations, blockStart,\r
-                  els, 0, (alignmentAnnotation.annotations.length - blockStart));\r
+                  els, 0,\r
+                  (alignmentAnnotation.annotations.length - blockStart));\r
         }\r
         w += els.length;\r
       }\r
@@ -1056,4 +1058,58 @@ public class ColumnSelection
       }\r
     }\r
   }\r
+\r
+  /**\r
+   * add in any unselected columns from the given column selection, excluding\r
+   * any that are hidden.\r
+   * \r
+   * @param colsel\r
+   */\r
+  public void addElementsFrom(ColumnSelection colsel)\r
+  {\r
+    if (colsel != null && colsel.size() > 0)\r
+    {\r
+      Enumeration e = colsel.getSelected().elements();\r
+      while (e.hasMoreElements())\r
+      {\r
+        Object eo = e.nextElement();\r
+        if (hiddenColumns != null && isVisible(((Integer) eo).intValue()))\r
+        {\r
+          if (!selected.contains(eo))\r
+          {\r
+            selected.addElement(eo);\r
+          }\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  /**\r
+   * set the selected columns the given column selection, excluding any columns\r
+   * that are hidden.\r
+   * \r
+   * @param colsel\r
+   */\r
+  public void setElementsFrom(ColumnSelection colsel)\r
+  {\r
+    selected = new Vector();\r
+    if (colsel.selected != null && colsel.selected.size() > 0)\r
+    {\r
+      if (hiddenColumns != null && hiddenColumns.size() > 0)\r
+      {\r
+        // only select visible columns in this columns selection\r
+        selected = new Vector();\r
+        addElementsFrom(colsel);\r
+      }\r
+      else\r
+      {\r
+        // add everything regardless\r
+        Enumeration en = colsel.selected.elements();\r
+        while (en.hasMoreElements())\r
+        {\r
+          selected.addElement(en.nextElement());\r
+        }\r
+      }\r
+    }\r
+  }\r
 }\r