2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.viewmodel;
23 import jalview.api.AlignViewportI;
24 import jalview.datamodel.AlignmentI;
27 * Slightly less embryonic class which: Supplies and updates viewport properties
28 * relating to position such as: start and end residues and sequences; ideally
29 * will serve hidden columns/rows too. Intention also to support calculations
30 * for positioning, scrolling etc. such as finding the middle of the viewport,
31 * checking for scrolls off screen
33 public class ViewportRanges extends ViewportProperties
35 // start residue of viewport
38 // end residue of viewport
41 // start sequence of viewport
44 // end sequence of viewport
48 private AlignmentI al;
54 * the viewport's alignment
56 public ViewportRanges(AlignmentI alignment)
58 // initial values of viewport settings
60 this.endRes = alignment.getWidth() - 1;
62 this.endSeq = alignment.getHeight() - 1;
67 * Get alignment width in cols, including hidden cols
69 public int getAbsoluteAlignmentWidth()
75 * Get alignment height in rows, including hidden rows
77 public int getAbsoluteAlignmentHeight()
79 return al.getHeight() + al.getHiddenSequences().getSize();
83 * Set first residue visible in the viewport, and retain the current width.
88 public void setStartRes(int res)
90 int width = getViewportWidth();
91 setStartEndRes(res, res + width - 1);
95 * Set start and end residues at the same time. This method only fires one
96 * event for the two changes, and should be used in preference to separate
97 * calls to setStartRes and setEndRes.
104 public void setStartEndRes(int start, int end)
106 int oldres = this.startRes;
107 if (start > al.getWidth() - 1)
109 startRes = al.getWidth() - 1;
129 changeSupport.firePropertyChange("startres", oldres, start);
133 * Set last residue visible in the viewport
138 public void setEndRes(int res)
140 int width = getViewportWidth();
141 setStartEndRes(res - width + 1, res);
145 * Set the first sequence visible in the viewport
150 public void setStartSeq(int seq)
152 int height = getViewportHeight();
153 setStartEndSeq(seq, seq + height - 1);
157 * Set start and end sequences at the same time. This method only fires one
158 * event for the two changes, and should be used in preference to separate
159 * calls to setStartSeq and setEndSeq.
166 public void setStartEndSeq(int start, int end)
168 int oldseq = this.startSeq;
169 if (start > al.getHeight() - 1)
171 startSeq = al.getHeight() - 1;
182 if (end >= al.getHeight())
184 endSeq = al.getHeight() - 1;
195 changeSupport.firePropertyChange("startseq", oldseq, start);
199 * Set the last sequence visible in the viewport
204 public void setEndSeq(int seq)
206 int height = getViewportHeight();
207 setStartEndSeq(seq - height + 1, seq);
211 * Get start residue of viewport
213 public int getStartRes()
219 * Get end residue of viewport
221 public int getEndRes()
227 * Get start sequence of viewport
229 public int getStartSeq()
235 * Get end sequence of viewport
237 public int getEndSeq()
243 * Set viewport width in residues, without changing startRes. Use in
244 * preference to calculating endRes from the width, to avoid out by one
250 public void setViewportWidth(int w)
252 setStartEndRes(startRes, startRes + w - 1);
256 * Set viewport height in residues, without changing startSeq. Use in
257 * preference to calculating endSeq from the height, to avoid out by one
261 * height in sequences
263 public void setViewportHeight(int h)
265 setStartEndSeq(startSeq, startSeq + h - 1);
269 * Set viewport horizontal start position and width. Use in preference to
270 * calculating endRes from the width, to avoid out by one errors!
277 public void setViewportStartAndWidth(int start, int w)
279 setStartEndRes(start, start + w - 1);
283 * Set viewport vertical start position and height. Use in preference to
284 * calculating endSeq from the height, to avoid out by one errors!
289 * height in sequences
291 public void setViewportStartAndHeight(int start, int h)
293 setStartEndSeq(start, start + h - 1);
297 * Get width of viewport in residues
299 * @return width of viewport
301 public int getViewportWidth()
303 return (endRes - startRes + 1);
307 * Get height of viewport in residues
309 * @return height of viewport
311 public int getViewportHeight()
313 return (endSeq - startSeq + 1);
317 * Scroll the viewport range vertically
320 * true if scrolling up, false if down
322 * @return true if the scroll is valid
324 public boolean scrollUp(boolean up)
333 setStartSeq(startSeq - 1);
337 if (endSeq >= al.getHeight() - 1)
342 setStartSeq(startSeq + 1);
348 * Scroll the viewport range horizontally
351 * true if scrolling right, false if left
353 * @return true if the scroll is valid
355 public boolean scrollRight(boolean right)
364 setStartRes(startRes - 1);
368 if (endRes > al.getWidth() - 1)
373 setStartRes(startRes + 1);
380 * Scroll a wrapped alignment so that the specified residue is visible
383 * residue position to scroll to
385 public void scrollToWrappedVisible(int res)
387 // get the start residue of the wrapped row which res is in
388 // and set that as our start residue
389 int width = getViewportWidth();
390 setStartRes((res / width) * width);
393 // pass av here until hidden columns JAL-2388 merged, then use alignment
395 public void scrollToVisible(int x, int y, AlignViewportI av)
401 while (y + 1 > endSeq)
406 while (x < av.getColumnSelection().adjustForHiddenColumns(startRes))
408 if (!scrollRight(false))
413 while (x > av.getColumnSelection().adjustForHiddenColumns(endRes))
415 if (!scrollRight(true))