2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.datamodel;
24 * NOTE: Columns are zero based.
26 public class ColumnSelection
28 Vector selected = new Vector();
30 //Vector of int [] {startCol, endCol}
34 * Add a column to the selection
36 * @param col index of column
38 public void addElement(int col)
40 Integer column = new Integer(col);
41 if (!selected.contains(column))
43 selected.addElement(column);
48 * clears column selection
52 selected.removeAllElements();
56 * removes col from selection
58 * @param col index of column to be removed
60 public void removeElement(int col)
62 Integer colInt = new Integer(col);
64 if (selected.contains(colInt))
66 selected.removeElement(colInt);
71 * removes a range of columns from the selection
72 * @param start int - first column in range to be removed
73 * @param end int - last col
75 public void removeElements(int start, int end)
78 for(int i=start; i<end; i++)
80 colInt = new Integer(i);
81 if (selected.contains(colInt))
83 selected.removeElement(colInt);
89 * @return Vector containing selected columns as Integers
91 public Vector getSelected()
98 * @param col index to search for in column selection
100 * @return true if Integer(col) is in selection.
102 public boolean contains(int col)
104 return selected.contains(new Integer(col));
110 * @param i DOCUMENT ME!
112 * @return DOCUMENT ME!
114 public int columnAt(int i)
116 return ((Integer) selected.elementAt(i)).intValue();
122 * @return DOCUMENT ME!
126 return selected.size();
132 * @return DOCUMENT ME!
138 for (int i = 0; i < selected.size(); i++)
140 if (columnAt(i) > max)
152 * @return DOCUMENT ME!
156 int min = 1000000000;
158 for (int i = 0; i < selected.size(); i++)
160 if (columnAt(i) < min)
173 * @param start DOCUMENT ME!
174 * @param change DOCUMENT ME!
176 public void compensateForEdit(int start, int change)
178 for (int i = 0; i < size(); i++)
180 int temp = columnAt(i);
184 selected.setElementAt(new Integer(temp - change), i);
188 if(hiddenColumns!=null)
190 for(int i=0; i<hiddenColumns.size(); i++)
192 int[] region = (int[]) hiddenColumns.elementAt(i);
193 if(region[0] > start)
207 * This Method is used to return all the HiddenColumn regions
208 * less than the given index.
212 public Vector getHiddenColumns()
214 return hiddenColumns;
217 * Return absolute column index for a visible column index
218 * @param column int column index in alignment view
219 * @return alignment column index for column
221 public int adjustForHiddenColumns(int column)
224 if (hiddenColumns != null)
226 for (int i = 0; i < hiddenColumns.size(); i++)
228 int[] region = (int[]) hiddenColumns.elementAt(i);
229 if (result >= region[0])
231 result += region[1] - region[0] + 1;
239 * Use this method to find out where a visible column is in the alignment
240 * when hidden columns exist
241 * @param hiddenColumn int
244 public int findColumnPosition(int hiddenColumn)
246 int result = hiddenColumn;
247 if (hiddenColumns != null)
253 int[] region = (int[]) hiddenColumns.elementAt(index);
254 if (hiddenColumn > region[1])
256 result -= region[1]+1-region[0];
260 while (index < hiddenColumns.size());
269 * Use this method to determine where the next hiddenRegion starts
271 public int findHiddenRegionPosition(int hiddenRegion)
274 if (hiddenColumns != null)
280 int[] region = (int[]) hiddenColumns.elementAt(index);
286 gaps += region[1] +1 - region[0];
287 result = region[1] +1;
290 while(index < hiddenRegion+1);
299 * THis method returns the rightmost limit of a
300 * region of an alignment with hidden columns.
301 * In otherwords, the next hidden column.
304 public int getHiddenBoundaryRight(int alPos)
306 if (hiddenColumns != null)
311 int[] region = (int[]) hiddenColumns.elementAt(index);
312 if(alPos < region[0])
317 while(index < hiddenColumns.size());
324 * THis method returns the rightmost limit of a
325 * region of an alignment with hidden columns.
326 * In otherwords, the next hidden column.
329 public int getHiddenBoundaryLeft(int alPos)
331 if (hiddenColumns != null)
333 int index = hiddenColumns.size()-1;
336 int[] region = (int[]) hiddenColumns.elementAt(index);
337 if(alPos > region[1])
349 public void hideSelectedColumns()
353 int column = ( (Integer) getSelected().firstElement()).intValue();
359 public void hideColumns(int start, int end)
361 if(hiddenColumns==null)
362 hiddenColumns = new Vector();
364 boolean added = false;
365 boolean overlap = false;
367 for (int i = 0; i < hiddenColumns.size(); i++)
369 int[] region = (int[]) hiddenColumns.elementAt(i);
370 if ( start<=region[1] && end>=region[0])
372 hiddenColumns.removeElementAt(i);
376 else if (end < region[0] && start < region[0])
378 hiddenColumns.insertElementAt(new int[]
387 hideColumns(start, end);
390 hiddenColumns.addElement(new int[] {start, end});
395 * This method will find a range of selected columns
396 * around the column specified
399 public void hideColumns(int col)
401 // First find out range of columns to hide
402 int min = col, max = col+1;
403 while( contains(min) )
404 { removeElement(min); min --; }
406 while( contains(max) )
407 { removeElement(max); max ++; }
411 hideColumns(min, max);
414 public void revealAllHiddenColumns()
416 if(hiddenColumns!=null)
418 for (int i = 0; i < hiddenColumns.size(); i++)
420 int[] region = (int[]) hiddenColumns.elementAt(i);
421 for (int j = region[0]; j < region[1]; j++)
428 hiddenColumns = null;
431 public void revealHiddenColumns(int res)
433 for(int i=0; i<hiddenColumns.size(); i++)
435 int [] region = (int[])hiddenColumns.elementAt(i);
436 if( res == region[0])
438 for (int j = region[0]; j < region[1]; j++)
443 hiddenColumns.removeElement(region);
447 if(hiddenColumns.size()==0)
448 hiddenColumns = null;
451 public boolean isVisible(int column)
453 for(int i=0; i<hiddenColumns.size(); i++)
455 int [] region = (int[])hiddenColumns.elementAt(i);
456 if( column >= region[0] && column <= region[1])