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 = getVisibleAlignmentWidth() - 1;
137 int oldendres = this.endRes;
147 changeSupport.firePropertyChange("startres", oldstartres, startRes);
148 if (oldstartres == startRes)
150 // event won't be fired if start positions are same
151 // fire an event for the end positions in case they changed
152 changeSupport.firePropertyChange("endres", oldendres, endRes);
157 * Set last residue visible in the viewport. Fires a property change event.
162 public void setEndRes(int res)
165 int width = getViewportWidth();
166 if (startres + width - 1 > getVisibleAlignmentWidth() - 1)
168 startres = getVisibleAlignmentWidth() - width;
170 setStartEndRes(startres - width + 1, startres);
174 * Set the first sequence visible in the viewport, maintaining the height. If
175 * the viewport would extend past the last sequence, sets the viewport so it
176 * sits at the bottom of the alignment. Fires a property change event.
181 public void setStartSeq(int seq)
184 int height = getViewportHeight();
185 if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
187 startseq = getVisibleAlignmentHeight() - height;
189 setStartEndSeq(startseq, startseq + height - 1);
193 * Set start and end sequences at the same time. The viewport height may
194 * change. This method only fires one event for the two changes, and should be
195 * used in preference to separate calls to setStartSeq and setEndSeq.
202 public void setStartEndSeq(int start, int end)
204 int oldstartseq = this.startSeq;
205 if (start > getVisibleAlignmentHeight() - 1)
207 startSeq = getVisibleAlignmentHeight() - 1;
218 int oldendseq = this.endSeq;
219 if (end >= getVisibleAlignmentHeight())
221 endSeq = getVisibleAlignmentHeight() - 1;
232 changeSupport.firePropertyChange("startseq", oldstartseq, startSeq);
233 if (oldstartseq == startSeq)
235 // event won't be fired if start positions are the same
236 // fire in case the end positions changed
237 changeSupport.firePropertyChange("endseq", oldendseq, endSeq);
242 * Set the last sequence visible in the viewport. Fires a property change
248 public void setEndSeq(int seq)
250 int height = getViewportHeight();
251 setStartEndSeq(seq - height + 1, seq);
255 * Get start residue of viewport
257 public int getStartRes()
263 * Get end residue of viewport
265 public int getEndRes()
271 * Get start sequence of viewport
273 public int getStartSeq()
279 * Get end sequence of viewport
281 public int getEndSeq()
287 * Set viewport width in residues, without changing startRes. Use in
288 * preference to calculating endRes from the width, to avoid out by one
289 * errors! Fires a property change event.
294 public void setViewportWidth(int w)
296 setStartEndRes(startRes, startRes + w - 1);
300 * Set viewport height in residues, without changing startSeq. Use in
301 * preference to calculating endSeq from the height, to avoid out by one
302 * errors! Fires a property change event.
305 * height in sequences
307 public void setViewportHeight(int h)
309 setStartEndSeq(startSeq, startSeq + h - 1);
313 * Set viewport horizontal start position and width. Use in preference to
314 * calculating endRes from the width, to avoid out by one errors! Fires a
315 * property change event.
322 public void setViewportStartAndWidth(int start, int w)
329 else if (vpstart + w - 1 > getVisibleAlignmentWidth() - 1)
331 vpstart = getVisibleAlignmentWidth() - 1;
333 setStartEndRes(vpstart, vpstart + w - 1);
337 * Set viewport vertical start position and height. Use in preference to
338 * calculating endSeq from the height, to avoid out by one errors! Fires a
339 * property change event.
344 * height in sequences
346 public void setViewportStartAndHeight(int start, int h)
353 else if (vpstart + h - 1 > getVisibleAlignmentHeight() - 1)
355 vpstart = getVisibleAlignmentHeight() - h;
357 setStartEndSeq(vpstart, vpstart + h - 1);
361 * Get width of viewport in residues
363 * @return width of viewport
365 public int getViewportWidth()
367 return (endRes - startRes + 1);
371 * Get height of viewport in residues
373 * @return height of viewport
375 public int getViewportHeight()
377 return (endSeq - startSeq + 1);
381 * Scroll the viewport range vertically. Fires a property change event.
384 * true if scrolling up, false if down
386 * @return true if the scroll is valid
388 public boolean scrollUp(boolean up)
397 setStartSeq(startSeq - 1);
401 if (endSeq >= getVisibleAlignmentHeight() - 1)
406 setStartSeq(startSeq + 1);
412 * Scroll the viewport range horizontally. Fires a property change event.
415 * true if scrolling right, false if left
417 * @return true if the scroll is valid
419 public boolean scrollRight(boolean right)
428 setStartRes(startRes - 1);
432 if (endRes > getVisibleAlignmentWidth() - 1)
437 setStartRes(startRes + 1);
444 * Scroll a wrapped alignment so that the specified residue is visible. Fires
445 * a property change event.
448 * residue position to scroll to
450 public void scrollToWrappedVisible(int res)
452 // get the start residue of the wrapped row which res is in
453 // and set that as our start residue
454 int width = getViewportWidth();
455 setStartRes((res / width) * width);
459 * Scroll so that (x,y) is visible. Fires a property change event.
462 * x position in alignment
464 * y position in alignment
466 public void scrollToVisible(int x, int y)
477 HiddenColumns hidden = al.getHiddenColumns();
478 while (x < hidden.adjustForHiddenColumns(startRes))
480 if (!scrollRight(false))
485 while (x > hidden.adjustForHiddenColumns(endRes))
487 if (!scrollRight(true))
495 * Adjust sequence position for page up. Fires a property change event.
499 setViewportStartAndHeight(2 * startSeq - endSeq, getViewportHeight());
503 * Adjust sequence position for page down. Fires a property change event.
505 public void pageDown()
507 setViewportStartAndHeight(endSeq, getViewportHeight());