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)
149 int width = getViewportWidth();
150 if (startres + width - 1 > al.getWidth() - 1)
152 startres = al.getWidth() - width;
154 setStartEndRes(startres - width + 1, startres);
158 * Set the first sequence visible in the viewport, maintaining the height. If
159 * the viewport would extend past the last sequence, sets the viewport so it
160 * sits at the bottom of the alignment. Fires a property change event.
165 public void setStartSeq(int seq)
168 int height = getViewportHeight();
169 if (startseq + height - 1 > al.getHeight() - 1)
171 startseq = al.getHeight() - height;
173 setStartEndSeq(startseq, startseq + height - 1);
177 * Set start and end sequences at the same time. The viewport height may
178 * change. This method only fires one event for the two changes, and should be
179 * used in preference to separate calls to setStartSeq and setEndSeq.
186 public void setStartEndSeq(int start, int end)
188 int oldstartseq = this.startSeq;
189 if (start > al.getHeight() - 1)
191 startSeq = al.getHeight() - 1;
202 int oldendseq = this.endSeq;
203 if (end >= al.getHeight())
205 endSeq = al.getHeight() - 1;
216 changeSupport.firePropertyChange("startseq", oldstartseq, startSeq);
217 if (oldstartseq == startSeq)
219 // event won't be fired if start positions are the same
220 // fire in case the end positions changed
221 changeSupport.firePropertyChange("endseq", oldendseq, endSeq);
226 * Set the last sequence visible in the viewport. Fires a property change
232 public void setEndSeq(int seq)
234 int height = getViewportHeight();
235 setStartEndSeq(seq - height + 1, seq);
239 * Get start residue of viewport
241 public int getStartRes()
247 * Get end residue of viewport
249 public int getEndRes()
255 * Get start sequence of viewport
257 public int getStartSeq()
263 * Get end sequence of viewport
265 public int getEndSeq()
271 * Set viewport width in residues, without changing startRes. Use in
272 * preference to calculating endRes from the width, to avoid out by one
273 * errors! Fires a property change event.
278 public void setViewportWidth(int w)
280 setStartEndRes(startRes, startRes + w - 1);
284 * Set viewport height in residues, without changing startSeq. Use in
285 * preference to calculating endSeq from the height, to avoid out by one
286 * errors! Fires a property change event.
289 * height in sequences
291 public void setViewportHeight(int h)
293 setStartEndSeq(startSeq, startSeq + h - 1);
297 * Set viewport horizontal start position and width. Use in preference to
298 * calculating endRes from the width, to avoid out by one errors! Fires a
299 * property change event.
306 public void setViewportStartAndWidth(int start, int w)
313 else if (vpstart + w - 1 > al.getWidth() - 1)
315 vpstart = al.getWidth() - 1;
317 setStartEndRes(vpstart, vpstart + w - 1);
321 * Set viewport vertical start position and height. Use in preference to
322 * calculating endSeq from the height, to avoid out by one errors! Fires a
323 * property change event.
328 * height in sequences
330 public void setViewportStartAndHeight(int start, int h)
337 else if (vpstart + h - 1 > al.getHeight() - 1)
339 vpstart = al.getHeight() - h;
341 setStartEndSeq(vpstart, vpstart + h - 1);
345 * Get width of viewport in residues
347 * @return width of viewport
349 public int getViewportWidth()
351 return (endRes - startRes + 1);
355 * Get height of viewport in residues
357 * @return height of viewport
359 public int getViewportHeight()
361 return (endSeq - startSeq + 1);
365 * Scroll the viewport range vertically. Fires a property change event.
368 * true if scrolling up, false if down
370 * @return true if the scroll is valid
372 public boolean scrollUp(boolean up)
381 setStartSeq(startSeq - 1);
385 if (endSeq >= al.getHeight() - 1)
390 setStartSeq(startSeq + 1);
396 * Scroll the viewport range horizontally. Fires a property change event.
399 * true if scrolling right, false if left
401 * @return true if the scroll is valid
403 public boolean scrollRight(boolean right)
412 setStartRes(startRes - 1);
416 if (endRes > al.getWidth() - 1)
421 setStartRes(startRes + 1);
428 * Scroll a wrapped alignment so that the specified residue is visible. Fires
429 * a property change event.
432 * residue position to scroll to
434 public void scrollToWrappedVisible(int res)
436 // get the start residue of the wrapped row which res is in
437 // and set that as our start residue
438 int width = getViewportWidth();
439 setStartRes((res / width) * width);
443 * Scroll so that (x,y) is visible. Fires a property change event.
446 * x position in alignment
448 * y position in alignment
450 * viewport to be visible in. Here until hidden columns JAL-2388
451 * merged, then use alignment to get hidden cols
453 public void scrollToVisible(int x, int y, AlignViewportI av)
464 while (x < av.getColumnSelection().adjustForHiddenColumns(startRes))
466 if (!scrollRight(false))
471 while (x > av.getColumnSelection().adjustForHiddenColumns(endRes))
473 if (!scrollRight(true))
481 * Adjust sequence position for page up. Fires a property change event.
485 setViewportStartAndHeight(2 * startSeq - endSeq, getViewportHeight());
489 * Adjust sequence position for page down. Fires a property change event.
491 public void pageDown()
493 setViewportStartAndHeight(endSeq, getViewportHeight());