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
333 public void hideColumns(int res, AlignViewport av)
\r
335 if(hiddenColumns==null)
\r
336 hiddenColumns = new Vector();
\r
338 // First find out range of columns to hide
\r
339 int min = res, max = res+1;
\r
340 while( contains(min) )
\r
341 { removeElement(min); min --; }
\r
343 while( contains(max) )
\r
344 { removeElement(max); max ++; }
\r
348 boolean added = false;
\r
349 for(int i=0; i<hiddenColumns.size(); i++)
\r
351 int [] region = (int[])hiddenColumns.elementAt(i);
\r
352 if( max < region[0])
\r
354 hiddenColumns.insertElementAt(new int[]{min, max}, i);
\r
361 hiddenColumns.addElement(new int[]{min, max});
\r
364 av.setSelectionGroup(null);
\r
365 av.hasHiddenColumns = true;
\r
368 public void revealAllHiddenColumns(AlignViewport av)
\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
378 av.hasHiddenColumns = false;
\r
379 hiddenColumns = null;
\r
382 public void revealHiddenColumns(int res, AlignViewport av)
\r
384 for(int i=0; i<hiddenColumns.size(); i++)
\r
386 int [] region = (int[])hiddenColumns.elementAt(i);
\r
387 if( res == region[0])
\r
389 for (int j = region[0]; j < region[1]; j++)
\r
394 hiddenColumns.remove(region);
\r
400 if(hiddenColumns.size()<1)
\r
401 av.hasHiddenColumns = false;
\r
404 public boolean isVisible(int column)
\r
406 for(int i=0; i<hiddenColumns.size(); i++)
\r
408 int [] region = (int[])hiddenColumns.elementAt(i);
\r
409 if( column >= region[0] && column <= region[1])
\r