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 public static final String MOVE_VIEWPORT = "move_viewport";
46 private boolean wrappedMode = false;
48 // start residue of viewport
51 // end residue of viewport
54 // start sequence of viewport
57 // end sequence of viewport
61 private AlignmentI al;
67 * the viewport's alignment
69 public ViewportRanges(AlignmentI alignment)
71 // initial values of viewport settings
73 this.endRes = alignment.getWidth() - 1;
75 this.endSeq = alignment.getHeight() - 1;
80 * Get alignment width in cols, including hidden cols
82 public int getAbsoluteAlignmentWidth()
88 * Get alignment height in rows, including hidden rows
90 public int getAbsoluteAlignmentHeight()
92 return al.getHeight() + al.getHiddenSequences().getSize();
96 * Get alignment width in cols, excluding hidden cols
98 public int getVisibleAlignmentWidth()
100 return al.getWidth() - al.getHiddenColumns().getSize();
104 * Get alignment height in rows, excluding hidden rows
106 public int getVisibleAlignmentHeight()
108 return al.getHeight();
112 * Set first residue visible in the viewport, and retain the current width.
113 * Fires a property change event.
118 public void setStartRes(int res)
120 int width = getViewportWidth();
121 setStartEndRes(res, res + width - 1);
125 * Set start and end residues at the same time. This method only fires one
126 * event for the two changes, and should be used in preference to separate
127 * calls to setStartRes and setEndRes.
134 public void setStartEndRes(int start, int end)
136 int[] oldvalues = updateStartEndRes(start, end);
137 int oldstartres = oldvalues[0];
138 int oldendres = oldvalues[1];
140 changeSupport.firePropertyChange(STARTRES, oldstartres, startRes);
141 if (oldstartres == startRes)
143 // event won't be fired if start positions are same
144 // fire an event for the end positions in case they changed
145 changeSupport.firePropertyChange(ENDRES, oldendres, endRes);
150 * Update start and end residue values, adjusting for width constraints if
157 * @return array containing old start and end residue values
159 private int[] updateStartEndRes(int start, int end)
161 int oldstartres = this.startRes;
164 * if not wrapped, don't leave white space at the right margin
166 int lastColumn = getVisibleAlignmentWidth() - 1;
167 if (!wrappedMode && (start > lastColumn))
169 startRes = Math.max(lastColumn, 0);
180 int oldendres = this.endRes;
185 else if (!wrappedMode && (end > lastColumn))
187 endRes = Math.max(lastColumn, 0);
193 return new int[] { oldstartres, oldendres };
197 * Set the first sequence visible in the viewport, maintaining the height. If
198 * the viewport would extend past the last sequence, sets the viewport so it
199 * sits at the bottom of the alignment. Fires a property change event.
204 public void setStartSeq(int seq)
207 int height = getViewportHeight();
208 if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
210 startseq = getVisibleAlignmentHeight() - height;
212 setStartEndSeq(startseq, startseq + height - 1);
216 * Set start and end sequences at the same time. The viewport height may
217 * change. This method only fires one event for the two changes, and should be
218 * used in preference to separate calls to setStartSeq and setEndSeq.
225 public void setStartEndSeq(int start, int end)
227 int[] oldvalues = updateStartEndSeq(start, end);
228 int oldstartseq = oldvalues[0];
229 int oldendseq = oldvalues[1];
231 changeSupport.firePropertyChange(STARTSEQ, oldstartseq, startSeq);
232 if (oldstartseq == startSeq)
234 // event won't be fired if start positions are the same
235 // fire in case the end positions changed
236 changeSupport.firePropertyChange(ENDSEQ, oldendseq, endSeq);
241 * Update start and end sequence values, adjusting for height constraints if
248 * @return array containing old start and end sequence values
250 private int[] updateStartEndSeq(int start, int end)
252 int oldstartseq = this.startSeq;
253 int visibleHeight = getVisibleAlignmentHeight();
254 if (start > visibleHeight - 1)
256 startSeq = Math.max(visibleHeight - 1, 0);
267 int oldendseq = this.endSeq;
268 if (end >= visibleHeight)
270 endSeq = Math.max(visibleHeight - 1, 0);
280 return new int[] { oldstartseq, oldendseq };
284 * Set the last sequence visible in the viewport. Fires a property change
290 public void setEndSeq(int seq)
292 int height = getViewportHeight();
293 setStartEndSeq(seq - height + 1, seq);
297 * Set start residue and start sequence together (fires single event). The
298 * event supplies a pair of old values and a pair of new values: [old start
299 * residue, old start sequence] and [new start residue, new start sequence]
306 public void setStartResAndSeq(int res, int seq)
308 int width = getViewportWidth();
309 int[] oldresvalues = updateStartEndRes(res, res + width - 1);
312 int height = getViewportHeight();
313 if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
315 startseq = getVisibleAlignmentHeight() - height;
317 int[] oldseqvalues = updateStartEndSeq(startseq, startseq + height - 1);
319 int[] old = new int[] { oldresvalues[0], oldseqvalues[0] };
320 int[] newresseq = new int[] { startRes, startSeq };
321 changeSupport.firePropertyChange(STARTRESANDSEQ, old, newresseq);
325 * Get start residue of viewport
327 public int getStartRes()
333 * Get end residue of viewport
335 public int getEndRes()
341 * Get start sequence of viewport
343 public int getStartSeq()
349 * Get end sequence of viewport
351 public int getEndSeq()
357 * Set viewport width in residues, without changing startRes. Use in
358 * preference to calculating endRes from the width, to avoid out by one
359 * errors! Fires a property change event.
364 public void setViewportWidth(int w)
366 setStartEndRes(startRes, startRes + w - 1);
370 * Set viewport height in residues, without changing startSeq. Use in
371 * preference to calculating endSeq from the height, to avoid out by one
372 * errors! Fires a property change event.
375 * height in sequences
377 public void setViewportHeight(int h)
379 setStartEndSeq(startSeq, startSeq + h - 1);
383 * Set viewport horizontal start position and width. Use in preference to
384 * calculating endRes from the width, to avoid out by one errors! Fires a
385 * property change event.
392 public void setViewportStartAndWidth(int start, int w)
401 * if not wrapped, don't leave white space at the right margin
405 if ((w <= getVisibleAlignmentWidth())
406 && (vpstart + w - 1 > getVisibleAlignmentWidth() - 1))
408 vpstart = getVisibleAlignmentWidth() - w;
412 setStartEndRes(vpstart, vpstart + w - 1);
416 * Set viewport vertical start position and height. Use in preference to
417 * calculating endSeq from the height, to avoid out by one errors! Fires a
418 * property change event.
423 * height in sequences
425 public void setViewportStartAndHeight(int start, int h)
432 else if ((h <= getVisibleAlignmentHeight())
433 && (vpstart + h - 1 > getVisibleAlignmentHeight() - 1))
434 // viewport height is less than the full alignment and we are running off
437 vpstart = getVisibleAlignmentHeight() - h;
439 setStartEndSeq(vpstart, vpstart + h - 1);
443 * Get width of viewport in residues
445 * @return width of viewport
447 public int getViewportWidth()
449 return (endRes - startRes + 1);
453 * Get height of viewport in residues
455 * @return height of viewport
457 public int getViewportHeight()
459 return (endSeq - startSeq + 1);
463 * Scroll the viewport range vertically. Fires a property change event.
466 * true if scrolling up, false if down
468 * @return true if the scroll is valid
470 public boolean scrollUp(boolean up)
473 * if in unwrapped mode, scroll up or down one sequence row;
474 * if in wrapped mode, scroll by one visible width of columns
488 setStartSeq(startSeq - 1);
499 if (endSeq >= getVisibleAlignmentHeight() - 1)
503 setStartSeq(startSeq + 1);
510 * Scroll the viewport range horizontally. Fires a property change event.
513 * true if scrolling right, false if left
515 * @return true if the scroll is valid
517 public boolean scrollRight(boolean right)
526 setStartRes(startRes - 1);
530 if (endRes >= getVisibleAlignmentWidth() - 1)
535 setStartRes(startRes + 1);
542 * Scroll a wrapped alignment so that the specified residue is in the first
543 * repeat of the wrapped view. Fires a property change event. Answers true if
544 * the startRes changed, else false.
547 * residue position to scroll to NB visible position not absolute
551 public boolean scrollToWrappedVisible(int res)
553 int newStartRes = calcWrappedStartResidue(res);
554 if (newStartRes == startRes)
558 setStartRes(newStartRes);
564 * Calculate wrapped start residue from visible start residue
567 * visible start residue
568 * @return left column of panel res will be located in
570 private int calcWrappedStartResidue(int res)
572 int oldStartRes = startRes;
573 int width = getViewportWidth();
575 boolean up = res < oldStartRes;
576 int widthsToScroll = Math.abs((res - oldStartRes) / width);
582 int residuesToScroll = width * widthsToScroll;
583 int newStartRes = up ? oldStartRes - residuesToScroll : oldStartRes
593 * Scroll so that (x,y) is visible. Fires a property change event.
596 * x position in alignment (absolute position)
598 * y position in alignment (absolute position)
600 public void scrollToVisible(int x, int y)
611 HiddenColumns hidden = al.getHiddenColumns();
612 while (x < hidden.adjustForHiddenColumns(startRes))
614 if (!scrollRight(false))
619 while (x > hidden.adjustForHiddenColumns(endRes))
621 if (!scrollRight(true))
629 * Set the viewport location so that a position is visible
632 * column to be visible: absolute position in alignment
634 * row to be visible: absolute position in alignment
636 public boolean setViewportLocation(int x, int y)
638 boolean changedLocation = false;
640 // convert the x,y location to visible coordinates
641 int visX = al.getHiddenColumns().findColumnPosition(x);
642 int visY = al.getHiddenSequences().findIndexWithoutHiddenSeqs(y);
644 // if (vis_x,vis_y) is already visible don't do anything
645 if (startRes > visX || visX > endRes
646 || startSeq > visY && visY > endSeq)
648 int[] old = new int[] { startRes, startSeq };
652 int newstartres = calcWrappedStartResidue(visX);
653 setStartRes(newstartres);
654 newresseq = new int[] { startRes, startSeq };
658 // set the viewport x location to contain vis_x
659 int newstartres = visX;
660 int width = getViewportWidth();
661 if (newstartres + width - 1 > getVisibleAlignmentWidth() - 1)
663 newstartres = getVisibleAlignmentWidth() - width;
665 updateStartEndRes(newstartres, newstartres + width - 1);
667 // set the viewport y location to contain vis_y
668 int newstartseq = visY;
669 int height = getViewportHeight();
670 if (newstartseq + height - 1 > getVisibleAlignmentHeight() - 1)
672 newstartseq = getVisibleAlignmentHeight() - height;
674 updateStartEndSeq(newstartseq, newstartseq + height - 1);
676 newresseq = new int[] { startRes, startSeq };
678 changedLocation = true;
679 changeSupport.firePropertyChange(MOVE_VIEWPORT, old, newresseq);
681 return changedLocation;
685 * Adjust sequence position for page up. Fires a property change event.
691 setStartRes(Math.max(0, getStartRes() - getViewportWidth()));
695 setViewportStartAndHeight(startSeq - (endSeq - startSeq),
696 getViewportHeight());
701 * Adjust sequence position for page down. Fires a property change event.
703 public void pageDown()
708 * if height is more than width (i.e. not all sequences fit on screen),
709 * increase page down to height
711 int newStart = getStartRes()
712 + Math.max(getViewportHeight(), getViewportWidth());
715 * don't page down beyond end of alignment, or if not all
716 * sequences fit in the visible height
718 if (newStart < getVisibleAlignmentWidth())
720 setStartRes(newStart);
725 setViewportStartAndHeight(endSeq, getViewportHeight());
729 public void setWrappedMode(boolean wrapped)
731 wrappedMode = wrapped;
734 public boolean isWrappedMode()
740 * Answers the vertical scroll position (0..) to set, given the visible column
741 * that is at top left.
745 * viewport width 40 columns (0-39, 40-79, 80-119...)
746 * column 0 returns scroll position 0
747 * columns 1-40 return scroll position 1
748 * columns 41-80 return scroll position 2
752 * @param topLeftColumn
756 public int getWrappedScrollPosition(final int topLeftColumn)
758 int w = getViewportWidth();
761 * visible whole widths
763 int scroll = topLeftColumn / w;
766 * add 1 for a part width if there is one
768 scroll += topLeftColumn % w > 0 ? 1 : 0;
774 * Answers the maximum wrapped vertical scroll value, given the column
775 * position (0..) to show at top left of the visible region.
777 * @param topLeftColumn
780 public int getWrappedMaxScroll(int topLeftColumn)
782 int scrollPosition = getWrappedScrollPosition(topLeftColumn);
785 * how many more widths could be drawn after this one?
787 int columnsRemaining = getVisibleAlignmentWidth() - topLeftColumn;
788 int width = getViewportWidth();
789 int widthsRemaining = columnsRemaining / width
790 + (columnsRemaining % width > 0 ? 1 : 0) - 1;
791 int maxScroll = scrollPosition + widthsRemaining;