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.setEndSeqTest(alignment.getHeight() - 1);
79 private void setEndSeqTest(int val)
84 * Get alignment width in cols, including hidden cols
86 public int getAbsoluteAlignmentWidth()
92 * Get alignment height in rows, including hidden rows
94 public int getAbsoluteAlignmentHeight()
96 return al.getHeight() + al.getHiddenSequences().getSize();
100 * Get alignment width in cols, excluding hidden cols
102 public int getVisibleAlignmentWidth()
104 return al.getVisibleWidth();
108 * Get alignment height in rows, excluding hidden rows
110 public int getVisibleAlignmentHeight()
112 return al.getHeight();
116 * Set first residue visible in the viewport, and retain the current width.
117 * Fires a property change event.
122 public void setStartRes(int res)
124 int width = getViewportWidth();
125 setStartEndRes(res, res + width - 1);
129 * Set start and end residues at the same time. This method only fires one
130 * event for the two changes, and should be used in preference to separate
131 * calls to setStartRes and setEndRes.
138 public void setStartEndRes(int start, int end)
140 int[] oldvalues = updateStartEndRes(start, end);
141 int oldstartres = oldvalues[0];
142 int oldendres = oldvalues[1];
144 if (oldstartres == startRes && oldendres == endRes)
146 return; // BH 2019.07.27 standard check for no changes
149 // "STARTRES" is a misnomer here -- really "STARTORENDRES"
150 // note that this could be "no change" if the range is just being expanded
151 changeSupport.firePropertyChange(STARTRES, oldstartres, startRes);
152 if (oldstartres == startRes)
154 // No listener cares about this
155 // "ENDRES" is a misnomer here -- really "ENDONLYRES"
156 // BH 2019.07.27 adds end change check
157 // fire only if only the end is changed
158 changeSupport.firePropertyChange(ENDRES, oldendres, endRes);
163 * Update start and end residue values, adjusting for width constraints if
170 * @return array containing old start and end residue values
172 private int[] updateStartEndRes(int start, int end)
174 int oldstartres = this.startRes;
177 * if not wrapped, don't leave white space at the right margin
179 int lastColumn = getVisibleAlignmentWidth() - 1;
180 if (!wrappedMode && (start > lastColumn))
182 startRes = Math.max(lastColumn, 0);
193 int oldendres = this.endRes;
198 else if (!wrappedMode && (end > lastColumn))
200 endRes = Math.max(lastColumn, 0);
206 return new int[] { oldstartres, oldendres };
210 * Set the first sequence visible in the viewport, maintaining the height. If
211 * the viewport would extend past the last sequence, sets the viewport so it
212 * sits at the bottom of the alignment. Fires a property change event.
217 public void setStartSeq(int seq)
219 int height = getViewportHeight();
220 int startseq = Math.min(seq, getVisibleAlignmentHeight() - height);
221 // BH 2019.07.27 cosmetic only -- was:
222 // if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
224 // startseq = getVisibleAlignmentHeight() - height;
226 setStartEndSeq(startseq, startseq + height - 1);
230 * Set start and end sequences at the same time. The viewport height may
231 * change. This method only fires one event for the two changes, and should be
232 * used in preference to separate calls to setStartSeq and setEndSeq.
239 public void setStartEndSeq(int start, int end)
241 // System.out.println("ViewportRange setStartEndSeq " + start + " " + end);
242 int[] oldvalues = updateStartEndSeq(start, end);
243 int oldstartseq = oldvalues[0];
244 int oldendseq = oldvalues[1];
246 if (oldstartseq == startSeq && oldendseq == endSeq)
248 return; // BH 2019.07.27 standard check for no changes
251 // "STARTSEQ" is a misnomer here -- really "STARTORENDSEQ"
252 changeSupport.firePropertyChange(STARTSEQ, oldstartseq, startSeq);
253 if (oldstartseq == startSeq)
255 // Note that all listeners ignore this - could be removed, or there is a
257 // "ENDSEQ" is a misnomer here -- really "ENDONLYSEQ"
258 // additional fire, only if only the end is changed
259 changeSupport.firePropertyChange(ENDSEQ, oldendseq, endSeq);
264 * Update start and end sequence values, adjusting for height constraints if
271 * @return array containing old start and end sequence values
273 private int[] updateStartEndSeq(int start, int end)
275 int oldstartseq = this.startSeq;
276 int visibleHeight = getVisibleAlignmentHeight();
277 if (start > visibleHeight - 1)
279 startSeq = Math.max(visibleHeight - 1, 0);
290 int oldendseq = this.endSeq;
291 if (end >= visibleHeight)
293 setEndSeqTest(Math.max(visibleHeight - 1, 0));
303 return new int[] { oldstartseq, oldendseq };
307 * Set the last sequence visible in the viewport. Fires a property change
311 * sequence position in the range [0, height)
313 public void setEndSeq(int seq)
315 // BH 2018.04.18 added safety for seq < 0; comment about not being >= height
316 setStartEndSeq(Math.max(0, seq + 1 - getViewportHeight()), seq);
320 * Set start residue and start sequence together (fires single event). The
321 * event supplies a pair of old values and a pair of new values: [old start
322 * residue, old start sequence] and [new start residue, new start sequence]
329 public void setStartResAndSeq(int res, int seq)
331 int width = getViewportWidth();
332 int[] oldresvalues = updateStartEndRes(res, res + width - 1);
335 int height = getViewportHeight();
336 if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
338 startseq = getVisibleAlignmentHeight() - height;
340 int[] oldseqvalues = updateStartEndSeq(startseq, startseq + height - 1);
342 int[] oldvalues = new int[] { oldresvalues[0], oldseqvalues[0] };
343 int[] newvalues = new int[] { startRes, startSeq };
344 changeSupport.firePropertyChange(STARTRESANDSEQ, oldvalues, newvalues);
348 * Get start residue of viewport
350 public int getStartRes()
356 * Get end residue of viewport
358 public int getEndRes()
364 * Get start sequence of viewport
366 public int getStartSeq()
372 * Get end sequence of viewport
374 public int getEndSeq()
380 * Set viewport width in residues, without changing startRes. Use in
381 * preference to calculating endRes from the width, to avoid out by one
382 * errors! Fires a property change event.
387 public void setViewportWidth(int w)
389 setStartEndRes(startRes, startRes + w - 1);
393 * Set viewport height in residues, without changing startSeq. Use in
394 * preference to calculating endSeq from the height, to avoid out by one
395 * errors! Fires a property change event.
398 * height in sequences
400 public void setViewportHeight(int h)
402 setStartEndSeq(startSeq, startSeq + h - 1);
406 * Set viewport horizontal start position and width. Use in preference to
407 * calculating endRes from the width, to avoid out by one errors! Fires a
408 * property change event.
415 public void setViewportStartAndWidth(int start, int w)
424 * if not wrapped, don't leave white space at the right margin
428 if ((w <= getVisibleAlignmentWidth())
429 && (vpstart + w - 1 > getVisibleAlignmentWidth() - 1))
431 vpstart = getVisibleAlignmentWidth() - w;
435 setStartEndRes(vpstart, vpstart + w - 1);
439 * Set viewport vertical start position and height. Use in preference to
440 * calculating endSeq from the height, to avoid out by one errors! Fires a
441 * property change event.
446 * height in sequences
448 public void setViewportStartAndHeight(int start, int h)
452 int visHeight = getVisibleAlignmentHeight();
457 else if (h <= visHeight && vpstart + h > visHeight)
458 // viewport height is less than the full alignment and we are running off
461 vpstart = visHeight - h;
464 setStartEndSeq(vpstart, vpstart + h - 1);
468 * Get width of viewport in residues
470 * @return width of viewport
472 public int getViewportWidth()
474 return (endRes - startRes + 1);
478 * Get height of viewport in residues
480 * @return height of viewport
482 public int getViewportHeight()
484 return (endSeq - startSeq + 1);
488 * Scroll the viewport range vertically. Fires a property change event.
491 * true if scrolling up, false if down
493 * @return true if the scroll is valid
495 public boolean scrollUp(boolean up)
498 * if in unwrapped mode, scroll up or down one sequence row;
499 * if in wrapped mode, scroll by one visible width of columns
513 setStartSeq(startSeq - 1);
524 if (endSeq >= getVisibleAlignmentHeight() - 1)
528 setStartSeq(startSeq + 1);
535 * Scroll the viewport range horizontally. Fires a property change event.
538 * true if scrolling right, false if left
540 * @return true if the scroll is valid
542 public boolean scrollRight(boolean right)
551 setStartRes(startRes - 1);
555 if (endRes >= getVisibleAlignmentWidth() - 1)
560 setStartRes(startRes + 1);
567 * Scroll a wrapped alignment so that the specified residue is in the first
568 * repeat of the wrapped view. Fires a property change event. Answers true if
569 * the startRes changed, else false.
572 * residue position to scroll to NB visible position not absolute
576 public boolean scrollToWrappedVisible(int res)
578 int newStartRes = calcWrappedStartResidue(res);
579 if (newStartRes == startRes)
583 setStartRes(newStartRes);
589 * Calculate wrapped start residue from visible start residue
592 * visible start residue
593 * @return left column of panel res will be located in
595 private int calcWrappedStartResidue(int res)
597 int oldStartRes = startRes;
598 int width = getViewportWidth();
600 boolean up = res < oldStartRes;
601 int widthsToScroll = Math.abs((res - oldStartRes) / width);
607 int residuesToScroll = width * widthsToScroll;
608 int newStartRes = up ? oldStartRes - residuesToScroll : oldStartRes
618 * Scroll so that (x,y) is visible. Fires a property change event.
621 * x position in alignment (absolute position)
623 * y position in alignment (absolute position)
625 public void scrollToVisible(int x, int y)
636 HiddenColumns hidden = al.getHiddenColumns();
637 while (x < hidden.visibleToAbsoluteColumn(startRes))
639 if (!scrollRight(false))
644 while (x > hidden.visibleToAbsoluteColumn(endRes))
646 if (!scrollRight(true))
654 * Set the viewport location so that a position is visible
657 * column to be visible: absolute position in alignment
659 * row to be visible: absolute position in alignment
661 public boolean setViewportLocation(int x, int y)
663 boolean changedLocation = false;
665 // convert the x,y location to visible coordinates
666 int visX = al.getHiddenColumns().absoluteToVisibleColumn(x);
667 int visY = al.getHiddenSequences().findIndexWithoutHiddenSeqs(y);
669 // if (vis_x,vis_y) is already visible don't do anything
670 if (startRes > visX || visX > endRes
671 || startSeq > visY && visY > endSeq)
673 int[] old = new int[] { startRes, startSeq };
677 int newstartres = calcWrappedStartResidue(visX);
678 setStartRes(newstartres);
679 newresseq = new int[] { startRes, startSeq };
683 // set the viewport x location to contain vis_x
684 int newstartres = visX;
685 int width = getViewportWidth();
686 if (newstartres + width - 1 > getVisibleAlignmentWidth() - 1)
688 newstartres = getVisibleAlignmentWidth() - width;
690 updateStartEndRes(newstartres, newstartres + width - 1);
692 // set the viewport y location to contain vis_y
693 int newstartseq = visY;
694 int height = getViewportHeight();
695 if (newstartseq + height - 1 > getVisibleAlignmentHeight() - 1)
697 newstartseq = getVisibleAlignmentHeight() - height;
699 updateStartEndSeq(newstartseq, newstartseq + height - 1);
701 newresseq = new int[] { startRes, startSeq };
703 changedLocation = true;
704 changeSupport.firePropertyChange(MOVE_VIEWPORT, old, newresseq);
706 return changedLocation;
710 * Adjust sequence position for page up. Fires a property change event.
716 setStartRes(Math.max(0, getStartRes() - getViewportWidth()));
720 setViewportStartAndHeight(startSeq - (endSeq - startSeq),
721 getViewportHeight());
726 * Adjust sequence position for page down. Fires a property change event.
728 public void pageDown()
733 * if height is more than width (i.e. not all sequences fit on screen),
734 * increase page down to height
736 int newStart = getStartRes()
737 + Math.max(getViewportHeight(), getViewportWidth());
740 * don't page down beyond end of alignment, or if not all
741 * sequences fit in the visible height
743 if (newStart < getVisibleAlignmentWidth())
745 setStartRes(newStart);
750 setViewportStartAndHeight(endSeq, getViewportHeight());
754 public void setWrappedMode(boolean wrapped)
756 wrappedMode = wrapped;
759 public boolean isWrappedMode()
765 * Answers the vertical scroll position (0..) to set, given the visible column
766 * that is at top left.
770 * viewport width 40 columns (0-39, 40-79, 80-119...)
771 * column 0 returns scroll position 0
772 * columns 1-40 return scroll position 1
773 * columns 41-80 return scroll position 2
777 * @param topLeftColumn
781 public int getWrappedScrollPosition(final int topLeftColumn)
783 int w = getViewportWidth();
786 * visible whole widths
788 int scroll = topLeftColumn / w;
791 * add 1 for a part width if there is one
793 scroll += topLeftColumn % w > 0 ? 1 : 0;
799 * Answers the maximum wrapped vertical scroll value, given the column
800 * position (0..) to show at top left of the visible region.
802 * @param topLeftColumn
805 public int getWrappedMaxScroll(int topLeftColumn)
807 int scrollPosition = getWrappedScrollPosition(topLeftColumn);
810 * how many more widths could be drawn after this one?
812 int columnsRemaining = getVisibleAlignmentWidth() - topLeftColumn;
813 int width = getViewportWidth();
814 int widthsRemaining = columnsRemaining / width
815 + (columnsRemaining % width > 0 ? 1 : 0) - 1;
816 int maxScroll = scrollPosition + widthsRemaining;
822 public String toString()
824 return "[ViewportRange startRes=" + startRes + " endRes=" + endRes
825 + " startSeq=" + startSeq + " endSeq=" + endSeq + "]";