2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.gui;
\r
24 * NOTE: Columns are zero based.
\r
26 public class ColumnSelection
\r
28 Vector selected = new Vector();
\r
30 //Vector of int [] {startCol, endCol}
\r
31 Vector hiddenColumns;
\r
36 * @param col DOCUMENT ME!
\r
38 public void addElement(int col)
\r
40 if (!selected.contains(new Integer(col)))
\r
42 selected.addElement(new Integer(col));
\r
51 selected.removeAllElements();
\r
57 * @param col DOCUMENT ME!
\r
59 public void removeElement(int col)
\r
61 Integer colInt = new Integer(col);
\r
63 if (selected.contains(colInt))
\r
65 selected.removeElement(colInt);
\r
69 public void removeElements(int start, int end)
\r
72 for(int i=start; i<end; i++)
\r
74 colInt = new Integer(i);
\r
75 if (selected.contains(colInt))
\r
77 selected.removeElement(colInt);
\r
85 * @param col DOCUMENT ME!
\r
87 * @return DOCUMENT ME!
\r
89 public boolean contains(int col)
\r
91 return selected.contains(new Integer(col));
\r
97 * @param i DOCUMENT ME!
\r
99 * @return DOCUMENT ME!
\r
101 public int columnAt(int i)
\r
103 return ((Integer) selected.elementAt(i)).intValue();
\r
109 * @return DOCUMENT ME!
\r
113 return selected.size();
\r
119 * @return DOCUMENT ME!
\r
121 public int getMax()
\r
125 for (int i = 0; i < selected.size(); i++)
\r
127 if (columnAt(i) > max)
\r
139 * @return DOCUMENT ME!
\r
141 public int getMin()
\r
143 int min = 1000000000;
\r
145 for (int i = 0; i < selected.size(); i++)
\r
147 if (columnAt(i) < min)
\r
160 * @param start DOCUMENT ME!
\r
161 * @param change DOCUMENT ME!
\r
163 public void compensateForEdit(int start, int change)
\r
165 for (int i = 0; i < size(); i++)
\r
167 int temp = columnAt(i);
\r
171 selected.setElementAt(new Integer(temp - change), i);
\r
175 if(hiddenColumns!=null)
\r
177 for(int i=0; i<hiddenColumns.size(); i++)
\r
179 int[] region = (int[]) hiddenColumns.elementAt(i);
\r
180 if(region[0] > start)
\r
182 region[0] -= change;
\r
183 region[1] -= change;
\r
194 * This Method is used to return all the HiddenColumn regions
\r
195 * less than the given index.
\r
199 public Vector getHiddenColumns()
\r
201 return hiddenColumns;
\r
204 public int adjustForHiddenColumns(int column)
\r
206 int result = column;
\r
207 if (hiddenColumns != null)
\r
209 for (int i = 0; i < hiddenColumns.size(); i++)
\r
211 int[] region = (int[]) hiddenColumns.elementAt(i);
\r
212 if (result >= region[0])
\r
214 result += region[1] - region[0] + 1;
\r
222 * Use this method to find out where a visible column is in the alignment
\r
223 * when hidden columns exist
\r
224 * @param hiddenColumn int
\r
227 public int findColumnPosition(int hiddenColumn)
\r
229 int result = hiddenColumn;
\r
230 if (hiddenColumns != null)
\r
236 int[] region = (int[]) hiddenColumns.elementAt(index);
\r
237 if (hiddenColumn > region[1])
\r
239 result -= region[1]+1-region[0];
\r
243 while (index < hiddenColumns.size());
\r
252 * Use this method to determine where the next hiddenRegion starts
\r
254 public int findHiddenRegionPosition(int hiddenRegion)
\r
257 if (hiddenColumns != null)
\r
263 int[] region = (int[]) hiddenColumns.elementAt(index);
\r
264 if(hiddenRegion==0)
\r
269 gaps += region[1] +1 - region[0];
\r
270 result = region[1] +1;
\r
273 while(index < hiddenRegion+1);
\r
282 * THis method returns the rightmost limit of a
\r
283 * region of an alignment with hidden columns.
\r
284 * In otherwords, the next hidden column.
\r
287 public int getHiddenBoundaryRight(int alPos)
\r
289 if (hiddenColumns != null)
\r
294 int[] region = (int[]) hiddenColumns.elementAt(index);
\r
295 if(alPos < region[0])
\r
300 while(index < hiddenColumns.size());
\r
307 * THis method returns the rightmost limit of a
\r
308 * region of an alignment with hidden columns.
\r
309 * In otherwords, the next hidden column.
\r
312 public int getHiddenBoundaryLeft(int alPos)
\r
314 if (hiddenColumns != null)
\r
316 int index = hiddenColumns.size()-1;
\r
319 int[] region = (int[]) hiddenColumns.elementAt(index);
\r
320 if(alPos > region[1])
\r
334 public void hideColumns(int res)
\r
336 if(hiddenColumns==null)
\r
337 hiddenColumns = new Vector();
\r
339 // First find out range of columns to hide
\r
340 int min = res, max = res+1;
\r
341 while( contains(min) )
\r
342 { removeElement(min); min --; }
\r
344 while( contains(max) )
\r
345 { removeElement(max); max ++; }
\r
349 boolean added = false;
\r
350 for(int i=0; i<hiddenColumns.size(); i++)
\r
352 int [] region = (int[])hiddenColumns.elementAt(i);
\r
353 if( max < region[0])
\r
355 hiddenColumns.insertElementAt(new int[]{min, max}, i);
\r
362 hiddenColumns.addElement(new int[]{min, max});
\r
366 public void revealAllHiddenColumns(AlignViewport av)
\r
368 if(hiddenColumns!=null)
\r
370 for (int i = 0; i < hiddenColumns.size(); i++)
\r
372 int[] region = (int[]) hiddenColumns.elementAt(i);
\r
373 for (int j = region[0]; j < region[1]; j++)
\r
379 av.hasHiddenColumns = false;
\r
380 hiddenColumns = null;
\r
383 public void revealHiddenColumns(int res, AlignViewport av)
\r
385 for(int i=0; i<hiddenColumns.size(); i++)
\r
387 int [] region = (int[])hiddenColumns.elementAt(i);
\r
388 if( res == region[0])
\r
390 for (int j = region[0]; j < region[1]; j++)
\r
395 hiddenColumns.remove(region);
\r
401 if(hiddenColumns.size()<1)
\r
402 av.hasHiddenColumns = false;
\r
405 public boolean isVisible(int column)
\r
407 for(int i=0; i<hiddenColumns.size(); i++)
\r
409 int [] region = (int[])hiddenColumns.elementAt(i);
\r
410 if( column >= region[0] && column <= region[1])
\r