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.
230 public void setStartSeq(int seq)
232 int height = getViewportHeight();
233 int startseq = Math.min(seq, getVisibleAlignmentHeight() - height);
234 // BH 2019.07.27 cosmetic only -- was:
235 // if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
237 // startseq = getVisibleAlignmentHeight() - height;
239 setStartEndSeq(startseq, startseq + height - 1);
243 * Set start and end sequences at the same time. The viewport height may
244 * change. This method only fires one event for the two changes, and should be
245 * used in preference to separate calls to setStartSeq and setEndSeq.
252 public void setStartEndSeq(int start, int end)
254 // System.out.println("ViewportRange setStartEndSeq " + start + " " + end);
255 int[] oldvalues = updateStartEndSeq(start, end);
256 int oldstartseq = oldvalues[0];
257 int oldendseq = oldvalues[1];
259 if (oldstartseq == startSeq && oldendseq == endSeq)
261 return; // BH 2019.07.27 standard check for no changes
264 // "STARTSEQ" is a misnomer here -- really "STARTORENDSEQ"
265 changeSupport.firePropertyChange(STARTSEQ, oldstartseq, startSeq);
266 if (oldstartseq == startSeq)
268 // Note that all listeners ignore this - could be removed, or there is a
270 // "ENDSEQ" is a misnomer here -- really "ENDONLYSEQ"
271 // additional fire, only if only the end is changed
272 changeSupport.firePropertyChange(ENDSEQ, oldendseq, endSeq);
277 * Update start and end sequence values, adjusting for height constraints if
284 * @return array containing old start and end sequence values
286 private int[] updateStartEndSeq(int start, int end)
288 int oldstartseq = this.startSeq;
289 int visibleHeight = getVisibleAlignmentHeight();
290 if (start > visibleHeight - 1)
292 startSeq = Math.max(visibleHeight - 1, 0);
303 int oldendseq = this.endSeq;
304 if (end >= visibleHeight)
306 setEndSeqTest(Math.max(visibleHeight - 1, 0));
316 return new int[] { oldstartseq, oldendseq };
320 * Set the last sequence visible in the viewport. Fires a property change
324 * sequence position in the range [0, height)
326 public void setEndSeq(int seq)
328 // BH 2018.04.18 added safety for seq < 0; comment about not being >= height
329 setStartEndSeq(Math.max(0, seq + 1 - getViewportHeight()), seq);
333 * Set start residue and start sequence together (fires single event). The
334 * event supplies a pair of old values and a pair of new values: [old start
335 * residue, old start sequence] and [new start residue, new start sequence]
342 public void setStartResAndSeq(int res, int seq)
344 int width = getViewportWidth();
345 int[] oldresvalues = updateStartEndRes(res, res + width - 1);
348 int height = getViewportHeight();
349 if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
351 startseq = getVisibleAlignmentHeight() - height;
353 int[] oldseqvalues = updateStartEndSeq(startseq, startseq + height - 1);
355 int[] oldvalues = new int[] { oldresvalues[0], oldseqvalues[0] };
356 int[] newvalues = new int[] { startRes, startSeq };
357 changeSupport.firePropertyChange(STARTRESANDSEQ, oldvalues, newvalues);
361 * Get start residue of viewport
363 public int getStartRes()
369 * Get end residue of viewport
371 public int getEndRes()
377 * Get start sequence of viewport
379 public int getStartSeq()
385 * Get end sequence of viewport
387 public int getEndSeq()
393 * Set viewport width in residues, without changing startRes. Use in
394 * preference to calculating endRes from the width, to avoid out by one
395 * errors! Fires a property change event.
400 public void setViewportWidth(int w)
402 setStartEndRes(startRes, startRes + w - 1);
406 * Set viewport height in residues, without changing startSeq. Use in
407 * preference to calculating endSeq from the height, to avoid out by one
408 * errors! Fires a property change event.
411 * height in sequences
413 public void setViewportHeight(int h)
415 setStartEndSeq(startSeq, startSeq + h - 1);
419 * Set viewport horizontal start position and width. Use in preference to
420 * calculating endRes from the width, to avoid out by one errors! Fires a
421 * property change event.
428 public void setViewportStartAndWidth(int start, int w)
437 * if not wrapped, don't leave white space at the right margin
441 if ((w <= getVisibleAlignmentWidth())
442 && (vpstart + w - 1 > getVisibleAlignmentWidth() - 1))
444 vpstart = getVisibleAlignmentWidth() - w;
448 setStartEndRes(vpstart, vpstart + w - 1);
452 * Set viewport vertical start position and height. Use in preference to
453 * calculating endSeq from the height, to avoid out by one errors! Fires a
454 * property change event.
459 * height in sequences
461 public void setViewportStartAndHeight(int start, int h)
465 int visHeight = getVisibleAlignmentHeight();
470 else if (h <= visHeight && vpstart + h > visHeight)
471 // viewport height is less than the full alignment and we are running off
474 vpstart = visHeight - h;
477 setStartEndSeq(vpstart, vpstart + h - 1);
481 * Get width of viewport in residues
483 * @return width of viewport
485 public int getViewportWidth()
487 return (endRes - startRes + 1);
491 * Get height of viewport in residues
493 * @return height of viewport
495 public int getViewportHeight()
497 return (endSeq - startSeq + 1);
501 * Scroll the viewport range vertically. Fires a property change event.
504 * true if scrolling up, false if down
506 * @return true if the scroll is valid
508 public boolean scrollUp(boolean up)
511 * if in unwrapped mode, scroll up or down one sequence row;
512 * if in wrapped mode, scroll by one visible width of columns
526 setStartSeq(startSeq - 1);
537 if (endSeq >= getVisibleAlignmentHeight() - 1)
541 setStartSeq(startSeq + 1);
548 * Scroll the viewport range horizontally. Fires a property change event.
551 * true if scrolling right, false if left
553 * @return true if the scroll is valid
555 public boolean scrollRight(boolean right)
564 setStartRes(startRes - 1);
568 if (endRes >= getVisibleAlignmentWidth() - 1)
573 setStartRes(startRes + 1);
580 * Scroll a wrapped alignment so that the specified residue is in the first
581 * repeat of the wrapped view. Fires a property change event. Answers true if
582 * the startRes changed, else false.
585 * residue position to scroll to NB visible position not absolute
589 public boolean scrollToWrappedVisible(int res)
591 int newStartRes = calcWrappedStartResidue(res);
592 if (newStartRes == startRes)
596 setStartRes(newStartRes);
602 * Calculate wrapped start residue from visible start residue
605 * visible start residue
606 * @return left column of panel res will be located in
608 private int calcWrappedStartResidue(int res)
610 int oldStartRes = startRes;
611 int width = getViewportWidth();
613 boolean up = res < oldStartRes;
614 int widthsToScroll = Math.abs((res - oldStartRes) / width);
620 int residuesToScroll = width * widthsToScroll;
621 int newStartRes = up ? oldStartRes - residuesToScroll : oldStartRes
631 * Scroll so that (x,y) is visible. Fires a property change event.
634 * x position in alignment (absolute position)
636 * y position in alignment (absolute position)
638 public void scrollToVisible(int x, int y)
649 HiddenColumns hidden = al.getHiddenColumns();
650 while (x < hidden.visibleToAbsoluteColumn(startRes))
652 if (!scrollRight(false))
657 while (x > hidden.visibleToAbsoluteColumn(endRes))
659 if (!scrollRight(true))
667 * Set the viewport location so that a position is visible
670 * column to be visible: absolute position in alignment
672 * row to be visible: absolute position in alignment
674 public boolean setViewportLocation(int x, int y)
676 boolean changedLocation = false;
678 // convert the x,y location to visible coordinates
679 int visX = al.getHiddenColumns().absoluteToVisibleColumn(x);
680 int visY = al.getHiddenSequences().findIndexWithoutHiddenSeqs(y);
682 // if (vis_x,vis_y) is already visible don't do anything
683 if (startRes > visX || visX > endRes
684 || startSeq > visY && visY > endSeq)
686 int[] old = new int[] { startRes, startSeq };
690 int newstartres = calcWrappedStartResidue(visX);
691 setStartRes(newstartres);
692 newresseq = new int[] { startRes, startSeq };
696 // set the viewport x location to contain vis_x
697 int newstartres = visX;
698 int width = getViewportWidth();
699 if (newstartres + width - 1 > getVisibleAlignmentWidth() - 1)
701 newstartres = getVisibleAlignmentWidth() - width;
703 updateStartEndRes(newstartres, newstartres + width - 1);
705 // set the viewport y location to contain vis_y
706 int newstartseq = visY;
707 int height = getViewportHeight();
708 if (newstartseq + height - 1 > getVisibleAlignmentHeight() - 1)
710 newstartseq = getVisibleAlignmentHeight() - height;
712 updateStartEndSeq(newstartseq, newstartseq + height - 1);
714 newresseq = new int[] { startRes, startSeq };
716 changedLocation = true;
717 changeSupport.firePropertyChange(MOVE_VIEWPORT, old, newresseq);
719 return changedLocation;
723 * Adjust sequence position for page up. Fires a property change event.
729 setStartRes(Math.max(0, getStartRes() - getViewportWidth()));
733 setViewportStartAndHeight(startSeq - (endSeq - startSeq),
734 getViewportHeight());
739 * Adjust sequence position for page down. Fires a property change event.
741 public void pageDown()
746 * if height is more than width (i.e. not all sequences fit on screen),
747 * increase page down to height
749 int newStart = getStartRes()
750 + Math.max(getViewportHeight(), getViewportWidth());
753 * don't page down beyond end of alignment, or if not all
754 * sequences fit in the visible height
756 if (newStart < getVisibleAlignmentWidth())
758 setStartRes(newStart);
763 setViewportStartAndHeight(endSeq, getViewportHeight());
767 public void setWrappedMode(boolean wrapped)
769 wrappedMode = wrapped;
772 public boolean isWrappedMode()
778 * Answers the vertical scroll position (0..) to set, given the visible column
779 * that is at top left.
783 * viewport width 40 columns (0-39, 40-79, 80-119...)
784 * column 0 returns scroll position 0
785 * columns 1-40 return scroll position 1
786 * columns 41-80 return scroll position 2
790 * @param topLeftColumn
794 public int getWrappedScrollPosition(final int topLeftColumn)
796 int w = getViewportWidth();
799 * visible whole widths
801 int scroll = topLeftColumn / w;
804 * add 1 for a part width if there is one
806 scroll += topLeftColumn % w > 0 ? 1 : 0;
812 * Answers the maximum wrapped vertical scroll value, given the column
813 * position (0..) to show at top left of the visible region.
815 * @param topLeftColumn
818 public int getWrappedMaxScroll(int topLeftColumn)
820 int scrollPosition = getWrappedScrollPosition(topLeftColumn);
823 * how many more widths could be drawn after this one?
825 int columnsRemaining = getVisibleAlignmentWidth() - topLeftColumn;
826 int width = getViewportWidth();
827 int widthsRemaining = columnsRemaining / width
828 + (columnsRemaining % width > 0 ? 1 : 0) - 1;
829 int maxScroll = scrollPosition + widthsRemaining;
835 public String toString()
837 return "[ViewportRange startRes=" + startRes + " endRes=" + endRes
838 + " startSeq=" + startSeq + " endSeq=" + endSeq + "]";