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 if (oldstartres == startRes && oldendres == endRes)
142 return; // BH 2019.07.27 standard check for no changes
145 // "STARTRES" is a misnomer here -- really "STARTORENDRES"
146 // note that this could be "no change" if the range is just being expanded
147 changeSupport.firePropertyChange(STARTRES, oldstartres, startRes);
148 if (oldstartres == startRes)
150 // only caught in ViewportRangesTest
151 // No listener cares about this
152 // "ENDRES" is a misnomer here -- really "ENDONLYRES"
153 // BH 2019.07.27 adds end change check
154 // fire only if only the end is changed
155 // event won't be fired if start positions are same
156 // fire an event for the end positions in case they changed
157 changeSupport.firePropertyChange(ENDRES, oldendres, endRes);
162 * Update start and end residue values, adjusting for width constraints if
169 * @return array containing old start and end residue values
171 private int[] updateStartEndRes(int start, int end)
173 int oldstartres = this.startRes;
176 * if not wrapped, don't leave white space at the right margin
178 int lastColumn = getVisibleAlignmentWidth() - 1;
179 if (!wrappedMode && (start > lastColumn))
181 startRes = Math.max(lastColumn, 0);
192 int oldendres = this.endRes;
197 else if (!wrappedMode && (end > lastColumn))
199 endRes = Math.max(lastColumn, 0);
205 return new int[] { oldstartres, oldendres };
209 * Set the first sequence visible in the viewport, maintaining the height. If
210 * the viewport would extend past the last sequence, sets the viewport so it
211 * sits at the bottom of the alignment. Fires a property change event.
216 public void setStartSeq(int seq)
219 int height = getViewportHeight();
220 if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
222 startseq = getVisibleAlignmentHeight() - height;
224 setStartEndSeq(startseq, startseq + height - 1);
228 * Set start and end sequences at the same time. The viewport height may
229 * change. This method only fires one event for the two changes, and should be
230 * used in preference to separate calls to setStartSeq and setEndSeq.
237 public void setStartEndSeq(int start, int end)
239 int[] oldvalues = updateStartEndSeq(start, end);
240 int oldstartseq = oldvalues[0];
241 int oldendseq = oldvalues[1];
243 changeSupport.firePropertyChange(STARTSEQ, oldstartseq, startSeq);
244 if (oldstartseq == startSeq)
246 // event won't be fired if start positions are the same
247 // fire in case the end positions changed
248 changeSupport.firePropertyChange(ENDSEQ, oldendseq, endSeq);
253 * Update start and end sequence values, adjusting for height constraints if
260 * @return array containing old start and end sequence values
262 private int[] updateStartEndSeq(int start, int end)
265 if (end == 3 && this.endSeq == 14 || end == 13 && this.endSeq == 3) {
266 new NullPointerException().printStackTrace(System.out);
267 System.out.println("ViewportRange updateStartEndSeq " + start + " " + end + " " + Thread.currentThread());
269 int oldstartseq = this.startSeq;
270 int visibleHeight = getVisibleAlignmentHeight();
271 if (start > visibleHeight - 1)
273 startSeq = Math.max(visibleHeight - 1, 0);
284 int oldendseq = this.endSeq;
285 if (end >= visibleHeight)
287 endSeq = Math.max(visibleHeight - 1, 0);
297 return new int[] { oldstartseq, oldendseq };
301 * Set the last sequence visible in the viewport. Fires a property change
305 * sequence position in the range [0, height)
307 public void setEndSeq(int seq)
309 // BH 2018.04.18 added safety for seq < 0; comment about not being >= height
310 setStartEndSeq(Math.max(0, seq + 1 - getViewportHeight()), seq);
314 * Set start residue and start sequence together (fires single event). The
315 * event supplies a pair of old values and a pair of new values: [old start
316 * residue, old start sequence] and [new start residue, new start sequence]
323 public void setStartResAndSeq(int res, int seq)
325 int width = getViewportWidth();
326 int[] oldresvalues = updateStartEndRes(res, res + width - 1);
329 int height = getViewportHeight();
330 if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
332 startseq = getVisibleAlignmentHeight() - height;
334 int[] oldseqvalues = updateStartEndSeq(startseq, startseq + height - 1);
336 int[] old = new int[] { oldresvalues[0], oldseqvalues[0] };
337 int[] newresseq = new int[] { startRes, startSeq };
338 changeSupport.firePropertyChange(STARTRESANDSEQ, old, newresseq);
342 * Get start residue of viewport
344 public int getStartRes()
350 * Get end residue of viewport
352 public int getEndRes()
358 * Get start sequence of viewport
360 public int getStartSeq()
366 * Get end sequence of viewport
368 public int getEndSeq()
374 * Set viewport width in residues, without changing startRes. Use in
375 * preference to calculating endRes from the width, to avoid out by one
376 * errors! Fires a property change event.
381 public void setViewportWidth(int w)
383 setStartEndRes(startRes, startRes + w - 1);
387 * Set viewport height in residues, without changing startSeq. Use in
388 * preference to calculating endSeq from the height, to avoid out by one
389 * errors! Fires a property change event.
392 * height in sequences
394 public void setViewportHeight(int h)
396 setStartEndSeq(startSeq, startSeq + h - 1);
400 * Set viewport horizontal start position and width. Use in preference to
401 * calculating endRes from the width, to avoid out by one errors! Fires a
402 * property change event.
409 public void setViewportStartAndWidth(int start, int w)
418 * if not wrapped, don't leave white space at the right margin
422 if ((w <= getVisibleAlignmentWidth())
423 && (vpstart + w - 1 > getVisibleAlignmentWidth() - 1))
425 vpstart = getVisibleAlignmentWidth() - w;
429 setStartEndRes(vpstart, vpstart + w - 1);
433 * Set viewport vertical start position and height. Use in preference to
434 * calculating endSeq from the height, to avoid out by one errors! Fires a
435 * property change event.
440 * height in sequences
442 public void setViewportStartAndHeight(int start, int h)
446 int visHeight = getVisibleAlignmentHeight();
451 else if (h <= visHeight && vpstart + h > visHeight)
452 // viewport height is less than the full alignment and we are running off
455 vpstart = visHeight - h;
457 // System.out.println("ViewportRanges setviewportStartAndHeight " + vpstart
458 // + " " + start + " " + h + " " + getVisibleAlignmentHeight());
460 setStartEndSeq(vpstart, vpstart + h - 1);
464 * Get width of viewport in residues
466 * @return width of viewport
468 public int getViewportWidth()
470 return (endRes - startRes + 1);
474 * Get height of viewport in residues
476 * @return height of viewport
478 public int getViewportHeight()
480 return (endSeq - startSeq + 1);
484 * Scroll the viewport range vertically. Fires a property change event.
487 * true if scrolling up, false if down
489 * @return true if the scroll is valid
491 public boolean scrollUp(boolean up)
494 * if in unwrapped mode, scroll up or down one sequence row;
495 * if in wrapped mode, scroll by one visible width of columns
509 setStartSeq(startSeq - 1);
520 if (endSeq >= getVisibleAlignmentHeight() - 1)
524 setStartSeq(startSeq + 1);
531 * Scroll the viewport range horizontally. Fires a property change event.
534 * true if scrolling right, false if left
536 * @return true if the scroll is valid
538 public boolean scrollRight(boolean right)
547 setStartRes(startRes - 1);
551 if (endRes >= getVisibleAlignmentWidth() - 1)
556 setStartRes(startRes + 1);
563 * Scroll a wrapped alignment so that the specified residue is in the first
564 * repeat of the wrapped view. Fires a property change event. Answers true if
565 * the startRes changed, else false.
568 * residue position to scroll to NB visible position not absolute
572 public boolean scrollToWrappedVisible(int res)
574 int newStartRes = calcWrappedStartResidue(res);
575 if (newStartRes == startRes)
579 setStartRes(newStartRes);
585 * Calculate wrapped start residue from visible start residue
588 * visible start residue
589 * @return left column of panel res will be located in
591 private int calcWrappedStartResidue(int res)
593 int oldStartRes = startRes;
594 int width = getViewportWidth();
596 boolean up = res < oldStartRes;
597 int widthsToScroll = Math.abs((res - oldStartRes) / width);
603 int residuesToScroll = width * widthsToScroll;
604 int newStartRes = up ? oldStartRes - residuesToScroll : oldStartRes
614 * Scroll so that (x,y) is visible. Fires a property change event.
617 * x position in alignment (absolute position)
619 * y position in alignment (absolute position)
621 public void scrollToVisible(int x, int y)
632 HiddenColumns hidden = al.getHiddenColumns();
633 while (x < hidden.visibleToAbsoluteColumn(startRes))
635 if (!scrollRight(false))
640 while (x > hidden.visibleToAbsoluteColumn(endRes))
642 if (!scrollRight(true))
650 * Set the viewport location so that a position is visible
653 * column to be visible: absolute position in alignment
655 * row to be visible: absolute position in alignment
657 public boolean setViewportLocation(int x, int y)
659 boolean changedLocation = false;
661 // convert the x,y location to visible coordinates
662 int visX = al.getHiddenColumns().absoluteToVisibleColumn(x);
663 int visY = al.getHiddenSequences().findIndexWithoutHiddenSeqs(y);
665 // if (vis_x,vis_y) is already visible don't do anything
666 if (startRes > visX || visX > endRes
667 || startSeq > visY && visY > endSeq)
669 int[] old = new int[] { startRes, startSeq };
673 int newstartres = calcWrappedStartResidue(visX);
674 setStartRes(newstartres);
675 newresseq = new int[] { startRes, startSeq };
679 // set the viewport x location to contain vis_x
680 int newstartres = visX;
681 int width = getViewportWidth();
682 if (newstartres + width - 1 > getVisibleAlignmentWidth() - 1)
684 newstartres = getVisibleAlignmentWidth() - width;
686 updateStartEndRes(newstartres, newstartres + width - 1);
688 // set the viewport y location to contain vis_y
689 int newstartseq = visY;
690 int height = getViewportHeight();
691 if (newstartseq + height - 1 > getVisibleAlignmentHeight() - 1)
693 newstartseq = getVisibleAlignmentHeight() - height;
695 updateStartEndSeq(newstartseq, newstartseq + height - 1);
697 newresseq = new int[] { startRes, startSeq };
699 changedLocation = true;
700 changeSupport.firePropertyChange(MOVE_VIEWPORT, old, newresseq);
702 return changedLocation;
706 * Adjust sequence position for page up. Fires a property change event.
712 setStartRes(Math.max(0, getStartRes() - getViewportWidth()));
716 setViewportStartAndHeight(startSeq - (endSeq - startSeq),
717 getViewportHeight());
722 * Adjust sequence position for page down. Fires a property change event.
724 public void pageDown()
729 * if height is more than width (i.e. not all sequences fit on screen),
730 * increase page down to height
732 int newStart = getStartRes()
733 + Math.max(getViewportHeight(), getViewportWidth());
736 * don't page down beyond end of alignment, or if not all
737 * sequences fit in the visible height
739 if (newStart < getVisibleAlignmentWidth())
741 setStartRes(newStart);
746 setViewportStartAndHeight(endSeq, getViewportHeight());
750 public void setWrappedMode(boolean wrapped)
752 wrappedMode = wrapped;
755 public boolean isWrappedMode()
761 * Answers the vertical scroll position (0..) to set, given the visible column
762 * that is at top left.
766 * viewport width 40 columns (0-39, 40-79, 80-119...)
767 * column 0 returns scroll position 0
768 * columns 1-40 return scroll position 1
769 * columns 41-80 return scroll position 2
773 * @param topLeftColumn
777 public int getWrappedScrollPosition(final int topLeftColumn)
779 int w = getViewportWidth();
782 * visible whole widths
784 int scroll = topLeftColumn / w;
787 * add 1 for a part width if there is one
789 scroll += topLeftColumn % w > 0 ? 1 : 0;
795 * Answers the maximum wrapped vertical scroll value, given the column
796 * position (0..) to show at top left of the visible region.
798 * @param topLeftColumn
801 public int getWrappedMaxScroll(int topLeftColumn)
803 int scrollPosition = getWrappedScrollPosition(topLeftColumn);
806 * how many more widths could be drawn after this one?
808 int columnsRemaining = getVisibleAlignmentWidth() - topLeftColumn;
809 int width = getViewportWidth();
810 int widthsRemaining = columnsRemaining / width
811 + (columnsRemaining % width > 0 ? 1 : 0) - 1;
812 int maxScroll = scrollPosition + widthsRemaining;
819 public String toString() {
820 return "[ViewportRange " + startSeq + "-" + endSeq + ", "+ startRes + "-" + endRes + "]";