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 // 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 * Get alignment width in cols, excluding hidden cols
85 public int getVisibleAlignmentWidth()
87 return al.getWidth() - al.getHiddenColumns().getSize();
91 * Get alignment height in rows, excluding hidden rows
93 public int getVisibleAlignmentHeight()
95 return al.getHeight();
99 * Set first residue visible in the viewport, and retain the current width.
100 * Fires a property change event.
105 public void setStartRes(int res)
107 int width = getViewportWidth();
108 setStartEndRes(res, res + width - 1);
112 * Set start and end residues at the same time. This method only fires one
113 * event for the two changes, and should be used in preference to separate
114 * calls to setStartRes and setEndRes.
121 public void setStartEndRes(int start, int end)
123 int oldstartres = this.startRes;
124 if (start > getVisibleAlignmentWidth() - 1)
126 startRes = Math.max(getVisibleAlignmentWidth() - 1, 0);
137 int oldendres = this.endRes;
142 else if (end > getVisibleAlignmentWidth() - 1)
144 endRes = Math.max(getVisibleAlignmentWidth() - 1, 0);
151 changeSupport.firePropertyChange("startres", oldstartres, startRes);
152 if (oldstartres == startRes)
154 // event won't be fired if start positions are same
155 // fire an event for the end positions in case they changed
156 changeSupport.firePropertyChange("endres", oldendres, endRes);
161 * Set last residue visible in the viewport. Fires a property change event.
166 public void setEndRes(int res)
169 int width = getViewportWidth();
170 if (startres + width - 1 > getVisibleAlignmentWidth() - 1)
172 startres = getVisibleAlignmentWidth() - width;
174 setStartEndRes(startres - width + 1, startres);
178 * Set the first sequence visible in the viewport, maintaining the height. If
179 * the viewport would extend past the last sequence, sets the viewport so it
180 * sits at the bottom of the alignment. Fires a property change event.
185 public void setStartSeq(int seq)
188 int height = getViewportHeight();
189 if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
191 startseq = getVisibleAlignmentHeight() - height;
193 setStartEndSeq(startseq, startseq + height - 1);
197 * Set start and end sequences at the same time. The viewport height may
198 * change. This method only fires one event for the two changes, and should be
199 * used in preference to separate calls to setStartSeq and setEndSeq.
206 public void setStartEndSeq(int start, int end)
208 int oldstartseq = this.startSeq;
209 if (start > getVisibleAlignmentHeight() - 1)
211 startSeq = Math.max(getVisibleAlignmentHeight() - 1, 0);
222 int oldendseq = this.endSeq;
223 if (end >= getVisibleAlignmentHeight())
225 endSeq = Math.max(getVisibleAlignmentHeight() - 1, 0);
236 changeSupport.firePropertyChange("startseq", oldstartseq, startSeq);
237 if (oldstartseq == startSeq)
239 // event won't be fired if start positions are the same
240 // fire in case the end positions changed
241 changeSupport.firePropertyChange("endseq", oldendseq, endSeq);
246 * Set the last sequence visible in the viewport. Fires a property change
252 public void setEndSeq(int seq)
254 int height = getViewportHeight();
255 setStartEndSeq(seq - height + 1, seq);
259 * Get start residue of viewport
261 public int getStartRes()
267 * Get end residue of viewport
269 public int getEndRes()
275 * Get start sequence of viewport
277 public int getStartSeq()
283 * Get end sequence of viewport
285 public int getEndSeq()
291 * Set viewport width in residues, without changing startRes. Use in
292 * preference to calculating endRes from the width, to avoid out by one
293 * errors! Fires a property change event.
298 public void setViewportWidth(int w)
300 setStartEndRes(startRes, startRes + w - 1);
304 * Set viewport height in residues, without changing startSeq. Use in
305 * preference to calculating endSeq from the height, to avoid out by one
306 * errors! Fires a property change event.
309 * height in sequences
311 public void setViewportHeight(int h)
313 setStartEndSeq(startSeq, startSeq + h - 1);
317 * Set viewport horizontal start position and width. Use in preference to
318 * calculating endRes from the width, to avoid out by one errors! Fires a
319 * property change event.
326 public void setViewportStartAndWidth(int start, int w)
333 else if ((w <= getVisibleAlignmentWidth())
334 && (vpstart + w - 1 > getVisibleAlignmentWidth() - 1))
335 // viewport width is less than the full alignment and we are running off the
338 vpstart = getVisibleAlignmentWidth() - w;
340 setStartEndRes(vpstart, vpstart + w - 1);
344 * Set viewport vertical start position and height. Use in preference to
345 * calculating endSeq from the height, to avoid out by one errors! Fires a
346 * property change event.
351 * height in sequences
353 public void setViewportStartAndHeight(int start, int h)
360 else if ((h <= getVisibleAlignmentHeight())
361 && (vpstart + h - 1 > getVisibleAlignmentHeight() - 1))
362 // viewport height is less than the full alignment and we are running off
365 vpstart = getVisibleAlignmentHeight() - h;
367 setStartEndSeq(vpstart, vpstart + h - 1);
371 * Get width of viewport in residues
373 * @return width of viewport
375 public int getViewportWidth()
377 return (endRes - startRes + 1);
381 * Get height of viewport in residues
383 * @return height of viewport
385 public int getViewportHeight()
387 return (endSeq - startSeq + 1);
391 * Scroll the viewport range vertically. Fires a property change event.
394 * true if scrolling up, false if down
396 * @return true if the scroll is valid
398 public boolean scrollUp(boolean up)
407 setStartSeq(startSeq - 1);
411 if (endSeq >= getVisibleAlignmentHeight() - 1)
416 setStartSeq(startSeq + 1);
422 * Scroll the viewport range horizontally. Fires a property change event.
425 * true if scrolling right, false if left
427 * @return true if the scroll is valid
429 public boolean scrollRight(boolean right)
438 setStartRes(startRes - 1);
442 if (endRes >= getVisibleAlignmentWidth() - 1)
447 setStartRes(startRes + 1);
454 * Scroll a wrapped alignment so that the specified residue is visible. Fires
455 * a property change event.
458 * residue position to scroll to
460 public void scrollToWrappedVisible(int res)
462 // get the start residue of the wrapped row which res is in
463 // and set that as our start residue
464 int width = getViewportWidth();
465 setStartRes((res / width) * width);
469 * Scroll so that (x,y) is visible. Fires a property change event.
472 * x position in alignment
474 * y position in alignment
476 public void scrollToVisible(int x, int y)
487 HiddenColumns hidden = al.getHiddenColumns();
488 while (x < hidden.adjustForHiddenColumns(startRes))
490 if (!scrollRight(false))
495 while (x > hidden.adjustForHiddenColumns(endRes))
497 if (!scrollRight(true))
505 * Adjust sequence position for page up. Fires a property change event.
509 setViewportStartAndHeight(2 * startSeq - endSeq, getViewportHeight());
513 * Adjust sequence position for page down. Fires a property change event.
515 public void pageDown()
517 setViewportStartAndHeight(endSeq, getViewportHeight());