Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.datamodel

File HiddenColumnsCursor.java

 

Coverage histogram

../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

20
36
8
1
213
103
22
0.61
4.5
8
2.75

Classes

Class Line # Actions
HiddenColumnsCursor 26 36 22 0
1.0100%
 

Contributing tests

This file is covered by 290 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.datamodel;
22   
23    import java.util.ArrayList;
24    import java.util.List;
25   
 
26    public class HiddenColumnsCursor
27    {
28    // absolute position of first hidden column
29    private int firstColumn;
30   
31    private List<int[]> hiddenColumns = new ArrayList<>();
32   
33    private HiddenCursorPosition cursorPos = new HiddenCursorPosition(0, 0);
34   
 
35  1390 toggle protected HiddenColumnsCursor()
36    {
37   
38    }
39   
 
40  194 toggle protected HiddenColumnsCursor(List<int[]> hiddenCols)
41    {
42  194 resetCursor(hiddenCols, 0, 0);
43    }
44   
 
45  374 toggle protected HiddenColumnsCursor(List<int[]> hiddenCols, int index,
46    int hiddencount)
47    {
48  374 resetCursor(hiddenCols, index, hiddencount);
49    }
50   
51    /**
52    * Reset the cursor with a new hidden columns collection, where we know in
53    * advance the index and hidden columns count of a particular location.
54    *
55    * @param hiddenCols
56    * new hidden columns collection
57    * @param index
58    * cursor index to reset to
59    * @param hiddencount
60    * hidden columns count to reset to
61    */
 
62  568 toggle private void resetCursor(List<int[]> hiddenCols, int index,
63    int hiddencount)
64    {
65  568 hiddenColumns = hiddenCols;
66  568 if (!hiddenCols.isEmpty())
67    {
68  411 firstColumn = hiddenColumns.get(0)[0];
69  411 cursorPos = new HiddenCursorPosition(index,
70    hiddencount);
71    }
72    }
73   
74    /**
75    * Get the cursor pointing to the hidden region that column is within (if
76    * column is hidden) or which is to the right of column (if column is
77    * visible). If no hidden columns are to the right, returns a cursor pointing
78    * to an imaginary hidden region beyond the end of the hidden columns
79    * collection (this ensures the count of previous hidden columns is correct).
80    * If hidden columns is empty returns null.
81    *
82    * @param column
83    * index of column in visible or absolute coordinates
84    * @param useVisible
85    * true if column is in visible coordinates, false if absolute
86    * @return cursor pointing to hidden region containing the column (if hidden)
87    * or to the right of the column (if visible)
88    */
 
89  209310 toggle protected HiddenCursorPosition findRegionForColumn(int column,
90    boolean useVisible)
91    {
92  209310 if (hiddenColumns.isEmpty())
93    {
94  6 return null;
95    }
96   
97    // used to add in hiddenColumns offset when working with visible columns
98  209304 int offset = (useVisible ? 1 : 0);
99   
100  209304 HiddenCursorPosition pos = cursorPos;
101  209304 int index = pos.getRegionIndex();
102  209304 int hiddenCount = pos.getHiddenSoFar();
103   
104  209304 if (column < firstColumn)
105    {
106  119 pos = new HiddenCursorPosition(0, 0);
107    }
108   
109    // column is after current region
110  209185 else if ((index < hiddenColumns.size())
111    && (hiddenColumns.get(index)[0] <= column
112    + offset * hiddenCount))
113    {
114    // iterate from where we are now, if we're lucky we'll be close by
115    // (but still better than iterating from 0)
116    // stop when we find the region *before* column
117    // i.e. the next region starts after column or if not, ends after column
118  719 pos = searchForward(pos, column, useVisible);
119    }
120   
121    // column is before current region
122    else
123    {
124  208466 pos = searchBackward(pos, column, useVisible);
125    }
126  209304 cursorPos = pos;
127  209304 return pos;
128    }
129   
130    /**
131    * Search forwards through the hidden columns collection to find the hidden
132    * region immediately before a column
133    *
134    * @param pos
135    * current position
136    * @param column
137    * column to locate
138    * @param useVisible
139    * whether using visible or absolute coordinates
140    * @return position of region before column
141    */
 
142  719 toggle private HiddenCursorPosition searchForward(HiddenCursorPosition pos,
143    int column, boolean useVisible)
144    {
145  719 HiddenCursorPosition p = pos;
146  719 if (useVisible)
147    {
148  712 while ((p.getRegionIndex() < hiddenColumns.size())
149    && hiddenColumns.get(p.getRegionIndex())[0] <= column
150    + p.getHiddenSoFar())
151    {
152  360 p = stepForward(p);
153    }
154    }
155    else
156    {
157  757 while ((p.getRegionIndex() < hiddenColumns.size())
158    && hiddenColumns.get(p.getRegionIndex())[1] < column)
159    {
160  390 p = stepForward(p);
161    }
162    }
163  719 return p;
164    }
165   
166    /**
167    * Move to the next (rightwards) hidden region after a given cursor position
168    *
169    * @param p
170    * current position of cursor
171    * @return new position of cursor at next region
172    */
 
173  750 toggle private HiddenCursorPosition stepForward(HiddenCursorPosition p)
174    {
175  750 int[] region = hiddenColumns.get(p.getRegionIndex());
176   
177    // increment the index, and add this region's hidden columns to the hidden
178    // column count
179  750 return new HiddenCursorPosition(p.getRegionIndex() + 1,
180    p.getHiddenSoFar() + region[1] - region[0] + 1);
181    }
182   
183    /**
184    * Search backwards through the hidden columns collection to find the hidden
185    * region immediately before (left of) a given column
186    *
187    * @param pos
188    * current position
189    * @param column
190    * column to locate
191    * @param useVisible
192    * whether using visible or absolute coordinates
193    * @return position of region immediately to left of column
194    */
 
195  208466 toggle private HiddenCursorPosition searchBackward(HiddenCursorPosition p,
196    int column, boolean useVisible)
197    {
198  208466 int i = p.getRegionIndex();
199  208466 int h = p.getHiddenSoFar();
200   
201    // used to add in hiddenColumns offset when working with visible columns
202  208466 int offset = (useVisible ? 1 : 0);
203   
204  208896 while ((i > 0) && (hiddenColumns.get(i - 1)[1] >= column + offset * h))
205    {
206  430 i--;
207  430 int[] region = hiddenColumns.get(i);
208  430 h -= region[1] - region[0] + 1;
209    }
210  208466 return new HiddenCursorPosition(i, h);
211    }
212   
213    }