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 public static String sTest = "";
81 private void setEndSeqTest(int val)
83 sTest += "ViewPortRanges.setEndseqTest " + val + " "
84 + Thread.currentThread() + "\n";
88 * Get alignment width in cols, including hidden cols
90 public int getAbsoluteAlignmentWidth()
96 * Get alignment height in rows, including hidden rows
98 public int getAbsoluteAlignmentHeight()
100 return al.getHeight() + al.getHiddenSequences().getSize();
104 * Get alignment width in cols, excluding hidden cols
106 public int getVisibleAlignmentWidth()
108 return al.getVisibleWidth();
112 * Get alignment height in rows, excluding hidden rows
114 public int getVisibleAlignmentHeight()
116 return al.getHeight();
120 * Set first residue visible in the viewport, and retain the current width.
121 * Fires a property change event.
126 public void setStartRes(int res)
128 int width = getViewportWidth();
129 setStartEndRes(res, res + width - 1);
133 * Set start and end residues at the same time. This method only fires one
134 * event for the two changes, and should be used in preference to separate
135 * calls to setStartRes and setEndRes.
142 public void setStartEndRes(int start, int end)
144 int[] oldvalues = updateStartEndRes(start, end);
145 int oldstartres = oldvalues[0];
146 int oldendres = oldvalues[1];
148 if (oldstartres == startRes && oldendres == endRes)
150 return; // BH 2019.07.27 standard check for no changes
153 // "STARTRES" is a misnomer here -- really "STARTORENDRES"
154 // note that this could be "no change" if the range is just being expanded
155 changeSupport.firePropertyChange(STARTRES, oldstartres, startRes);
156 if (oldstartres == startRes)
158 // No listener cares about this
159 // "ENDRES" is a misnomer here -- really "ENDONLYRES"
160 // BH 2019.07.27 adds end change check
161 // fire only if only the end is changed
162 changeSupport.firePropertyChange(ENDRES, oldendres, endRes);
167 * Update start and end residue values, adjusting for width constraints if
174 * @return array containing old start and end residue values
176 private int[] updateStartEndRes(int start, int end)
178 int oldstartres = this.startRes;
181 * if not wrapped, don't leave white space at the right margin
183 int lastColumn = getVisibleAlignmentWidth() - 1;
184 if (!wrappedMode && (start > lastColumn))
186 startRes = Math.max(lastColumn, 0);
197 int oldendres = this.endRes;
202 else if (!wrappedMode && (end > lastColumn))
204 endRes = Math.max(lastColumn, 0);
210 return new int[] { oldstartres, oldendres };
214 * Set the first sequence visible in the viewport, maintaining the height. If
215 * the viewport would extend past the last sequence, sets the viewport so it
216 * sits at the bottom of the alignment. Fires a property change event.
221 public void setStartSeq(int seq)
223 int height = getViewportHeight();
224 int startseq = Math.min(seq, getVisibleAlignmentHeight() - height);
225 // BH 2019.07.27 cosmetic only -- was:
226 // if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
228 // startseq = getVisibleAlignmentHeight() - height;
230 setStartEndSeq(startseq, startseq + height - 1);
234 * Set start and end sequences at the same time. The viewport height may
235 * change. This method only fires one event for the two changes, and should be
236 * used in preference to separate calls to setStartSeq and setEndSeq.
243 public void setStartEndSeq(int start, int end)
245 // System.out.println("ViewportRange setStartEndSeq " + start + " " + end);
246 int[] oldvalues = updateStartEndSeq(start, end);
247 int oldstartseq = oldvalues[0];
248 int oldendseq = oldvalues[1];
250 if (oldstartseq == startSeq && oldendseq == endSeq)
252 return; // BH 2019.07.27 standard check for no changes
255 // "STARTSEQ" is a misnomer here -- really "STARTORENDSEQ"
256 changeSupport.firePropertyChange(STARTSEQ, oldstartseq, startSeq);
257 if (oldstartseq == startSeq)
259 // Note that all listeners ignore this - could be removed, or there is a
261 // "ENDSEQ" is a misnomer here -- really "ENDONLYSEQ"
262 // additional fire, only if only the end is changed
263 changeSupport.firePropertyChange(ENDSEQ, oldendseq, endSeq);
268 * Update start and end sequence values, adjusting for height constraints if
275 * @return array containing old start and end sequence values
277 private int[] updateStartEndSeq(int start, int end)
279 int oldstartseq = this.startSeq;
280 int visibleHeight = getVisibleAlignmentHeight();
281 if (start > visibleHeight - 1)
283 startSeq = Math.max(visibleHeight - 1, 0);
294 int oldendseq = this.endSeq;
295 if (end >= visibleHeight)
297 setEndSeqTest(Math.max(visibleHeight - 1, 0));
307 return new int[] { oldstartseq, oldendseq };
311 * Set the last sequence visible in the viewport. Fires a property change
315 * sequence position in the range [0, height)
317 public void setEndSeq(int seq)
319 // BH 2018.04.18 added safety for seq < 0; comment about not being >= height
320 setStartEndSeq(Math.max(0, seq + 1 - getViewportHeight()), seq);
324 * Set start residue and start sequence together (fires single event). The
325 * event supplies a pair of old values and a pair of new values: [old start
326 * residue, old start sequence] and [new start residue, new start sequence]
333 public void setStartResAndSeq(int res, int seq)
335 int width = getViewportWidth();
336 int[] oldresvalues = updateStartEndRes(res, res + width - 1);
339 int height = getViewportHeight();
340 if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
342 startseq = getVisibleAlignmentHeight() - height;
344 int[] oldseqvalues = updateStartEndSeq(startseq, startseq + height - 1);
346 int[] oldvalues = new int[] { oldresvalues[0], oldseqvalues[0] };
347 int[] newvalues = new int[] { startRes, startSeq };
348 changeSupport.firePropertyChange(STARTRESANDSEQ, oldvalues, newvalues);
352 * Get start residue of viewport
354 public int getStartRes()
360 * Get end residue of viewport
362 public int getEndRes()
368 * Get start sequence of viewport
370 public int getStartSeq()
376 * Get end sequence of viewport
378 public int getEndSeq()
384 * Set viewport width in residues, without changing startRes. Use in
385 * preference to calculating endRes from the width, to avoid out by one
386 * errors! Fires a property change event.
391 public void setViewportWidth(int w)
393 setStartEndRes(startRes, startRes + w - 1);
397 * Set viewport height in residues, without changing startSeq. Use in
398 * preference to calculating endSeq from the height, to avoid out by one
399 * errors! Fires a property change event.
402 * height in sequences
404 public void setViewportHeight(int h)
406 setStartEndSeq(startSeq, startSeq + h - 1);
410 * Set viewport horizontal start position and width. Use in preference to
411 * calculating endRes from the width, to avoid out by one errors! Fires a
412 * property change event.
419 public void setViewportStartAndWidth(int start, int w)
428 * if not wrapped, don't leave white space at the right margin
432 if ((w <= getVisibleAlignmentWidth())
433 && (vpstart + w - 1 > getVisibleAlignmentWidth() - 1))
435 vpstart = getVisibleAlignmentWidth() - w;
439 setStartEndRes(vpstart, vpstart + w - 1);
443 * Set viewport vertical start position and height. Use in preference to
444 * calculating endSeq from the height, to avoid out by one errors! Fires a
445 * property change event.
450 * height in sequences
452 public void setViewportStartAndHeight(int start, int h)
456 int visHeight = getVisibleAlignmentHeight();
461 else if (h <= visHeight && vpstart + h > visHeight)
462 // viewport height is less than the full alignment and we are running off
465 vpstart = visHeight - h;
468 setStartEndSeq(vpstart, vpstart + h - 1);
472 * Get width of viewport in residues
474 * @return width of viewport
476 public int getViewportWidth()
478 return (endRes - startRes + 1);
482 * Get height of viewport in residues
484 * @return height of viewport
486 public int getViewportHeight()
488 return (endSeq - startSeq + 1);
492 * Scroll the viewport range vertically. Fires a property change event.
495 * true if scrolling up, false if down
497 * @return true if the scroll is valid
499 public boolean scrollUp(boolean up)
502 * if in unwrapped mode, scroll up or down one sequence row;
503 * if in wrapped mode, scroll by one visible width of columns
517 setStartSeq(startSeq - 1);
528 if (endSeq >= getVisibleAlignmentHeight() - 1)
532 setStartSeq(startSeq + 1);
539 * Scroll the viewport range horizontally. Fires a property change event.
542 * true if scrolling right, false if left
544 * @return true if the scroll is valid
546 public boolean scrollRight(boolean right)
555 setStartRes(startRes - 1);
559 if (endRes >= getVisibleAlignmentWidth() - 1)
564 setStartRes(startRes + 1);
571 * Scroll a wrapped alignment so that the specified residue is in the first
572 * repeat of the wrapped view. Fires a property change event. Answers true if
573 * the startRes changed, else false.
576 * residue position to scroll to NB visible position not absolute
580 public boolean scrollToWrappedVisible(int res)
582 int newStartRes = calcWrappedStartResidue(res);
583 if (newStartRes == startRes)
587 setStartRes(newStartRes);
593 * Calculate wrapped start residue from visible start residue
596 * visible start residue
597 * @return left column of panel res will be located in
599 private int calcWrappedStartResidue(int res)
601 int oldStartRes = startRes;
602 int width = getViewportWidth();
604 boolean up = res < oldStartRes;
605 int widthsToScroll = Math.abs((res - oldStartRes) / width);
611 int residuesToScroll = width * widthsToScroll;
612 int newStartRes = up ? oldStartRes - residuesToScroll : oldStartRes
622 * Scroll so that (x,y) is visible. Fires a property change event.
625 * x position in alignment (absolute position)
627 * y position in alignment (absolute position)
629 public void scrollToVisible(int x, int y)
640 HiddenColumns hidden = al.getHiddenColumns();
641 while (x < hidden.visibleToAbsoluteColumn(startRes))
643 if (!scrollRight(false))
648 while (x > hidden.visibleToAbsoluteColumn(endRes))
650 if (!scrollRight(true))
658 * Set the viewport location so that a position is visible
661 * column to be visible: absolute position in alignment
663 * row to be visible: absolute position in alignment
665 public boolean setViewportLocation(int x, int y)
667 boolean changedLocation = false;
669 // convert the x,y location to visible coordinates
670 int visX = al.getHiddenColumns().absoluteToVisibleColumn(x);
671 int visY = al.getHiddenSequences().findIndexWithoutHiddenSeqs(y);
673 // if (vis_x,vis_y) is already visible don't do anything
674 if (startRes > visX || visX > endRes
675 || startSeq > visY && visY > endSeq)
677 int[] old = new int[] { startRes, startSeq };
681 int newstartres = calcWrappedStartResidue(visX);
682 setStartRes(newstartres);
683 newresseq = new int[] { startRes, startSeq };
687 // set the viewport x location to contain vis_x
688 int newstartres = visX;
689 int width = getViewportWidth();
690 if (newstartres + width - 1 > getVisibleAlignmentWidth() - 1)
692 newstartres = getVisibleAlignmentWidth() - width;
694 updateStartEndRes(newstartres, newstartres + width - 1);
696 // set the viewport y location to contain vis_y
697 int newstartseq = visY;
698 int height = getViewportHeight();
699 if (newstartseq + height - 1 > getVisibleAlignmentHeight() - 1)
701 newstartseq = getVisibleAlignmentHeight() - height;
703 updateStartEndSeq(newstartseq, newstartseq + height - 1);
705 newresseq = new int[] { startRes, startSeq };
707 changedLocation = true;
708 changeSupport.firePropertyChange(MOVE_VIEWPORT, old, newresseq);
710 return changedLocation;
714 * Adjust sequence position for page up. Fires a property change event.
720 setStartRes(Math.max(0, getStartRes() - getViewportWidth()));
724 setViewportStartAndHeight(startSeq - (endSeq - startSeq),
725 getViewportHeight());
730 * Adjust sequence position for page down. Fires a property change event.
732 public void pageDown()
737 * if height is more than width (i.e. not all sequences fit on screen),
738 * increase page down to height
740 int newStart = getStartRes()
741 + Math.max(getViewportHeight(), getViewportWidth());
744 * don't page down beyond end of alignment, or if not all
745 * sequences fit in the visible height
747 if (newStart < getVisibleAlignmentWidth())
749 setStartRes(newStart);
754 setViewportStartAndHeight(endSeq, getViewportHeight());
758 public void setWrappedMode(boolean wrapped)
760 wrappedMode = wrapped;
763 public boolean isWrappedMode()
769 * Answers the vertical scroll position (0..) to set, given the visible column
770 * that is at top left.
774 * viewport width 40 columns (0-39, 40-79, 80-119...)
775 * column 0 returns scroll position 0
776 * columns 1-40 return scroll position 1
777 * columns 41-80 return scroll position 2
781 * @param topLeftColumn
785 public int getWrappedScrollPosition(final int topLeftColumn)
787 int w = getViewportWidth();
790 * visible whole widths
792 int scroll = topLeftColumn / w;
795 * add 1 for a part width if there is one
797 scroll += topLeftColumn % w > 0 ? 1 : 0;
803 * Answers the maximum wrapped vertical scroll value, given the column
804 * position (0..) to show at top left of the visible region.
806 * @param topLeftColumn
809 public int getWrappedMaxScroll(int topLeftColumn)
811 int scrollPosition = getWrappedScrollPosition(topLeftColumn);
814 * how many more widths could be drawn after this one?
816 int columnsRemaining = getVisibleAlignmentWidth() - topLeftColumn;
817 int width = getViewportWidth();
818 int widthsRemaining = columnsRemaining / width
819 + (columnsRemaining % width > 0 ? 1 : 0) - 1;
820 int maxScroll = scrollPosition + widthsRemaining;
826 public String toString()
828 return "[ViewportRange startRes=" + startRes + " endRes=" + endRes
829 + " startSeq=" + startSeq + " endSeq=" + endSeq + "]";