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 * Supplies and updates viewport properties relating to position such as: start
28 * and end residues and sequences; ideally will serve hidden columns/rows too.
29 * Intention also to support calculations for positioning, scrolling etc. such
30 * as finding the middle of the viewport, checking for scrolls off screen
32 public class ViewportRanges extends ViewportProperties
34 public static final String STARTRES = "startres";
36 public static final String ENDRES = "endres";
38 public static final String STARTSEQ = "startseq";
40 public static final String ENDSEQ = "endseq";
42 public static final String STARTRESANDSEQ = "startresandseq";
44 private boolean wrappedMode = false;
46 // start residue of viewport
49 // end residue of viewport
52 // start sequence of viewport
55 // end sequence of viewport
59 private AlignmentI al;
65 * the viewport's alignment
67 public ViewportRanges(AlignmentI alignment)
69 // initial values of viewport settings
71 this.endRes = alignment.getWidth() - 1;
73 this.endSeq = alignment.getHeight() - 1;
78 * Get alignment width in cols, including hidden cols
80 public int getAbsoluteAlignmentWidth()
86 * Get alignment height in rows, including hidden rows
88 public int getAbsoluteAlignmentHeight()
90 return al.getHeight() + al.getHiddenSequences().getSize();
94 * Get alignment width in cols, excluding hidden cols
96 public int getVisibleAlignmentWidth()
98 return al.getWidth() - al.getHiddenColumns().getSize();
102 * Get alignment height in rows, excluding hidden rows
104 public int getVisibleAlignmentHeight()
106 return al.getHeight();
110 * Set first residue visible in the viewport, and retain the current width.
111 * Fires a property change event.
116 public void setStartRes(int res)
118 int width = getViewportWidth();
119 setStartEndRes(res, res + width - 1);
123 * Set start and end residues at the same time. This method only fires one
124 * event for the two changes, and should be used in preference to separate
125 * calls to setStartRes and setEndRes.
132 public void setStartEndRes(int start, int end)
134 int[] oldvalues = updateStartEndRes(start, end);
135 int oldstartres = oldvalues[0];
136 int oldendres = oldvalues[1];
138 changeSupport.firePropertyChange(STARTRES, oldstartres, startRes);
139 if (oldstartres == startRes)
141 // event won't be fired if start positions are same
142 // fire an event for the end positions in case they changed
143 changeSupport.firePropertyChange(ENDRES, oldendres, endRes);
148 * Update start and end residue values, adjusting for width constraints if
155 * @return array containing old start and end residue values
157 private int[] updateStartEndRes(int start, int end)
159 int oldstartres = this.startRes;
162 * if not wrapped, don't leave white space at the right margin
164 int lastColumn = getVisibleAlignmentWidth() - 1;
165 if (!wrappedMode && (start > lastColumn))
167 startRes = Math.max(lastColumn, 0);
178 int oldendres = this.endRes;
183 else if (!wrappedMode && (end > lastColumn))
185 endRes = Math.max(lastColumn, 0);
191 return new int[] { oldstartres, oldendres };
195 * Set the first sequence visible in the viewport, maintaining the height. If
196 * the viewport would extend past the last sequence, sets the viewport so it
197 * sits at the bottom of the alignment. Fires a property change event.
202 public void setStartSeq(int seq)
205 int height = getViewportHeight();
206 if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
208 startseq = getVisibleAlignmentHeight() - height;
210 setStartEndSeq(startseq, startseq + height - 1);
214 * Set start and end sequences at the same time. The viewport height may
215 * change. This method only fires one event for the two changes, and should be
216 * used in preference to separate calls to setStartSeq and setEndSeq.
223 public void setStartEndSeq(int start, int end)
225 int[] oldvalues = updateStartEndSeq(start, end);
226 int oldstartseq = oldvalues[0];
227 int oldendseq = oldvalues[1];
229 changeSupport.firePropertyChange(STARTSEQ, oldstartseq, startSeq);
230 if (oldstartseq == startSeq)
232 // event won't be fired if start positions are the same
233 // fire in case the end positions changed
234 changeSupport.firePropertyChange(ENDSEQ, oldendseq, endSeq);
239 * Update start and end sequence values, adjusting for height constraints if
246 * @return array containing old start and end sequence values
248 private int[] updateStartEndSeq(int start, int end)
250 int oldstartseq = this.startSeq;
251 int visibleHeight = getVisibleAlignmentHeight();
252 if (start > visibleHeight - 1)
254 startSeq = Math.max(visibleHeight - 1, 0);
265 int oldendseq = this.endSeq;
266 if (end >= visibleHeight)
268 endSeq = Math.max(visibleHeight - 1, 0);
278 return new int[] { oldstartseq, oldendseq };
282 * Set the last sequence visible in the viewport. Fires a property change
288 public void setEndSeq(int seq)
290 int height = getViewportHeight();
291 setStartEndSeq(seq - height + 1, seq);
295 * Set start residue and start sequence together (fires single event)
302 public void setStartResAndSeq(int res, int seq)
304 int width = getViewportWidth();
305 int[] oldresvalues = updateStartEndRes(res, res + width - 1);
308 int height = getViewportHeight();
309 if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
311 startseq = getVisibleAlignmentHeight() - height;
313 int[] oldseqvalues = updateStartEndSeq(startseq, startseq + height - 1);
315 int[] old = new int[] { oldresvalues[0], oldseqvalues[0] };
316 int[] newresseq = new int[] { startRes, startSeq };
317 changeSupport.firePropertyChange(STARTRESANDSEQ, old, newresseq);
321 * Get start residue of viewport
323 public int getStartRes()
329 * Get end residue of viewport
331 public int getEndRes()
337 * Get start sequence of viewport
339 public int getStartSeq()
345 * Get end sequence of viewport
347 public int getEndSeq()
353 * Set viewport width in residues, without changing startRes. Use in
354 * preference to calculating endRes from the width, to avoid out by one
355 * errors! Fires a property change event.
360 public void setViewportWidth(int w)
362 setStartEndRes(startRes, startRes + w - 1);
366 * Set viewport height in residues, without changing startSeq. Use in
367 * preference to calculating endSeq from the height, to avoid out by one
368 * errors! Fires a property change event.
371 * height in sequences
373 public void setViewportHeight(int h)
375 setStartEndSeq(startSeq, startSeq + h - 1);
379 * Set viewport horizontal start position and width. Use in preference to
380 * calculating endRes from the width, to avoid out by one errors! Fires a
381 * property change event.
388 public void setViewportStartAndWidth(int start, int w)
397 * if not wrapped, don't leave white space at the right margin
401 if ((w <= getVisibleAlignmentWidth())
402 && (vpstart + w - 1 > getVisibleAlignmentWidth() - 1))
404 vpstart = getVisibleAlignmentWidth() - w;
408 setStartEndRes(vpstart, vpstart + w - 1);
412 * Set viewport vertical start position and height. Use in preference to
413 * calculating endSeq from the height, to avoid out by one errors! Fires a
414 * property change event.
419 * height in sequences
421 public void setViewportStartAndHeight(int start, int h)
428 else if ((h <= getVisibleAlignmentHeight())
429 && (vpstart + h - 1 > getVisibleAlignmentHeight() - 1))
430 // viewport height is less than the full alignment and we are running off
433 vpstart = getVisibleAlignmentHeight() - h;
435 setStartEndSeq(vpstart, vpstart + h - 1);
439 * Get width of viewport in residues
441 * @return width of viewport
443 public int getViewportWidth()
445 return (endRes - startRes + 1);
449 * Get height of viewport in residues
451 * @return height of viewport
453 public int getViewportHeight()
455 return (endSeq - startSeq + 1);
459 * Scroll the viewport range vertically. Fires a property change event.
462 * true if scrolling up, false if down
464 * @return true if the scroll is valid
466 public boolean scrollUp(boolean up)
469 * if in unwrapped mode, scroll up or down one sequence row;
470 * if in wrapped mode, scroll by one visible width of columns
484 setStartSeq(startSeq - 1);
495 if (endSeq >= getVisibleAlignmentHeight() - 1)
499 setStartSeq(startSeq + 1);
506 * Scroll the viewport range horizontally. Fires a property change event.
509 * true if scrolling right, false if left
511 * @return true if the scroll is valid
513 public boolean scrollRight(boolean right)
522 setStartRes(startRes - 1);
526 if (endRes >= getVisibleAlignmentWidth() - 1)
531 setStartRes(startRes + 1);
538 * Scroll a wrapped alignment so that the specified residue is in the first
539 * repeat of the wrapped view. Fires a property change event. Answers true if
540 * the startRes changed, else false.
543 * residue position to scroll to
546 public boolean scrollToWrappedVisible(int res)
548 int oldStartRes = startRes;
549 int width = getViewportWidth();
551 if (res >= oldStartRes && res < oldStartRes + width)
556 boolean up = res < oldStartRes;
557 int widthsToScroll = Math.abs((res - oldStartRes) / width);
563 int residuesToScroll = width * widthsToScroll;
564 int newStartRes = up ? oldStartRes - residuesToScroll : oldStartRes
571 setStartRes(newStartRes);
577 * Scroll so that (x,y) is visible. Fires a property change event.
580 * x position in alignment
582 * y position in alignment
584 public void scrollToVisible(int x, int y)
595 HiddenColumns hidden = al.getHiddenColumns();
596 while (x < hidden.adjustForHiddenColumns(startRes))
598 if (!scrollRight(false))
603 while (x > hidden.adjustForHiddenColumns(endRes))
605 if (!scrollRight(true))
613 * Adjust sequence position for page up. Fires a property change event.
619 setStartRes(Math.max(0, getStartRes() - getViewportWidth()));
623 setViewportStartAndHeight(startSeq - (endSeq - startSeq),
624 getViewportHeight());
629 * Adjust sequence position for page down. Fires a property change event.
631 public void pageDown()
636 * if height is more than width (i.e. not all sequences fit on screen),
637 * increase page down to height
639 int newStart = getStartRes()
640 + Math.max(getViewportHeight(), getViewportWidth());
643 * don't page down beyond end of alignment, or if not all
644 * sequences fit in the visible height
646 if (newStart < getVisibleAlignmentWidth())
648 setStartRes(newStart);
653 setViewportStartAndHeight(endSeq, getViewportHeight());
657 public void setWrappedMode(boolean wrapped)
659 wrappedMode = wrapped;
662 public boolean isWrappedMode()
668 * Answers the vertical scroll position (0..) to set, given the visible column
669 * that is at top left.
673 * viewport width 40 columns (0-39, 40-79, 80-119...)
674 * column 0 returns scroll position 0
675 * columns 1-40 return scroll position 1
676 * columns 41-80 return scroll position 2
680 * @param topLeftColumn
684 public int getWrappedScrollPosition(final int topLeftColumn)
686 int w = getViewportWidth();
689 * visible whole widths
691 int scroll = topLeftColumn / w;
694 * add 1 for a part width if there is one
696 scroll += topLeftColumn % w > 0 ? 1 : 0;
702 * Answers the maximum wrapped vertical scroll value, given the column
703 * position (0..) to show at top left of the visible region.
705 * @param topLeftColumn
708 public int getWrappedMaxScroll(int topLeftColumn)
710 int scrollPosition = getWrappedScrollPosition(topLeftColumn);
713 * how many more widths could be drawn after this one?
715 int columnsRemaining = getVisibleAlignmentWidth() - topLeftColumn;
716 int width = getViewportWidth();
717 int widthsRemaining = columnsRemaining / width
718 + (columnsRemaining % width > 0 ? 1 : 0) - 1;
719 int maxScroll = scrollPosition + widthsRemaining;