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;
21 import jalview.util.ShiftList;
26 * NOTE: Columns are zero based.
28 public class ColumnSelection
30 Vector selected = new Vector();
32 //Vector of int [] {startCol, endCol}
36 * Add a column to the selection
38 * @param col index of column
40 public void addElement(int col)
42 Integer column = new Integer(col);
43 if (!selected.contains(column))
45 selected.addElement(column);
50 * clears column selection
54 selected.removeAllElements();
58 * removes col from selection
60 * @param col index of column to be removed
62 public void removeElement(int col)
64 Integer colInt = new Integer(col);
66 if (selected.contains(colInt))
68 selected.removeElement(colInt);
73 * removes a range of columns from the selection
74 * @param start int - first column in range to be removed
75 * @param end int - last col
77 public void removeElements(int start, int end)
80 for(int i=start; i<end; i++)
82 colInt = new Integer(i);
83 if (selected.contains(colInt))
85 selected.removeElement(colInt);
91 * @return Vector containing selected columns as Integers
93 public Vector getSelected()
100 * @param col index to search for in column selection
102 * @return true if Integer(col) is in selection.
104 public boolean contains(int col)
106 return selected.contains(new Integer(col));
112 * @param i DOCUMENT ME!
114 * @return DOCUMENT ME!
116 public int columnAt(int i)
118 return ((Integer) selected.elementAt(i)).intValue();
124 * @return DOCUMENT ME!
128 return selected.size();
134 * @return DOCUMENT ME!
140 for (int i = 0; i < selected.size(); i++)
142 if (columnAt(i) > max)
154 * @return DOCUMENT ME!
158 int min = 1000000000;
160 for (int i = 0; i < selected.size(); i++)
162 if (columnAt(i) < min)
173 * propagate shift in alignment columns to column selection
175 * @param start beginning of edit
176 * @param change shift in edit (-ve or +ve number of columns)
178 public void compensateForEdit(int start, int change)
180 for (int i = 0; i < size(); i++)
182 int temp = columnAt(i);
186 selected.setElementAt(new Integer(temp - change), i);
190 if(hiddenColumns!=null)
192 for(int i=0; i<hiddenColumns.size(); i++)
194 int[] region = (int[]) hiddenColumns.elementAt(i);
195 if(region[0] > start)
207 public ShiftList compensateForEdits(ShiftList shiftrecord) {
208 if (shiftrecord!=null) {
209 Vector shifts = shiftrecord.shifts;
210 if (shifts!=null && shifts.size()>0) {
211 for (int i=0,j=shifts.size(); i<j; i++) {
212 int[] sh = (int[]) shifts.get(i);
213 compensateForEdit(sh[0], sh[1]);
216 return shiftrecord.getInverse();
221 * This Method is used to return all the HiddenColumn regions
222 * less than the given index.
226 public Vector getHiddenColumns()
228 return hiddenColumns;
231 * Return absolute column index for a visible column index
232 * @param column int column index in alignment view
233 * @return alignment column index for column
235 public int adjustForHiddenColumns(int column)
238 if (hiddenColumns != null)
240 for (int i = 0; i < hiddenColumns.size(); i++)
242 int[] region = (int[]) hiddenColumns.elementAt(i);
243 if (result >= region[0])
245 result += region[1] - region[0] + 1;
253 * Use this method to find out where a visible column is in the alignment
254 * when hidden columns exist
255 * @param hiddenColumn int
258 public int findColumnPosition(int hiddenColumn)
260 int result = hiddenColumn;
261 if (hiddenColumns != null)
267 int[] region = (int[]) hiddenColumns.elementAt(index);
268 if (hiddenColumn > region[1])
270 result -= region[1]+1-region[0];
274 while (index < hiddenColumns.size());
283 * Use this method to determine where the next hiddenRegion starts
285 public int findHiddenRegionPosition(int hiddenRegion)
288 if (hiddenColumns != null)
294 int[] region = (int[]) hiddenColumns.elementAt(index);
300 gaps += region[1] +1 - region[0];
301 result = region[1] +1;
304 while(index < hiddenRegion+1);
313 * THis method returns the rightmost limit of a
314 * region of an alignment with hidden columns.
315 * In otherwords, the next hidden column.
318 public int getHiddenBoundaryRight(int alPos)
320 if (hiddenColumns != null)
325 int[] region = (int[]) hiddenColumns.elementAt(index);
326 if(alPos < region[0])
331 while(index < hiddenColumns.size());
338 * THis method returns the rightmost limit of a
339 * region of an alignment with hidden columns.
340 * In otherwords, the next hidden column.
343 public int getHiddenBoundaryLeft(int alPos)
345 if (hiddenColumns != null)
347 int index = hiddenColumns.size()-1;
350 int[] region = (int[]) hiddenColumns.elementAt(index);
351 if(alPos > region[1])
363 public void hideSelectedColumns()
367 int column = ( (Integer) getSelected().firstElement()).intValue();
373 public void hideColumns(int start, int end)
375 if(hiddenColumns==null)
376 hiddenColumns = new Vector();
378 boolean added = false;
379 boolean overlap = false;
381 for (int i = 0; i < hiddenColumns.size(); i++)
383 int[] region = (int[]) hiddenColumns.elementAt(i);
384 if ( start<=region[1] && end>=region[0])
386 hiddenColumns.removeElementAt(i);
390 else if (end < region[0] && start < region[0])
392 hiddenColumns.insertElementAt(new int[]
401 hideColumns(start, end);
404 hiddenColumns.addElement(new int[] {start, end});
409 * This method will find a range of selected columns
410 * around the column specified
413 public void hideColumns(int col)
415 // First find out range of columns to hide
416 int min = col, max = col+1;
417 while( contains(min) )
418 { removeElement(min); min --; }
420 while( contains(max) )
421 { removeElement(max); max ++; }
425 hideColumns(min, max);
428 public void revealAllHiddenColumns()
430 if(hiddenColumns!=null)
432 for (int i = 0; i < hiddenColumns.size(); i++)
434 int[] region = (int[]) hiddenColumns.elementAt(i);
435 for (int j = region[0]; j < region[1]+1; j++)
442 hiddenColumns = null;
445 public void revealHiddenColumns(int res)
447 for(int i=0; i<hiddenColumns.size(); i++)
449 int [] region = (int[])hiddenColumns.elementAt(i);
450 if( res == region[0])
452 for (int j = region[0]; j < region[1]+1; j++)
457 hiddenColumns.removeElement(region);
461 if(hiddenColumns.size()==0)
462 hiddenColumns = null;
465 public boolean isVisible(int column)
467 for(int i=0; i<hiddenColumns.size(); i++)
469 int [] region = (int[])hiddenColumns.elementAt(i);
470 if( column >= region[0] && column <= region[1])
481 public ColumnSelection(ColumnSelection copy) {
483 if (copy.selected!=null) {
484 selected = new Vector();
485 for (int i=0,j=copy.selected.size(); i<j; i++) {
486 selected.set(i, ((Integer) copy.selected.get(i)));
489 if (copy.hiddenColumns!=null) {
490 hiddenColumns=new Vector();
491 for (int i=0,j=copy.hiddenColumns.size(); i<j; i++) {
493 rh = (int[])copy.hiddenColumns.get(i);
495 cp = new int[rh.length];
496 System.arraycopy(rh, 0, cp, 0, rh.length);
497 hiddenColumns.set(i, cp);
507 public ColumnSelection()