2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel;
23 import java.util.List;
25 public class HiddenColumnsCursor
27 // absolute position of first hidden column
28 private int firstColumn;
30 // index of last visited region
31 private int regionIndex;
33 // number of hidden columns before last visited region
34 private int hiddenSoFar;
36 private List<int[]> hiddenColumns;
38 protected HiddenColumnsCursor()
44 * Set the cursor to a position
47 * absolute position of first hidden column
49 * absolute position of last hidden column
51 * index of last visited region
53 * number of hidden columns before last visited region
55 protected void resetCursor(List<int[]> hiddenCols)
59 if ((hiddenCols != null) && (!hiddenCols.isEmpty()))
61 hiddenColumns = hiddenCols;
62 firstColumn = hiddenColumns.get(0)[0];
69 protected void updateCursor(int index, int hiddenCount)
74 hiddenSoFar = hiddenCount;
78 protected synchronized int getIndex()
83 protected synchronized int getHiddenSoFar()
90 * Get the index of the region that column is within (if column is hidden) or
91 * which is to the right of column (if column is visible). If no hidden
92 * columns are to the right, will return size of hiddenColumns. If hidden
93 * columns is empty returns -1.
96 * absolute position of a column in the alignment
97 * @return region index
99 protected int findRegionForColumn(int column)
101 if (hiddenColumns == null)
106 int index = regionIndex;
107 int hiddenCount = hiddenSoFar;
109 if (index == hiddenColumns.size())
111 // went past the end of hiddenColumns collection last time
113 int[] region = hiddenColumns.get(index);
114 hiddenCount -= region[1] - region[0] + 1;
117 if ((hiddenColumns.get(index)[0] <= column)
118 && hiddenColumns.get(index)[1] >= column)
120 // column is in the current region
121 // we hit the jackpot
122 // don't need to move index
124 else if (column < firstColumn)
129 // column is after current region
130 else if (column > hiddenColumns.get(index)[1]) // includes if column >
133 // iterate from where we are now, if we're lucky we'll be close by
134 // (but still better than iterating from 0)
135 // stop when we find the region *before* column
136 // i.e. the next region starts after column or if not, ends after column
137 while ((index < hiddenColumns.size())
138 && (column > hiddenColumns.get(index)[1]))
140 int[] region = hiddenColumns.get(index);
141 hiddenCount += region[1] - region[0] + 1;
146 // column is before current region
147 else if (column < hiddenColumns.get(index)[0])
149 // column is before or in the previous region
150 if ((index > 0) && (hiddenColumns.get(index - 1)[1] >= column))
152 while ((index > 0) && (hiddenColumns.get(index)[1] > column))
155 int[] region = hiddenColumns.get(index);
156 hiddenCount -= region[1] - region[0] + 1;
160 updateCursor(index, hiddenCount);
165 * Get the number of hidden columns in regions before column i.e. excludes
166 * hidden columns in the region column is in, if any
169 * index of column in visible alignment
172 protected int getHiddenOffset(int column)
174 if (hiddenColumns == null)
179 int index = getIndex();
180 int hiddenCount = getHiddenSoFar();
182 if (column < firstColumn)
187 else if ((index < hiddenColumns.size())
188 && (hiddenColumns.get(index)[0] <= column + hiddenCount))
190 // iterate from where we are now, if we're lucky we'll be close by
191 // (but still better than iterating from 0)
192 while ((index < hiddenColumns.size())
193 && (hiddenColumns.get(index)[0] <= column + hiddenCount))
195 int[] region = hiddenColumns.get(index);
196 hiddenCount += region[1] - region[0] + 1;
203 && (hiddenColumns.get(index - 1)[1] >= column + hiddenCount))
206 int[] region = hiddenColumns.get(index);
207 hiddenCount -= region[1] - region[0] + 1;
211 updateCursor(index, hiddenCount);