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;
25 import jalview.datamodel.HiddenColumns;
28 * Slightly less embryonic class which: Supplies and updates viewport properties
29 * relating to position such as: start and end residues and sequences; ideally
30 * will serve hidden columns/rows too. Intention also to support calculations
31 * for positioning, scrolling etc. such as finding the middle of the viewport,
32 * checking for scrolls off screen
34 public class ViewportRanges extends ViewportProperties
36 // start residue of viewport
39 // end residue of viewport
42 // start sequence of viewport
45 // end sequence of viewport
49 private AlignmentI al;
55 * the viewport's alignment
57 public ViewportRanges(AlignmentI alignment)
59 // initial values of viewport settings
61 this.endRes = alignment.getWidth() - 1;
63 this.endSeq = alignment.getHeight() - 1;
68 * Get alignment width in cols, including hidden cols
70 public int getAbsoluteAlignmentWidth()
76 * Get alignment height in rows, including hidden rows
78 public int getAbsoluteAlignmentHeight()
80 return al.getHeight() + al.getHiddenSequences().getSize();
84 * Get alignment width in cols, excluding hidden cols
86 public int getVisibleAlignmentWidth()
88 return al.getWidth() - al.getHiddenColumns().getSize();
92 * Get alignment height in rows, excluding hidden rows
94 public int getVisibleAlignmentHeight()
96 return al.getHeight();
100 * Set first residue visible in the viewport, and retain the current width.
101 * Fires a property change event.
106 public void setStartRes(int res)
108 int width = getViewportWidth();
109 setStartEndRes(res, res + width - 1);
113 * Set start and end residues at the same time. This method only fires one
114 * event for the two changes, and should be used in preference to separate
115 * calls to setStartRes and setEndRes.
122 public void setStartEndRes(int start, int end)
124 int oldstartres = this.startRes;
125 if (start > al.getWidth() - 1)
127 startRes = al.getWidth() - 1;
138 int oldendres = this.endRes;
148 changeSupport.firePropertyChange("startres", oldstartres, startRes);
149 if (oldstartres == startRes)
151 // event won't be fired if start positions are same
152 // fire an event for the end positions in case they changed
153 changeSupport.firePropertyChange("endres", oldendres, endRes);
158 * Set last residue visible in the viewport. Fires a property change event.
163 public void setEndRes(int res)
166 int width = getViewportWidth();
167 if (startres + width - 1 > al.getWidth() - 1)
169 startres = al.getWidth() - width;
171 setStartEndRes(startres - width + 1, startres);
175 * Set the first sequence visible in the viewport, maintaining the height. If
176 * the viewport would extend past the last sequence, sets the viewport so it
177 * sits at the bottom of the alignment. Fires a property change event.
182 public void setStartSeq(int seq)
185 int height = getViewportHeight();
186 if (startseq + height - 1 > al.getHeight() - 1)
188 startseq = al.getHeight() - height;
190 setStartEndSeq(startseq, startseq + height - 1);
194 * Set start and end sequences at the same time. The viewport height may
195 * change. This method only fires one event for the two changes, and should be
196 * used in preference to separate calls to setStartSeq and setEndSeq.
203 public void setStartEndSeq(int start, int end)
205 int oldstartseq = this.startSeq;
206 if (start > al.getHeight() - 1)
208 startSeq = al.getHeight() - 1;
219 int oldendseq = this.endSeq;
220 if (end >= al.getHeight())
222 endSeq = al.getHeight() - 1;
233 changeSupport.firePropertyChange("startseq", oldstartseq, startSeq);
234 if (oldstartseq == startSeq)
236 // event won't be fired if start positions are the same
237 // fire in case the end positions changed
238 changeSupport.firePropertyChange("endseq", oldendseq, endSeq);
243 * Set the last sequence visible in the viewport. Fires a property change
249 public void setEndSeq(int seq)
251 int height = getViewportHeight();
252 setStartEndSeq(seq - height + 1, seq);
256 * Get start residue of viewport
258 public int getStartRes()
264 * Get end residue of viewport
266 public int getEndRes()
272 * Get start sequence of viewport
274 public int getStartSeq()
280 * Get end sequence of viewport
282 public int getEndSeq()
288 * Set viewport width in residues, without changing startRes. Use in
289 * preference to calculating endRes from the width, to avoid out by one
290 * errors! Fires a property change event.
295 public void setViewportWidth(int w)
297 setStartEndRes(startRes, startRes + w - 1);
301 * Set viewport height in residues, without changing startSeq. Use in
302 * preference to calculating endSeq from the height, to avoid out by one
303 * errors! Fires a property change event.
306 * height in sequences
308 public void setViewportHeight(int h)
310 setStartEndSeq(startSeq, startSeq + h - 1);
314 * Set viewport horizontal start position and width. Use in preference to
315 * calculating endRes from the width, to avoid out by one errors! Fires a
316 * property change event.
323 public void setViewportStartAndWidth(int start, int w)
330 else if (vpstart + w - 1 > al.getWidth() - 1)
332 vpstart = al.getWidth() - 1;
334 setStartEndRes(vpstart, vpstart + w - 1);
338 * Set viewport vertical start position and height. Use in preference to
339 * calculating endSeq from the height, to avoid out by one errors! Fires a
340 * property change event.
345 * height in sequences
347 public void setViewportStartAndHeight(int start, int h)
354 else if (vpstart + h - 1 > al.getHeight() - 1)
356 vpstart = al.getHeight() - h;
358 setStartEndSeq(vpstart, vpstart + h - 1);
362 * Get width of viewport in residues
364 * @return width of viewport
366 public int getViewportWidth()
368 return (endRes - startRes + 1);
372 * Get height of viewport in residues
374 * @return height of viewport
376 public int getViewportHeight()
378 return (endSeq - startSeq + 1);
382 * Scroll the viewport range vertically. Fires a property change event.
385 * true if scrolling up, false if down
387 * @return true if the scroll is valid
389 public boolean scrollUp(boolean up)
398 setStartSeq(startSeq - 1);
402 if (endSeq >= al.getHeight() - 1)
407 setStartSeq(startSeq + 1);
413 * Scroll the viewport range horizontally. Fires a property change event.
416 * true if scrolling right, false if left
418 * @return true if the scroll is valid
420 public boolean scrollRight(boolean right)
429 setStartRes(startRes - 1);
433 if (endRes > al.getWidth() - 1)
438 setStartRes(startRes + 1);
445 * Scroll a wrapped alignment so that the specified residue is visible. Fires
446 * a property change event.
449 * residue position to scroll to
451 public void scrollToWrappedVisible(int res)
453 // get the start residue of the wrapped row which res is in
454 // and set that as our start residue
455 int width = getViewportWidth();
456 setStartRes((res / width) * width);
460 * Scroll so that (x,y) is visible. Fires a property change event.
463 * x position in alignment
465 * y position in alignment
467 * viewport to be visible in. Here until hidden columns JAL-2388
468 * merged, then use alignment to get hidden cols
470 public void scrollToVisible(int x, int y, AlignViewportI av)
481 HiddenColumns hidden = al.getHiddenColumns();
482 while (x < hidden.adjustForHiddenColumns(startRes))
484 if (!scrollRight(false))
489 while (x > hidden.adjustForHiddenColumns(endRes))
491 if (!scrollRight(true))
499 * Adjust sequence position for page up. Fires a property change event.
503 setViewportStartAndHeight(2 * startSeq - endSeq, getViewportHeight());
507 * Adjust sequence position for page down. Fires a property change event.
509 public void pageDown()
511 setViewportStartAndHeight(endSeq, getViewportHeight());