JAL-2491 Tweaks to avoid scroll problems at bottom of wrapped alignment
[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 < 0)
107     {
108       endres = 0;
109     }
110     this.endRes = endres;
111
112     changeSupport.firePropertyChange("startres", oldres, startres);
113   }
114
115   /**
116    * Set last residue visible in the viewport
117    * 
118    * @param res
119    *          residue position
120    */
121   public void setEndRes(int res)
122   {
123     int width = getViewportWidth();
124     setStartEndRes(res - width + 1, res);
125   }
126
127   /**
128    * Set the first sequence visible in the viewport
129    * 
130    * @param seq
131    *          sequence position
132    */
133   public void setStartSeq(int seq)
134   {
135     int height = getViewportHeight();
136     setStartEndSeq(seq, seq + height - 1);
137   }
138
139   public void setStartEndSeq(int startseq, int endseq)
140   {
141     int oldseq = this.startSeq;
142     if (startseq > al.getHeight() - 1)
143     {
144       startseq = al.getHeight() - 1;
145     }
146     else if (startseq < 0)
147     {
148       startseq = 0;
149     }
150     this.startSeq = startseq;
151
152     if (endseq >= al.getHeight())
153     {
154       endseq = al.getHeight() - 1;
155     }
156     else if (endseq < 0)
157     {
158       endseq = 0;
159     }
160     this.endSeq = endseq;
161     changeSupport.firePropertyChange("startseq", oldseq, startseq);
162   }
163
164   /**
165    * Set the last sequence visible in the viewport
166    * 
167    * @param seq
168    *          sequence position
169    */
170   public void setEndSeq(int seq)
171   {
172     int height = getViewportHeight();
173     setStartEndSeq(seq - height + 1, seq);
174   }
175
176   /**
177    * Get start residue of viewport
178    */
179   public int getStartRes()
180   {
181     return startRes;
182   }
183
184   /**
185    * Get end residue of viewport
186    */
187   public int getEndRes()
188   {
189     return endRes;
190   }
191
192   /**
193    * Get start sequence of viewport
194    */
195   public int getStartSeq()
196   {
197     return startSeq;
198   }
199
200   /**
201    * Get end sequence of viewport
202    */
203   public int getEndSeq()
204   {
205     return endSeq;
206   }
207
208   /**
209    * Get width of viewport in residues
210    * 
211    * @return width of viewport
212    */
213   public int getViewportWidth()
214   {
215     return (endRes - startRes + 1); // TODO get for wrapped alignments too
216   }
217
218   /**
219    * Get height of viewport in residues
220    * 
221    * @return height of viewport
222    */
223   public int getViewportHeight()
224   {
225     return (endSeq - startSeq + 1);
226   }
227
228   // return value is true if the scroll is valid
229   public boolean scrollUp(boolean up)
230   {
231     if (up)
232     {
233       if (startSeq < 1)
234       {
235         return false;
236       }
237
238       setStartSeq(startSeq - 1);
239     }
240     else
241     {
242       if (endSeq >= al.getHeight() - 1)
243       {
244         return false;
245       }
246
247       setStartSeq(startSeq + 1);
248     }
249     return true;
250   }
251
252   /**
253    * DOCUMENT ME!
254    * 
255    * @param right
256    *          DOCUMENT ME!
257    * 
258    * @return DOCUMENT ME!
259    */
260   public boolean scrollRight(boolean right)
261   {
262     if (!right)
263     {
264       if (startRes < 1)
265       {
266         return false;
267       }
268
269       setStartRes(startRes - 1);
270     }
271     else
272     {
273       if (endRes > al.getWidth() - 1)
274       {
275         return false;
276       }
277
278       setStartRes(startRes + 1);
279     }
280
281     return true;
282   }
283
284   public void scrollToWrappedVisible(int res)
285   {
286     setStartRes(res);
287   }
288
289 }