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 private synchronized int getIndex()
67 private 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 = getIndex();
91 int hiddenCount = getHiddenSoFar();
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 // we hit the jackpot
105 // don't need to move index
107 else if (column < firstColumn)
112 else if (column > lastColumn)
114 index = hiddenColumns.size();
115 // TODO resolve here - need full hidden count
117 else if (column > hiddenColumns.get(index)[1])
119 // iterate from where we are now, if we're lucky we'll be close by
120 // (but still better than iterating from 0)
121 while ((index < hiddenColumns.size())
122 && (hiddenColumns.get(index)[0] <= column))
124 int[] region = hiddenColumns.get(index);
125 hiddenCount += region[1] - region[0] + 1;
130 else // (column < hiddenColumns.get(regionIndex)[0])
132 while ((index > 0) && (hiddenColumns.get(index)[1] > column))
135 int[] region = hiddenColumns.get(index);
136 hiddenCount -= region[1] - region[0] + 1;
139 updateCursor(index, hiddenCount);
143 protected int getHiddenOffset(int column)
145 if (hiddenColumns == null)
150 int index = getIndex();
151 int hiddenCount = getHiddenSoFar();
153 if (column < firstColumn)
158 else if ((index < hiddenColumns.size())
159 && (hiddenColumns.get(index)[0] <= column + hiddenCount))
161 // iterate from where we are now, if we're lucky we'll be close by
162 // (but still better than iterating from 0)
163 while ((index < hiddenColumns.size())
164 && (hiddenColumns.get(index)[0] <= column + hiddenCount))
166 int[] region = hiddenColumns.get(index);
167 hiddenCount += region[1] - region[0] + 1;
171 else if (index < hiddenColumns.size())
174 && (hiddenColumns.get(index)[1] > column + hiddenCount))
177 int[] region = hiddenColumns.get(index);
178 hiddenCount -= region[1] - region[0] + 1;
182 updateCursor(index, hiddenCount);