--- /dev/null
+/*\r
+ * Jalview - A Sequence Alignment Editor and Viewer\r
+ * Copyright (C) 2005 AM Waterhouse, J Procter, 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
+ *\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
+ *\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
+ */\r
+package jalview.datamodel;\r
+\r
+import java.util.*;\r
+\r
+/**\r
+ * NOTE: Columns are zero based.\r
+ */\r
+public class ColumnSelection\r
+{\r
+ Vector selected = new Vector();\r
+\r
+ //Vector of int [] {startCol, endCol}\r
+ Vector hiddenColumns;\r
+\r
+ /**\r
+ * DOCUMENT ME!\r
+ *\r
+ * @param col DOCUMENT ME!\r
+ */\r
+ public void addElement(int col)\r
+ {\r
+ if (!selected.contains(new Integer(col)))\r
+ {\r
+ selected.addElement(new Integer(col));\r
+ }\r
+ }\r
+\r
+ /**\r
+ * DOCUMENT ME!\r
+ */\r
+ public void clear()\r
+ {\r
+ selected.removeAllElements();\r
+ }\r
+\r
+ /**\r
+ * DOCUMENT ME!\r
+ *\r
+ * @param col DOCUMENT ME!\r
+ */\r
+ public void removeElement(int col)\r
+ {\r
+ Integer colInt = new Integer(col);\r
+\r
+ if (selected.contains(colInt))\r
+ {\r
+ selected.removeElement(colInt);\r
+ }\r
+ }\r
+\r
+ public void removeElements(int start, int end)\r
+ {\r
+ Integer colInt;\r
+ for(int i=start; i<end; i++)\r
+ {\r
+ colInt = new Integer(i);\r
+ if (selected.contains(colInt))\r
+ {\r
+ selected.removeElement(colInt);\r
+ }\r
+ }\r
+ }\r
+\r
+ public Vector getSelected()\r
+ {\r
+ return selected;\r
+ }\r
+\r
+ /**\r
+ * DOCUMENT ME!\r
+ *\r
+ * @param col DOCUMENT ME!\r
+ *\r
+ * @return DOCUMENT ME!\r
+ */\r
+ public boolean contains(int col)\r
+ {\r
+ return selected.contains(new Integer(col));\r
+ }\r
+\r
+ /**\r
+ * DOCUMENT ME!\r
+ *\r
+ * @param i DOCUMENT ME!\r
+ *\r
+ * @return DOCUMENT ME!\r
+ */\r
+ public int columnAt(int i)\r
+ {\r
+ return ((Integer) selected.elementAt(i)).intValue();\r
+ }\r
+\r
+ /**\r
+ * DOCUMENT ME!\r
+ *\r
+ * @return DOCUMENT ME!\r
+ */\r
+ public int size()\r
+ {\r
+ return selected.size();\r
+ }\r
+\r
+ /**\r
+ * DOCUMENT ME!\r
+ *\r
+ * @return DOCUMENT ME!\r
+ */\r
+ public int getMax()\r
+ {\r
+ int max = -1;\r
+\r
+ for (int i = 0; i < selected.size(); i++)\r
+ {\r
+ if (columnAt(i) > max)\r
+ {\r
+ max = columnAt(i);\r
+ }\r
+ }\r
+\r
+ return max;\r
+ }\r
+\r
+ /**\r
+ * DOCUMENT ME!\r
+ *\r
+ * @return DOCUMENT ME!\r
+ */\r
+ public int getMin()\r
+ {\r
+ int min = 1000000000;\r
+\r
+ for (int i = 0; i < selected.size(); i++)\r
+ {\r
+ if (columnAt(i) < min)\r
+ {\r
+ min = columnAt(i);\r
+ }\r
+ }\r
+\r
+ return min;\r
+ }\r
+\r
+\r
+ /**\r
+ * DOCUMENT ME!\r
+ *\r
+ * @param start DOCUMENT ME!\r
+ * @param change DOCUMENT ME!\r
+ */\r
+ public void compensateForEdit(int start, int change)\r
+ {\r
+ for (int i = 0; i < size(); i++)\r
+ {\r
+ int temp = columnAt(i);\r
+\r
+ if (temp >= start)\r
+ {\r
+ selected.setElementAt(new Integer(temp - change), i);\r
+ }\r
+ }\r
+\r
+ if(hiddenColumns!=null)\r
+ {\r
+ for(int i=0; i<hiddenColumns.size(); i++)\r
+ {\r
+ int[] region = (int[]) hiddenColumns.elementAt(i);\r
+ if(region[0] > start)\r
+ {\r
+ region[0] -= change;\r
+ region[1] -= change;\r
+ }\r
+ if(region[0]<0)\r
+ region[0] = 0;\r
+ if(region[1] <0)\r
+ region[1] = 0;\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * This Method is used to return all the HiddenColumn regions\r
+ * less than the given index.\r
+ * @param end int\r
+ * @return Vector\r
+ */\r
+ public Vector getHiddenColumns()\r
+ {\r
+ return hiddenColumns;\r
+ }\r
+\r
+ public int adjustForHiddenColumns(int column)\r
+ {\r
+ int result = column;\r
+ if (hiddenColumns != null)\r
+ {\r
+ for (int i = 0; i < hiddenColumns.size(); i++)\r
+ {\r
+ int[] region = (int[]) hiddenColumns.elementAt(i);\r
+ if (result >= region[0])\r
+ {\r
+ result += region[1] - region[0] + 1;\r
+ }\r
+ }\r
+ }\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * Use this method to find out where a visible column is in the alignment\r
+ * when hidden columns exist\r
+ * @param hiddenColumn int\r
+ * @return int\r
+ */\r
+ public int findColumnPosition(int hiddenColumn)\r
+ {\r
+ int result = hiddenColumn;\r
+ if (hiddenColumns != null)\r
+ {\r
+ int index = 0;\r
+ int gaps = 0;\r
+ do\r
+ {\r
+ int[] region = (int[]) hiddenColumns.elementAt(index);\r
+ if (hiddenColumn > region[1])\r
+ {\r
+ result -= region[1]+1-region[0];\r
+ }\r
+ index++;\r
+ }\r
+ while (index < hiddenColumns.size());\r
+\r
+ result -= gaps;\r
+ }\r
+\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * Use this method to determine where the next hiddenRegion starts\r
+ */\r
+ public int findHiddenRegionPosition(int hiddenRegion)\r
+ {\r
+ int result = 0;\r
+ if (hiddenColumns != null)\r
+ {\r
+ int index = 0;\r
+ int gaps = 0;\r
+ do\r
+ {\r
+ int[] region = (int[]) hiddenColumns.elementAt(index);\r
+ if(hiddenRegion==0)\r
+ {\r
+ return region[0];\r
+ }\r
+\r
+ gaps += region[1] +1 - region[0];\r
+ result = region[1] +1;\r
+ index++;\r
+ }\r
+ while(index < hiddenRegion+1);\r
+\r
+ result -= gaps;\r
+ }\r
+\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * THis method returns the rightmost limit of a\r
+ * region of an alignment with hidden columns.\r
+ * In otherwords, the next hidden column.\r
+ * @param index int\r
+ */\r
+ public int getHiddenBoundaryRight(int alPos)\r
+ {\r
+ if (hiddenColumns != null)\r
+ {\r
+ int index = 0;\r
+ do\r
+ {\r
+ int[] region = (int[]) hiddenColumns.elementAt(index);\r
+ if(alPos < region[0])\r
+ return region[0];\r
+\r
+ index++;\r
+ }\r
+ while(index < hiddenColumns.size());\r
+ }\r
+\r
+ return alPos;\r
+\r
+ }\r
+ /**\r
+ * THis method returns the rightmost limit of a\r
+ * region of an alignment with hidden columns.\r
+ * In otherwords, the next hidden column.\r
+ * @param index int\r
+ */\r
+ public int getHiddenBoundaryLeft(int alPos)\r
+ {\r
+ if (hiddenColumns != null)\r
+ {\r
+ int index = hiddenColumns.size()-1;\r
+ do\r
+ {\r
+ int[] region = (int[]) hiddenColumns.elementAt(index);\r
+ if(alPos > region[1])\r
+ return region[1];\r
+\r
+ index--;\r
+ }\r
+ while(index >-1);\r
+ }\r
+\r
+ return alPos;\r
+\r
+ }\r
+\r
+\r
+\r
+ public void hideColumns(int res)\r
+ {\r
+ if(hiddenColumns==null)\r
+ hiddenColumns = new Vector();\r
+\r
+ // First find out range of columns to hide\r
+ int min = res, max = res+1;\r
+ while( contains(min) )\r
+ { removeElement(min); min --; }\r
+\r
+ while( contains(max) )\r
+ { removeElement(max); max ++; }\r
+\r
+ min++; max--;\r
+\r
+ boolean added = false;\r
+ for(int i=0; i<hiddenColumns.size(); i++)\r
+ {\r
+ int [] region = (int[])hiddenColumns.elementAt(i);\r
+ if( max < region[0])\r
+ {\r
+ hiddenColumns.insertElementAt(new int[]{min, max}, i);\r
+ added = true;\r
+ break;\r
+ }\r
+ }\r
+\r
+ if(!added)\r
+ hiddenColumns.addElement(new int[]{min, max});\r
+\r
+ }\r
+\r
+ public void revealAllHiddenColumns()\r
+ {\r
+ if(hiddenColumns!=null)\r
+ {\r
+ for (int i = 0; i < hiddenColumns.size(); i++)\r
+ {\r
+ int[] region = (int[]) hiddenColumns.elementAt(i);\r
+ for (int j = region[0]; j < region[1]; j++)\r
+ {\r
+ addElement(j);\r
+ }\r
+ }\r
+ }\r
+\r
+ hiddenColumns = null;\r
+ }\r
+\r
+ public void revealHiddenColumns(int res)\r
+ {\r
+ for(int i=0; i<hiddenColumns.size(); i++)\r
+ {\r
+ int [] region = (int[])hiddenColumns.elementAt(i);\r
+ if( res == region[0])\r
+ {\r
+ for (int j = region[0]; j < region[1]; j++)\r
+ {\r
+ addElement(j);\r
+ }\r
+\r
+ hiddenColumns.removeElement(region);\r
+ break;\r
+ }\r
+ }\r
+ }\r
+\r
+ public boolean isVisible(int column)\r
+ {\r
+ for(int i=0; i<hiddenColumns.size(); i++)\r
+ {\r
+ int [] region = (int[])hiddenColumns.elementAt(i);\r
+ if( column >= region[0] && column <= region[1])\r
+ {\r
+ return false;\r
+ }\r
+ }\r
+ return true;\r
+ }\r
+\r
+}\r