JAL-2491 Started moving other scrolling fns into viewportranges
[jalview.git] / src / jalview / viewmodel / ViewportRanges.java
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.viewmodel;
22
23 import jalview.datamodel.AlignmentI;
24
25 /**
26  * Embryonic class which: Supplies and updates viewport properties relating to
27  * position such as: start and end residues and sequences; ideally will serve
28  * hidden columns/rows too. Intention also to support calculations for
29  * positioning, scrolling etc. such as finding the middle of the viewport,
30  * checking for scrolls off screen
31  */
32 public class ViewportRanges extends ViewportProperties
33 {
34   // start residue of viewport
35   private int startRes;
36
37   // end residue of viewport
38   private int endRes;
39
40   // start sequence of viewport
41   private int startSeq;
42
43   // end sequence of viewport
44   private int endSeq;
45
46   // alignment
47   private AlignmentI al;
48
49   /**
50    * Constructor
51    * 
52    * @param alignment
53    *          the viewport's alignment
54    */
55   public ViewportRanges(AlignmentI alignment)
56   {
57     // initial values of viewport settings
58     this.startRes = 0;
59     this.endRes = alignment.getWidth() - 1;
60     this.startSeq = 0;
61     this.endSeq = alignment.getHeight() - 1;
62     this.al = alignment;
63   }
64
65   /**
66    * Get alignment width in cols, including hidden cols
67    */
68   public int getAbsoluteAlignmentWidth()
69   {
70     return al.getWidth();
71   }
72
73   /**
74    * Get alignment height in rows, including hidden rows
75    */
76   public int getAbsoluteAlignmentHeight()
77   {
78     return al.getHeight() + al.getHiddenSequences().getSize();
79   }
80
81   /**
82    * Set first residue visible in the viewport, and retain the current width.
83    * 
84    * @param res
85    *          residue position
86    */
87   public void setStartRes(int res)
88   {
89     int width = getViewportWidth();
90     setStartEndRes(res, res + width - 1);
91   }
92
93   public void setStartEndRes(int startres, int endres)
94   {
95     int oldres = this.startRes;
96     if (startres > al.getWidth() - 1)
97     {
98       startres = al.getWidth() - 1;
99     }
100     else if (startres < 0)
101     {
102       startres = 0;
103     }
104     this.startRes = startres;
105
106     if (endres >= al.getWidth())
107     {
108       endres = al.getWidth() - 1;
109     }
110     else if (endres < 0)
111     {
112       endres = 0;
113     }
114     this.endRes = endres;
115
116     changeSupport.firePropertyChange("startres", oldres, startres);
117   }
118
119   /**
120    * Set last residue visible in the viewport
121    * 
122    * @param res
123    *          residue position
124    */
125   public void setEndRes(int res)
126   {
127     int width = getViewportWidth();
128     setStartEndRes(res - width + 1, res);
129   }
130
131   /**
132    * Set the first sequence visible in the viewport
133    * 
134    * @param seq
135    *          sequence position
136    */
137   public void setStartSeq(int seq)
138   {
139     int height = getViewportHeight();
140     setStartEndSeq(seq, seq + height - 1);
141   }
142
143   public void setStartEndSeq(int startseq, int endseq)
144   {
145     int oldseq = this.startSeq;
146     if (startseq > al.getHeight() - 1)
147     {
148       startseq = al.getHeight() - 1;
149     }
150     else if (startseq < 0)
151     {
152       startseq = 0;
153     }
154     this.startSeq = startseq;
155
156     if (endseq >= al.getHeight())
157     {
158       endseq = al.getHeight() - 1;
159     }
160     else if (endseq < 0)
161     {
162       endseq = 0;
163     }
164     this.endSeq = endseq;
165     changeSupport.firePropertyChange("startseq", oldseq, startseq);
166   }
167
168   /**
169    * Set the last sequence visible in the viewport
170    * 
171    * @param seq
172    *          sequence position
173    */
174   public void setEndSeq(int seq)
175   {
176     int height = getViewportHeight();
177     setStartEndSeq(seq - height + 1, seq);
178   }
179
180   /**
181    * Get start residue of viewport
182    */
183   public int getStartRes()
184   {
185     return startRes;
186   }
187
188   /**
189    * Get end residue of viewport
190    */
191   public int getEndRes()
192   {
193     return endRes;
194   }
195
196   /**
197    * Get start sequence of viewport
198    */
199   public int getStartSeq()
200   {
201     return startSeq;
202   }
203
204   /**
205    * Get end sequence of viewport
206    */
207   public int getEndSeq()
208   {
209     return endSeq;
210   }
211
212   /**
213    * Get width of viewport in residues
214    * 
215    * @return width of viewport
216    */
217   public int getViewportWidth()
218   {
219     return (endRes - startRes + 1); // TODO get for wrapped alignments too
220   }
221
222   /**
223    * Get height of viewport in residues
224    * 
225    * @return height of viewport
226    */
227   public int getViewportHeight()
228   {
229     return (endSeq - startSeq + 1);
230   }
231
232   // return value is true if the scroll is valid
233   public boolean scrollUp(boolean up)
234   {
235     if (up)
236     {
237       if (startSeq < 1)
238       {
239         return false;
240       }
241
242       setStartSeq(startSeq - 1);
243     }
244     else
245     {
246       if (endSeq >= al.getHeight() - 1)
247       {
248         return false;
249       }
250
251       setStartSeq(startSeq + 1);
252     }
253     return true;
254   }
255
256   /**
257    * DOCUMENT ME!
258    * 
259    * @param right
260    *          DOCUMENT ME!
261    * 
262    * @return DOCUMENT ME!
263    */
264   public boolean scrollRight(boolean right)
265   {
266     if (!right)
267     {
268       if (startRes < 1)
269       {
270         return false;
271       }
272
273       setStartRes(startRes - 1);
274     }
275     else
276     {
277       if (endRes > al.getWidth() - 1)
278       {
279         return false;
280       }
281
282       setStartRes(startRes + 1);
283     }
284
285     return true;
286   }
287
288   public void scrollToWrappedVisible(int res)
289   {
290     if (res < startRes || res > endRes)
291     {
292       setEndSeq(res / getViewportWidth());
293     }
294
295   }
296
297 }