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.datamodel.AlignmentI;
24 import jalview.datamodel.HiddenColumns;
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 public static final String STARTRES = "startres";
37 public static final String ENDRES = "endres";
39 public static final String STARTSEQ = "startseq";
41 public static final String ENDSEQ = "endseq";
43 // start residue of viewport
46 // end residue of viewport
49 // start sequence of viewport
52 // end sequence of viewport
56 private AlignmentI al;
62 * the viewport's alignment
64 public ViewportRanges(AlignmentI alignment)
66 // initial values of viewport settings
68 this.endRes = alignment.getWidth() - 1;
70 this.endSeq = alignment.getHeight() - 1;
75 * Get alignment width in cols, including hidden cols
77 public int getAbsoluteAlignmentWidth()
83 * Get alignment height in rows, including hidden rows
85 public int getAbsoluteAlignmentHeight()
87 return al.getHeight() + al.getHiddenSequences().getSize();
91 * Get alignment width in cols, excluding hidden cols
93 public int getVisibleAlignmentWidth()
95 return al.getWidth() - al.getHiddenColumns().getSize();
99 * Get alignment height in rows, excluding hidden rows
101 public int getVisibleAlignmentHeight()
103 return al.getHeight();
107 * Set first residue visible in the viewport, and retain the current width.
108 * Fires a property change event.
113 public void setStartRes(int res)
115 int width = getViewportWidth();
116 setStartEndRes(res, res + width - 1);
120 * Set start and end residues at the same time. This method only fires one
121 * event for the two changes, and should be used in preference to separate
122 * calls to setStartRes and setEndRes.
129 public void setStartEndRes(int start, int end)
131 int oldstartres = this.startRes;
132 if (start > getVisibleAlignmentWidth() - 1)
134 startRes = Math.max(getVisibleAlignmentWidth() - 1, 0);
145 int oldendres = this.endRes;
150 else if (end > getVisibleAlignmentWidth() - 1)
152 endRes = Math.max(getVisibleAlignmentWidth() - 1, 0);
159 changeSupport.firePropertyChange(STARTRES, oldstartres, startRes);
160 if (oldstartres == startRes)
162 // event won't be fired if start positions are same
163 // fire an event for the end positions in case they changed
164 changeSupport.firePropertyChange(ENDRES, oldendres, endRes);
169 * Set last residue visible in the viewport. Fires a property change event.
174 public void setEndRes(int res)
177 int width = getViewportWidth();
178 if (startres + width - 1 > getVisibleAlignmentWidth() - 1)
180 startres = getVisibleAlignmentWidth() - width;
182 setStartEndRes(startres - width + 1, startres);
186 * Set the first sequence visible in the viewport, maintaining the height. If
187 * the viewport would extend past the last sequence, sets the viewport so it
188 * sits at the bottom of the alignment. Fires a property change event.
193 public void setStartSeq(int seq)
196 int height = getViewportHeight();
197 if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
199 startseq = getVisibleAlignmentHeight() - height;
201 setStartEndSeq(startseq, startseq + height - 1);
205 * Set start and end sequences at the same time. The viewport height may
206 * change. This method only fires one event for the two changes, and should be
207 * used in preference to separate calls to setStartSeq and setEndSeq.
214 public void setStartEndSeq(int start, int end)
216 int oldstartseq = this.startSeq;
217 if (start > getVisibleAlignmentHeight() - 1)
219 startSeq = Math.max(getVisibleAlignmentHeight() - 1, 0);
230 int oldendseq = this.endSeq;
231 if (end >= getVisibleAlignmentHeight())
233 endSeq = Math.max(getVisibleAlignmentHeight() - 1, 0);
244 changeSupport.firePropertyChange(STARTSEQ, oldstartseq, startSeq);
245 if (oldstartseq == startSeq)
247 // event won't be fired if start positions are the same
248 // fire in case the end positions changed
249 changeSupport.firePropertyChange(ENDSEQ, oldendseq, endSeq);
254 * Set the last sequence visible in the viewport. Fires a property change
260 public void setEndSeq(int seq)
262 int height = getViewportHeight();
263 setStartEndSeq(seq - height + 1, seq);
267 * Get start residue of viewport
269 public int getStartRes()
275 * Get end residue of viewport
277 public int getEndRes()
283 * Get start sequence of viewport
285 public int getStartSeq()
291 * Get end sequence of viewport
293 public int getEndSeq()
299 * Set viewport width in residues, without changing startRes. Use in
300 * preference to calculating endRes from the width, to avoid out by one
301 * errors! Fires a property change event.
306 public void setViewportWidth(int w)
308 setStartEndRes(startRes, startRes + w - 1);
312 * Set viewport height in residues, without changing startSeq. Use in
313 * preference to calculating endSeq from the height, to avoid out by one
314 * errors! Fires a property change event.
317 * height in sequences
319 public void setViewportHeight(int h)
321 setStartEndSeq(startSeq, startSeq + h - 1);
325 * Set viewport horizontal start position and width. Use in preference to
326 * calculating endRes from the width, to avoid out by one errors! Fires a
327 * property change event.
334 public void setViewportStartAndWidth(int start, int w)
341 else if ((w <= getVisibleAlignmentWidth())
342 && (vpstart + w - 1 > getVisibleAlignmentWidth() - 1))
343 // viewport width is less than the full alignment and we are running off the
346 vpstart = getVisibleAlignmentWidth() - w;
348 setStartEndRes(vpstart, vpstart + w - 1);
352 * Set viewport vertical start position and height. Use in preference to
353 * calculating endSeq from the height, to avoid out by one errors! Fires a
354 * property change event.
359 * height in sequences
361 public void setViewportStartAndHeight(int start, int h)
368 else if ((h <= getVisibleAlignmentHeight())
369 && (vpstart + h - 1 > getVisibleAlignmentHeight() - 1))
370 // viewport height is less than the full alignment and we are running off
373 vpstart = getVisibleAlignmentHeight() - h;
375 setStartEndSeq(vpstart, vpstart + h - 1);
379 * Get width of viewport in residues
381 * @return width of viewport
383 public int getViewportWidth()
385 return (endRes - startRes + 1);
389 * Get height of viewport in residues
391 * @return height of viewport
393 public int getViewportHeight()
395 return (endSeq - startSeq + 1);
399 * Scroll the viewport range vertically. Fires a property change event.
402 * true if scrolling up, false if down
404 * @return true if the scroll is valid
406 public boolean scrollUp(boolean up)
415 setStartSeq(startSeq - 1);
419 if (endSeq >= getVisibleAlignmentHeight() - 1)
424 setStartSeq(startSeq + 1);
430 * Scroll the viewport range horizontally. Fires a property change event.
433 * true if scrolling right, false if left
435 * @return true if the scroll is valid
437 public boolean scrollRight(boolean right)
446 setStartRes(startRes - 1);
450 if (endRes >= getVisibleAlignmentWidth() - 1)
455 setStartRes(startRes + 1);
462 * Scroll a wrapped alignment so that the specified residue is visible. Fires
463 * a property change event.
466 * residue position to scroll to
468 public void scrollToWrappedVisible(int res)
470 // get the start residue of the wrapped row which res is in
471 // and set that as our start residue
472 int width = getViewportWidth();
473 setStartRes((res / width) * width);
477 * Scroll so that (x,y) is visible. Fires a property change event.
480 * x position in alignment
482 * y position in alignment
484 public void scrollToVisible(int x, int y)
495 HiddenColumns hidden = al.getHiddenColumns();
496 while (x < hidden.adjustForHiddenColumns(startRes))
498 if (!scrollRight(false))
503 while (x > hidden.adjustForHiddenColumns(endRes))
505 if (!scrollRight(true))
513 * Adjust sequence position for page up. Fires a property change event.
517 setViewportStartAndHeight(2 * startSeq - endSeq, getViewportHeight());
521 * Adjust sequence position for page down. Fires a property change event.
523 public void pageDown()
525 setViewportStartAndHeight(endSeq, getViewportHeight());