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.getVisibleWidth();
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 // System.out.println("ViewportRange setStartEndSeq " + start + " " + end);
228 int[] oldvalues = updateStartEndSeq(start, end);
229 int oldstartseq = oldvalues[0];
230 int oldendseq = oldvalues[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 * Update start and end sequence values, adjusting for height constraints if
249 * @return array containing old start and end sequence values
251 private int[] updateStartEndSeq(int start, int end)
253 int oldstartseq = this.startSeq;
254 int visibleHeight = getVisibleAlignmentHeight();
255 if (start > visibleHeight - 1)
257 startSeq = Math.max(visibleHeight - 1, 0);
268 int oldendseq = this.endSeq;
269 if (end >= visibleHeight)
271 endSeq = Math.max(visibleHeight - 1, 0);
281 return new int[] { oldstartseq, oldendseq };
285 * Set the last sequence visible in the viewport. Fires a property change
289 * sequence position in the range [0, height)
291 public void setEndSeq(int seq)
293 // BH 2018.04.18 added safety for seq < 0; comment about not being >= height
294 setStartEndSeq(Math.max(0, seq + 1 - getViewportHeight()), seq);
298 * Set start residue and start sequence together (fires single event). The
299 * event supplies a pair of old values and a pair of new values: [old start
300 * residue, old start sequence] and [new start residue, new start sequence]
307 public void setStartResAndSeq(int res, int seq)
309 int width = getViewportWidth();
310 int[] oldresvalues = updateStartEndRes(res, res + width - 1);
313 int height = getViewportHeight();
314 if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
316 startseq = getVisibleAlignmentHeight() - height;
318 int[] oldseqvalues = updateStartEndSeq(startseq, startseq + height - 1);
320 int[] old = new int[] { oldresvalues[0], oldseqvalues[0] };
321 int[] newresseq = new int[] { startRes, startSeq };
322 changeSupport.firePropertyChange(STARTRESANDSEQ, old, newresseq);
326 * Get start residue of viewport
328 public int getStartRes()
334 * Get end residue of viewport
336 public int getEndRes()
342 * Get start sequence of viewport
344 public int getStartSeq()
350 * Get end sequence of viewport
352 public int getEndSeq()
358 * Set viewport width in residues, without changing startRes. Use in
359 * preference to calculating endRes from the width, to avoid out by one
360 * errors! Fires a property change event.
365 public void setViewportWidth(int w)
367 setStartEndRes(startRes, startRes + w - 1);
371 * Set viewport height in residues, without changing startSeq. Use in
372 * preference to calculating endSeq from the height, to avoid out by one
373 * errors! Fires a property change event.
376 * height in sequences
378 public void setViewportHeight(int h)
380 setStartEndSeq(startSeq, startSeq + h - 1);
384 * Set viewport horizontal start position and width. Use in preference to
385 * calculating endRes from the width, to avoid out by one errors! Fires a
386 * property change event.
393 public void setViewportStartAndWidth(int start, int w)
402 * if not wrapped, don't leave white space at the right margin
406 if ((w <= getVisibleAlignmentWidth())
407 && (vpstart + w - 1 > getVisibleAlignmentWidth() - 1))
409 vpstart = getVisibleAlignmentWidth() - w;
413 setStartEndRes(vpstart, vpstart + w - 1);
417 * Set viewport vertical start position and height. Use in preference to
418 * calculating endSeq from the height, to avoid out by one errors! Fires a
419 * property change event.
424 * height in sequences
426 public void setViewportStartAndHeight(int start, int h)
430 int visHeight = getVisibleAlignmentHeight();
435 else if (h <= visHeight && vpstart + h > visHeight)
436 // viewport height is less than the full alignment and we are running off
439 vpstart = visHeight - h;
441 // System.out.println("ViewportRanges setviewportStartAndHeight " + vpstart
442 // + " " + start + " " + h + " " + getVisibleAlignmentHeight());
444 setStartEndSeq(vpstart, vpstart + h - 1);
448 * Get width of viewport in residues
450 * @return width of viewport
452 public int getViewportWidth()
454 return (endRes - startRes + 1);
458 * Get height of viewport in residues
460 * @return height of viewport
462 public int getViewportHeight()
464 return (endSeq - startSeq + 1);
468 * Scroll the viewport range vertically. Fires a property change event.
471 * true if scrolling up, false if down
473 * @return true if the scroll is valid
475 public boolean scrollUp(boolean up)
478 * if in unwrapped mode, scroll up or down one sequence row;
479 * if in wrapped mode, scroll by one visible width of columns
493 setStartSeq(startSeq - 1);
504 if (endSeq >= getVisibleAlignmentHeight() - 1)
508 setStartSeq(startSeq + 1);
515 * Scroll the viewport range horizontally. Fires a property change event.
518 * true if scrolling right, false if left
520 * @return true if the scroll is valid
522 public boolean scrollRight(boolean right)
531 setStartRes(startRes - 1);
535 if (endRes >= getVisibleAlignmentWidth() - 1)
540 setStartRes(startRes + 1);
547 * Scroll a wrapped alignment so that the specified residue is in the first
548 * repeat of the wrapped view. Fires a property change event. Answers true if
549 * the startRes changed, else false.
552 * residue position to scroll to NB visible position not absolute
556 public boolean scrollToWrappedVisible(int res)
558 int newStartRes = calcWrappedStartResidue(res);
559 if (newStartRes == startRes)
563 setStartRes(newStartRes);
569 * Calculate wrapped start residue from visible start residue
572 * visible start residue
573 * @return left column of panel res will be located in
575 private int calcWrappedStartResidue(int res)
577 int oldStartRes = startRes;
578 int width = getViewportWidth();
580 boolean up = res < oldStartRes;
581 int widthsToScroll = Math.abs((res - oldStartRes) / width);
587 int residuesToScroll = width * widthsToScroll;
588 int newStartRes = up ? oldStartRes - residuesToScroll
589 : oldStartRes + residuesToScroll;
598 * Scroll so that (x,y) is visible. Fires a property change event.
601 * x position in alignment (absolute position)
603 * y position in alignment (absolute position)
605 public void scrollToVisible(int x, int y)
616 HiddenColumns hidden = al.getHiddenColumns();
617 while (x < hidden.visibleToAbsoluteColumn(startRes))
619 if (!scrollRight(false))
624 while (x > hidden.visibleToAbsoluteColumn(endRes))
626 if (!scrollRight(true))
634 * Set the viewport location so that a position is visible
637 * column to be visible: absolute position in alignment
639 * row to be visible: absolute position in alignment
641 public boolean setViewportLocation(int x, int y)
643 boolean changedLocation = false;
645 // convert the x,y location to visible coordinates
646 int visX = al.getHiddenColumns().absoluteToVisibleColumn(x);
647 int visY = al.getHiddenSequences().findIndexWithoutHiddenSeqs(y);
649 // if (vis_x,vis_y) is already visible don't do anything
650 if (startRes > visX || visX > endRes
651 || startSeq > visY && visY > endSeq)
653 int[] old = new int[] { startRes, startSeq };
657 int newstartres = calcWrappedStartResidue(visX);
658 setStartRes(newstartres);
659 newresseq = new int[] { startRes, startSeq };
663 // set the viewport x location to contain vis_x
664 int newstartres = visX;
665 int width = getViewportWidth();
666 if (newstartres + width - 1 > getVisibleAlignmentWidth() - 1)
668 newstartres = getVisibleAlignmentWidth() - width;
670 updateStartEndRes(newstartres, newstartres + width - 1);
672 // set the viewport y location to contain vis_y
673 int newstartseq = visY;
674 int height = getViewportHeight();
675 if (newstartseq + height - 1 > getVisibleAlignmentHeight() - 1)
677 newstartseq = getVisibleAlignmentHeight() - height;
679 updateStartEndSeq(newstartseq, newstartseq + height - 1);
681 newresseq = new int[] { startRes, startSeq };
683 changedLocation = true;
684 changeSupport.firePropertyChange(MOVE_VIEWPORT, old, newresseq);
686 return changedLocation;
690 * Adjust sequence position for page up. Fires a property change event.
696 setStartRes(Math.max(0, getStartRes() - getViewportWidth()));
700 setViewportStartAndHeight(startSeq - (endSeq - startSeq),
701 getViewportHeight());
706 * Adjust sequence position for page down. Fires a property change event.
708 public void pageDown()
713 * if height is more than width (i.e. not all sequences fit on screen),
714 * increase page down to height
716 int newStart = getStartRes()
717 + Math.max(getViewportHeight(), getViewportWidth());
720 * don't page down beyond end of alignment, or if not all
721 * sequences fit in the visible height
723 if (newStart < getVisibleAlignmentWidth())
725 setStartRes(newStart);
730 setViewportStartAndHeight(endSeq, getViewportHeight());
734 public void setWrappedMode(boolean wrapped)
736 wrappedMode = wrapped;
739 public boolean isWrappedMode()
745 * Answers the vertical scroll position (0..) to set, given the visible column
746 * that is at top left.
750 * viewport width 40 columns (0-39, 40-79, 80-119...)
751 * column 0 returns scroll position 0
752 * columns 1-40 return scroll position 1
753 * columns 41-80 return scroll position 2
757 * @param topLeftColumn
761 public int getWrappedScrollPosition(final int topLeftColumn)
763 int w = getViewportWidth();
766 * visible whole widths
768 int scroll = topLeftColumn / w;
771 * add 1 for a part width if there is one
773 scroll += topLeftColumn % w > 0 ? 1 : 0;
779 * Answers the maximum wrapped vertical scroll value, given the column
780 * position (0..) to show at top left of the visible region.
782 * @param topLeftColumn
785 public int getWrappedMaxScroll(int topLeftColumn)
787 int scrollPosition = getWrappedScrollPosition(topLeftColumn);
790 * how many more widths could be drawn after this one?
792 int columnsRemaining = getVisibleAlignmentWidth() - topLeftColumn;
793 int width = getViewportWidth();
794 int widthsRemaining = columnsRemaining / width
795 + (columnsRemaining % width > 0 ? 1 : 0) - 1;
796 int maxScroll = scrollPosition + widthsRemaining;