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.
84 * Fires a property change event.
89 public void setStartRes(int res)
91 int width = getViewportWidth();
92 setStartEndRes(res, res + width - 1);
96 * Set start and end residues at the same time. This method only fires one
97 * event for the two changes, and should be used in preference to separate
98 * calls to setStartRes and setEndRes.
105 public void setStartEndRes(int start, int end)
107 int oldstartres = this.startRes;
108 if (start > al.getWidth() - 1)
110 startRes = al.getWidth() - 1;
121 int oldendres = this.endRes;
131 changeSupport.firePropertyChange("startres", oldstartres, startRes);
132 if (oldstartres == startRes)
134 // event won't be fired if start positions are same
135 // fire an event for the end positions in case they changed
136 changeSupport.firePropertyChange("endres", oldendres, endRes);
141 * Set last residue visible in the viewport. Fires a property change event.
146 public void setEndRes(int res)
148 int width = getViewportWidth();
149 setStartEndRes(res - width + 1, res);
153 * Set the first sequence visible in the viewport. Fires a property change
159 public void setStartSeq(int seq)
161 int height = getViewportHeight();
162 setStartEndSeq(seq, seq + height - 1);
166 * Set start and end sequences at the same time. This method only fires one
167 * event for the two changes, and should be used in preference to separate
168 * calls to setStartSeq and setEndSeq.
175 public void setStartEndSeq(int start, int end)
177 int oldstartseq = this.startSeq;
178 if (start > al.getHeight() - 1)
180 startSeq = al.getHeight() - 1;
191 int oldendseq = this.endSeq;
192 if (end >= al.getHeight())
194 endSeq = al.getHeight() - 1;
205 changeSupport.firePropertyChange("startseq", oldstartseq, startSeq);
206 if (oldstartseq == startSeq)
208 // event won't be fired if start positions are the same
209 // fire in case the end positions changed
210 changeSupport.firePropertyChange("endseq", oldendseq, endSeq);
215 * Set the last sequence visible in the viewport. Fires a property change
221 public void setEndSeq(int seq)
223 int height = getViewportHeight();
224 setStartEndSeq(seq - height + 1, seq);
228 * Get start residue of viewport
230 public int getStartRes()
236 * Get end residue of viewport
238 public int getEndRes()
244 * Get start sequence of viewport
246 public int getStartSeq()
252 * Get end sequence of viewport
254 public int getEndSeq()
260 * Set viewport width in residues, without changing startRes. Use in
261 * preference to calculating endRes from the width, to avoid out by one
262 * errors! Fires a property change event.
267 public void setViewportWidth(int w)
269 setStartEndRes(startRes, startRes + w - 1);
273 * Set viewport height in residues, without changing startSeq. Use in
274 * preference to calculating endSeq from the height, to avoid out by one
275 * errors! Fires a property change event.
278 * height in sequences
280 public void setViewportHeight(int h)
282 setStartEndSeq(startSeq, startSeq + h - 1);
286 * Set viewport horizontal start position and width. Use in preference to
287 * calculating endRes from the width, to avoid out by one errors! Fires a
288 * property change event.
295 public void setViewportStartAndWidth(int start, int w)
302 else if (vpstart + w - 1 > al.getWidth() - 1)
304 vpstart = al.getWidth() - 1;
306 setStartEndRes(vpstart, vpstart + w - 1);
310 * Set viewport vertical start position and height. Use in preference to
311 * calculating endSeq from the height, to avoid out by one errors! Fires a
312 * property change event.
317 * height in sequences
319 public void setViewportStartAndHeight(int start, int h)
326 else if (vpstart + h - 1 > al.getHeight() - 1)
328 vpstart = al.getHeight() - h;
330 setStartEndSeq(vpstart, vpstart + h - 1);
334 * Get width of viewport in residues
336 * @return width of viewport
338 public int getViewportWidth()
340 return (endRes - startRes + 1);
344 * Get height of viewport in residues
346 * @return height of viewport
348 public int getViewportHeight()
350 return (endSeq - startSeq + 1);
354 * Scroll the viewport range vertically. Fires a property change event.
357 * true if scrolling up, false if down
359 * @return true if the scroll is valid
361 public boolean scrollUp(boolean up)
370 setStartSeq(startSeq - 1);
374 if (endSeq >= al.getHeight() - 1)
379 setStartSeq(startSeq + 1);
385 * Scroll the viewport range horizontally. Fires a property change event.
388 * true if scrolling right, false if left
390 * @return true if the scroll is valid
392 public boolean scrollRight(boolean right)
401 setStartRes(startRes - 1);
405 if (endRes > al.getWidth() - 1)
410 setStartRes(startRes + 1);
417 * Scroll a wrapped alignment so that the specified residue is visible. Fires
418 * a property change event.
421 * residue position to scroll to
423 public void scrollToWrappedVisible(int res)
425 // get the start residue of the wrapped row which res is in
426 // and set that as our start residue
427 int width = getViewportWidth();
428 setStartRes((res / width) * width);
432 * Scroll so that (x,y) is visible. Fires a property change event.
435 * x position in alignment
437 * y position in alignment
439 * viewport to be visible in. Here until hidden columns JAL-2388
440 * merged, then use alignment to get hidden cols
442 public void scrollToVisible(int x, int y, AlignViewportI av)
453 while (x < av.getColumnSelection().adjustForHiddenColumns(startRes))
455 if (!scrollRight(false))
460 while (x > av.getColumnSelection().adjustForHiddenColumns(endRes))
462 if (!scrollRight(true))
470 * Adjust sequence position for page up. Fires a property change event.
474 setViewportStartAndHeight(2 * startSeq - endSeq, getViewportHeight());
478 * Adjust sequence position for page down. Fires a property change event.
480 public void pageDown()
482 setViewportStartAndHeight(endSeq, getViewportHeight());