1 package jalview.datamodel;
5 public class HiddenColumnsCursor
7 // absolute position of first hidden column
8 private int firstColumn;
10 // absolute position of last hidden column
11 private int lastColumn;
13 // index of last visited region
14 private int regionIndex;
16 // number of hidden columns before last visited region
17 private int hiddenSoFar;
19 private List<int[]> hiddenColumns;
21 protected HiddenColumnsCursor()
27 * Set the cursor to a position
30 * absolute position of first hidden column
32 * absolute position of last hidden column
34 * index of last visited region
36 * number of hidden columns before last visited region
38 protected void resetCursor(List<int[]> hiddenCols)
42 if ((hiddenCols != null) && (!hiddenCols.isEmpty()))
44 hiddenColumns = hiddenCols;
45 firstColumn = hiddenColumns.get(0)[0];
46 lastColumn = hiddenColumns.get(hiddenColumns.size() - 1)[1];
53 protected void updateCursor(int index, int hiddenCount)
58 hiddenSoFar = hiddenCount;
62 protected synchronized int getIndex()
67 protected synchronized int getHiddenSoFar()
74 * Get the index of the region that column is within (if column is hidden) or
75 * which is to the right of column (if column is visible). If no hidden
76 * columns are to the right, will return size of hiddenColumns. If hidden
77 * columns is empty returns -1.
80 * absolute position of a column in the alignment
81 * @return region index
83 protected int findRegionForColumn(int column)
85 if (hiddenColumns == null)
90 int index = regionIndex;
91 int hiddenCount = hiddenSoFar;
93 if (index == hiddenColumns.size())
95 // went past the end of hiddenColumns collection last time
97 int[] region = hiddenColumns.get(index);
98 hiddenCount -= region[1] - region[0] + 1;
101 if ((hiddenColumns.get(index)[0] <= column)
102 && hiddenColumns.get(index)[1] >= column)
104 // column is in the current region
105 // we hit the jackpot
106 // don't need to move index
108 else if (column < firstColumn)
113 // column is after current region
114 else if (column > hiddenColumns.get(index)[1]) // includes if column >
117 // iterate from where we are now, if we're lucky we'll be close by
118 // (but still better than iterating from 0)
119 // stop when we find the region *before* column
120 // i.e. the next region starts after column or if not, ends after column
121 while ((index < hiddenColumns.size())
122 && (column > hiddenColumns.get(index)[1]))
124 int[] region = hiddenColumns.get(index);
125 hiddenCount += region[1] - region[0] + 1;
130 // column is before current region
131 else if (column < hiddenColumns.get(index)[0])
133 while ((index > 0) && (hiddenColumns.get(index)[1] > column))
136 int[] region = hiddenColumns.get(index);
137 hiddenCount -= region[1] - region[0] + 1;
140 updateCursor(index, hiddenCount);
144 protected int getHiddenOffset(int column)
146 if (hiddenColumns == null)
151 int index = getIndex();
152 int hiddenCount = getHiddenSoFar();
154 if (column < firstColumn)
159 else if ((index < hiddenColumns.size())
160 && (hiddenColumns.get(index)[0] <= column + hiddenCount))
162 // iterate from where we are now, if we're lucky we'll be close by
163 // (but still better than iterating from 0)
164 while ((index < hiddenColumns.size())
165 && (hiddenColumns.get(index)[0] <= column + hiddenCount))
167 int[] region = hiddenColumns.get(index);
168 hiddenCount += region[1] - region[0] + 1;
172 else if (index < hiddenColumns.size())
175 && (hiddenColumns.get(index - 1)[1] >= column + hiddenCount))
178 int[] region = hiddenColumns.get(index);
179 hiddenCount -= region[1] - region[0] + 1;
183 updateCursor(index, hiddenCount);