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 = 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 // 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]) // includes if column >
120 // iterate from where we are now, if we're lucky we'll be close by
121 // (but still better than iterating from 0)
122 while ((index < hiddenColumns.size())
123 && (hiddenColumns.get(index)[0] <= column))
125 int[] region = hiddenColumns.get(index);
126 hiddenCount += region[1] - region[0] + 1;
131 else // (column < hiddenColumns.get(regionIndex)[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);