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;
26 import java.util.Arrays;
29 * Supplies and updates viewport properties relating to position such as: start
30 * and end residues and sequences; ideally will serve hidden columns/rows too.
31 * Intention also to support calculations for positioning, scrolling etc. such
32 * as finding the middle of the viewport, checking for scrolls off screen
34 public class ViewportRanges extends ViewportProperties
36 public static final String STARTRES = "startres";
38 public static final String ENDRES = "endres";
40 public static final String STARTSEQ = "startseq";
42 public static final String ENDSEQ = "endseq";
44 public static final String STARTRESANDSEQ = "startresandseq";
46 public static final String MOVE_VIEWPORT = "move_viewport";
48 private boolean wrappedMode = false;
50 // start residue of viewport
53 // end residue of viewport
56 // start sequence of viewport
59 // end sequence of viewport
63 private AlignmentI al;
69 * the viewport's alignment
71 public ViewportRanges(AlignmentI alignment)
73 // initial values of viewport settings
75 this.endRes = alignment.getWidth() - 1;
77 this.setEndSeqTest(alignment.getHeight() - 1);
81 public static String sTest = "";
83 private void setEndSeqTest(int val)
85 String st = Thread.currentThread().toString();
86 sTest += "ViewPortRanges.setEndseqTest " + val + " "
90 sTest += Arrays.toString(new NullPointerException().getStackTrace())
97 * Get alignment width in cols, including hidden cols
99 public int getAbsoluteAlignmentWidth()
101 return al.getWidth();
105 * Get alignment height in rows, including hidden rows
107 public int getAbsoluteAlignmentHeight()
109 return al.getHeight() + al.getHiddenSequences().getSize();
113 * Get alignment width in cols, excluding hidden cols
115 public int getVisibleAlignmentWidth()
117 return al.getVisibleWidth();
121 * Get alignment height in rows, excluding hidden rows
123 public int getVisibleAlignmentHeight()
125 return al.getHeight();
129 * Set first residue visible in the viewport, and retain the current width.
130 * Fires a property change event.
135 public void setStartRes(int res)
137 int width = getViewportWidth();
138 setStartEndRes(res, res + width - 1);
142 * Set start and end residues at the same time. This method only fires one
143 * event for the two changes, and should be used in preference to separate
144 * calls to setStartRes and setEndRes.
151 public void setStartEndRes(int start, int end)
153 int[] oldvalues = updateStartEndRes(start, end);
154 int oldstartres = oldvalues[0];
155 int oldendres = oldvalues[1];
157 if (oldstartres == startRes && oldendres == endRes)
159 return; // BH 2019.07.27 standard check for no changes
162 // "STARTRES" is a misnomer here -- really "STARTORENDRES"
163 // note that this could be "no change" if the range is just being expanded
164 changeSupport.firePropertyChange(STARTRES, oldstartres, startRes);
165 if (oldstartres == startRes)
167 // No listener cares about this
168 // "ENDRES" is a misnomer here -- really "ENDONLYRES"
169 // BH 2019.07.27 adds end change check
170 // fire only if only the end is changed
171 changeSupport.firePropertyChange(ENDRES, oldendres, endRes);
176 * Update start and end residue values, adjusting for width constraints if
183 * @return array containing old start and end residue values
185 private int[] updateStartEndRes(int start, int end)
187 int oldstartres = this.startRes;
190 * if not wrapped, don't leave white space at the right margin
192 int lastColumn = getVisibleAlignmentWidth() - 1;
193 if (!wrappedMode && (start > lastColumn))
195 startRes = Math.max(lastColumn, 0);
206 int oldendres = this.endRes;
211 else if (!wrappedMode && (end > lastColumn))
213 endRes = Math.max(lastColumn, 0);
219 return new int[] { oldstartres, oldendres };
223 * Set the first sequence visible in the viewport, maintaining the height. If
224 * the viewport would extend past the last sequence, sets the viewport so it
225 * sits at the bottom of the alignment. Fires a property change event.
231 public void setStartSeq(int seq)
233 int height = getViewportHeight();
234 int startseq = Math.min(seq, getVisibleAlignmentHeight() - height);
235 // BH 2019.07.27 cosmetic only -- was:
236 // if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
238 // startseq = getVisibleAlignmentHeight() - height;
240 setStartEndSeq(startseq, startseq + height - 1);
244 * Set start and end sequences at the same time. The viewport height may
245 * change. This method only fires one event for the two changes, and should be
246 * used in preference to separate calls to setStartSeq and setEndSeq.
253 public void setStartEndSeq(int start, int end)
255 // System.out.println("ViewportRange setStartEndSeq " + start + " " + end);
256 int[] oldvalues = updateStartEndSeq(start, end);
257 int oldstartseq = oldvalues[0];
258 int oldendseq = oldvalues[1];
260 if (oldstartseq == startSeq && oldendseq == endSeq)
262 return; // BH 2019.07.27 standard check for no changes
265 // "STARTSEQ" is a misnomer here -- really "STARTORENDSEQ"
266 changeSupport.firePropertyChange(STARTSEQ, oldstartseq, startSeq);
267 if (oldstartseq == startSeq)
269 // Note that all listeners ignore this - could be removed, or there is a
271 // "ENDSEQ" is a misnomer here -- really "ENDONLYSEQ"
272 // additional fire, only if only the end is changed
273 changeSupport.firePropertyChange(ENDSEQ, oldendseq, endSeq);
278 * Update start and end sequence values, adjusting for height constraints if
285 * @return array containing old start and end sequence values
287 private int[] updateStartEndSeq(int start, int end)
289 int oldstartseq = this.startSeq;
290 int visibleHeight = getVisibleAlignmentHeight();
291 if (start > visibleHeight - 1)
293 startSeq = Math.max(visibleHeight - 1, 0);
304 int oldendseq = this.endSeq;
305 if (end >= visibleHeight)
307 setEndSeqTest(Math.max(visibleHeight - 1, 0));
317 return new int[] { oldstartseq, oldendseq };
321 * Set the last sequence visible in the viewport. Fires a property change
325 * sequence position in the range [0, height)
327 public void setEndSeq(int seq)
329 // BH 2018.04.18 added safety for seq < 0; comment about not being >= height
330 setStartEndSeq(Math.max(0, seq + 1 - getViewportHeight()), seq);
334 * Set start residue and start sequence together (fires single event). The
335 * event supplies a pair of old values and a pair of new values: [old start
336 * residue, old start sequence] and [new start residue, new start sequence]
343 public void setStartResAndSeq(int res, int seq)
345 int width = getViewportWidth();
346 int[] oldresvalues = updateStartEndRes(res, res + width - 1);
349 int height = getViewportHeight();
350 if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
352 startseq = getVisibleAlignmentHeight() - height;
354 int[] oldseqvalues = updateStartEndSeq(startseq, startseq + height - 1);
356 int[] oldvalues = new int[] { oldresvalues[0], oldseqvalues[0] };
357 int[] newvalues = new int[] { startRes, startSeq };
358 changeSupport.firePropertyChange(STARTRESANDSEQ, oldvalues, newvalues);
362 * Get start residue of viewport
364 public int getStartRes()
370 * Get end residue of viewport
372 public int getEndRes()
378 * Get start sequence of viewport
380 public int getStartSeq()
386 * Get end sequence of viewport
388 public int getEndSeq()
394 * Set viewport width in residues, without changing startRes. Use in
395 * preference to calculating endRes from the width, to avoid out by one
396 * errors! Fires a property change event.
401 public void setViewportWidth(int w)
403 setStartEndRes(startRes, startRes + w - 1);
407 * Set viewport height in residues, without changing startSeq. Use in
408 * preference to calculating endSeq from the height, to avoid out by one
409 * errors! Fires a property change event.
412 * height in sequences
414 public void setViewportHeight(int h)
416 setStartEndSeq(startSeq, startSeq + h - 1);
420 * Set viewport horizontal start position and width. Use in preference to
421 * calculating endRes from the width, to avoid out by one errors! Fires a
422 * property change event.
429 public void setViewportStartAndWidth(int start, int w)
438 * if not wrapped, don't leave white space at the right margin
442 if ((w <= getVisibleAlignmentWidth())
443 && (vpstart + w - 1 > getVisibleAlignmentWidth() - 1))
445 vpstart = getVisibleAlignmentWidth() - w;
449 setStartEndRes(vpstart, vpstart + w - 1);
453 * Set viewport vertical start position and height. Use in preference to
454 * calculating endSeq from the height, to avoid out by one errors! Fires a
455 * property change event.
460 * height in sequences
462 public void setViewportStartAndHeight(int start, int h)
466 int visHeight = getVisibleAlignmentHeight();
471 else if (h <= visHeight && vpstart + h > visHeight)
472 // viewport height is less than the full alignment and we are running off
475 vpstart = visHeight - h;
478 setStartEndSeq(vpstart, vpstart + h - 1);
482 * Get width of viewport in residues
484 * @return width of viewport
486 public int getViewportWidth()
488 return (endRes - startRes + 1);
492 * Get height of viewport in residues
494 * @return height of viewport
496 public int getViewportHeight()
498 return (endSeq - startSeq + 1);
502 * Scroll the viewport range vertically. Fires a property change event.
505 * true if scrolling up, false if down
507 * @return true if the scroll is valid
509 public boolean scrollUp(boolean up)
512 * if in unwrapped mode, scroll up or down one sequence row;
513 * if in wrapped mode, scroll by one visible width of columns
527 setStartSeq(startSeq - 1);
538 if (endSeq >= getVisibleAlignmentHeight() - 1)
542 setStartSeq(startSeq + 1);
549 * Scroll the viewport range horizontally. Fires a property change event.
552 * true if scrolling right, false if left
554 * @return true if the scroll is valid
556 public boolean scrollRight(boolean right)
565 setStartRes(startRes - 1);
569 if (endRes >= getVisibleAlignmentWidth() - 1)
574 setStartRes(startRes + 1);
581 * Scroll a wrapped alignment so that the specified residue is in the first
582 * repeat of the wrapped view. Fires a property change event. Answers true if
583 * the startRes changed, else false.
586 * residue position to scroll to NB visible position not absolute
590 public boolean scrollToWrappedVisible(int res)
592 int newStartRes = calcWrappedStartResidue(res);
593 if (newStartRes == startRes)
597 setStartRes(newStartRes);
603 * Calculate wrapped start residue from visible start residue
606 * visible start residue
607 * @return left column of panel res will be located in
609 private int calcWrappedStartResidue(int res)
611 int oldStartRes = startRes;
612 int width = getViewportWidth();
614 boolean up = res < oldStartRes;
615 int widthsToScroll = Math.abs((res - oldStartRes) / width);
621 int residuesToScroll = width * widthsToScroll;
622 int newStartRes = up ? oldStartRes - residuesToScroll : oldStartRes
632 * Scroll so that (x,y) is visible. Fires a property change event.
635 * x position in alignment (absolute position)
637 * y position in alignment (absolute position)
639 public void scrollToVisible(int x, int y)
650 HiddenColumns hidden = al.getHiddenColumns();
651 while (x < hidden.visibleToAbsoluteColumn(startRes))
653 if (!scrollRight(false))
658 while (x > hidden.visibleToAbsoluteColumn(endRes))
660 if (!scrollRight(true))
668 * Set the viewport location so that a position is visible
671 * column to be visible: absolute position in alignment
673 * row to be visible: absolute position in alignment
675 public boolean setViewportLocation(int x, int y)
677 boolean changedLocation = false;
679 // convert the x,y location to visible coordinates
680 int visX = al.getHiddenColumns().absoluteToVisibleColumn(x);
681 int visY = al.getHiddenSequences().findIndexWithoutHiddenSeqs(y);
683 // if (vis_x,vis_y) is already visible don't do anything
684 if (startRes > visX || visX > endRes
685 || startSeq > visY && visY > endSeq)
687 int[] old = new int[] { startRes, startSeq };
691 int newstartres = calcWrappedStartResidue(visX);
692 setStartRes(newstartres);
693 newresseq = new int[] { startRes, startSeq };
697 // set the viewport x location to contain vis_x
698 int newstartres = visX;
699 int width = getViewportWidth();
700 if (newstartres + width - 1 > getVisibleAlignmentWidth() - 1)
702 newstartres = getVisibleAlignmentWidth() - width;
704 updateStartEndRes(newstartres, newstartres + width - 1);
706 // set the viewport y location to contain vis_y
707 int newstartseq = visY;
708 int height = getViewportHeight();
709 if (newstartseq + height - 1 > getVisibleAlignmentHeight() - 1)
711 newstartseq = getVisibleAlignmentHeight() - height;
713 updateStartEndSeq(newstartseq, newstartseq + height - 1);
715 newresseq = new int[] { startRes, startSeq };
717 changedLocation = true;
718 changeSupport.firePropertyChange(MOVE_VIEWPORT, old, newresseq);
720 return changedLocation;
724 * Adjust sequence position for page up. Fires a property change event.
730 setStartRes(Math.max(0, getStartRes() - getViewportWidth()));
734 setViewportStartAndHeight(startSeq - (endSeq - startSeq),
735 getViewportHeight());
740 * Adjust sequence position for page down. Fires a property change event.
742 public void pageDown()
747 * if height is more than width (i.e. not all sequences fit on screen),
748 * increase page down to height
750 int newStart = getStartRes()
751 + Math.max(getViewportHeight(), getViewportWidth());
754 * don't page down beyond end of alignment, or if not all
755 * sequences fit in the visible height
757 if (newStart < getVisibleAlignmentWidth())
759 setStartRes(newStart);
764 setViewportStartAndHeight(endSeq, getViewportHeight());
768 public void setWrappedMode(boolean wrapped)
770 wrappedMode = wrapped;
773 public boolean isWrappedMode()
779 * Answers the vertical scroll position (0..) to set, given the visible column
780 * that is at top left.
784 * viewport width 40 columns (0-39, 40-79, 80-119...)
785 * column 0 returns scroll position 0
786 * columns 1-40 return scroll position 1
787 * columns 41-80 return scroll position 2
791 * @param topLeftColumn
795 public int getWrappedScrollPosition(final int topLeftColumn)
797 int w = getViewportWidth();
800 * visible whole widths
802 int scroll = topLeftColumn / w;
805 * add 1 for a part width if there is one
807 scroll += topLeftColumn % w > 0 ? 1 : 0;
813 * Answers the maximum wrapped vertical scroll value, given the column
814 * position (0..) to show at top left of the visible region.
816 * @param topLeftColumn
819 public int getWrappedMaxScroll(int topLeftColumn)
821 int scrollPosition = getWrappedScrollPosition(topLeftColumn);
824 * how many more widths could be drawn after this one?
826 int columnsRemaining = getVisibleAlignmentWidth() - topLeftColumn;
827 int width = getViewportWidth();
828 int widthsRemaining = columnsRemaining / width
829 + (columnsRemaining % width > 0 ? 1 : 0) - 1;
830 int maxScroll = scrollPosition + widthsRemaining;
836 public String toString()
838 return "[ViewportRange startRes=" + startRes + " endRes=" + endRes
839 + " startSeq=" + startSeq + " endSeq=" + endSeq + "]";