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 * Slightly less embryonic class which: Supplies and updates viewport properties
28 * relating to position such as: start and end residues and sequences; ideally
29 * will serve hidden columns/rows too. Intention also to support calculations
30 * for positioning, scrolling etc. such as finding the middle of the viewport,
31 * checking for scrolls off screen
33 public class ViewportRanges extends ViewportProperties
35 public static final String STARTRES = "startres";
37 public static final String ENDRES = "endres";
39 public static final String STARTSEQ = "startseq";
41 public static final String ENDSEQ = "endseq";
43 private boolean wrappedMode = false;
45 // start residue of viewport
48 // end residue of viewport
51 // start sequence of viewport
54 // end sequence of viewport
58 private AlignmentI al;
64 * the viewport's alignment
66 public ViewportRanges(AlignmentI alignment)
68 // initial values of viewport settings
70 this.endRes = alignment.getWidth() - 1;
72 this.endSeq = alignment.getHeight() - 1;
77 * Get alignment width in cols, including hidden cols
79 public int getAbsoluteAlignmentWidth()
85 * Get alignment height in rows, including hidden rows
87 public int getAbsoluteAlignmentHeight()
89 return al.getHeight() + al.getHiddenSequences().getSize();
93 * Get alignment width in cols, excluding hidden cols
95 public int getVisibleAlignmentWidth()
97 return al.getWidth() - al.getHiddenColumns().getSize();
101 * Get alignment height in rows, excluding hidden rows
103 public int getVisibleAlignmentHeight()
105 return al.getHeight();
109 * Set first residue visible in the viewport, and retain the current width.
110 * Fires a property change event.
115 public void setStartRes(int res)
117 int width = getViewportWidth();
118 setStartEndRes(res, res + width - 1);
122 * Set start and end residues at the same time. This method only fires one
123 * event for the two changes, and should be used in preference to separate
124 * calls to setStartRes and setEndRes.
131 public void setStartEndRes(int start, int end)
133 int oldstartres = this.startRes;
136 * if not wrapped, don't leave white space at the right margin
138 int lastColumn = getVisibleAlignmentWidth() - 1;
139 if (!wrappedMode && (start > lastColumn))
141 startRes = Math.max(lastColumn, 0);
152 int oldendres = this.endRes;
157 else if (!wrappedMode && (end > lastColumn))
159 endRes = Math.max(lastColumn, 0);
166 changeSupport.firePropertyChange(STARTRES, oldstartres, startRes);
167 if (oldstartres == startRes)
169 // event won't be fired if start positions are same
170 // fire an event for the end positions in case they changed
171 changeSupport.firePropertyChange(ENDRES, oldendres, endRes);
176 * Set the first sequence visible in the viewport, maintaining the height. If
177 * the viewport would extend past the last sequence, sets the viewport so it
178 * sits at the bottom of the alignment. Fires a property change event.
183 public void setStartSeq(int seq)
186 int height = getViewportHeight();
187 if (startseq + height - 1 > getVisibleAlignmentHeight() - 1)
189 startseq = getVisibleAlignmentHeight() - height;
191 setStartEndSeq(startseq, startseq + height - 1);
195 * Set start and end sequences at the same time. The viewport height may
196 * change. This method only fires one event for the two changes, and should be
197 * used in preference to separate calls to setStartSeq and setEndSeq.
204 public void setStartEndSeq(int start, int end)
206 int oldstartseq = this.startSeq;
207 int visibleHeight = getVisibleAlignmentHeight();
208 if (start > visibleHeight - 1)
210 startSeq = Math.max(visibleHeight - 1, 0);
221 int oldendseq = this.endSeq;
222 if (end >= visibleHeight)
224 endSeq = Math.max(visibleHeight - 1, 0);
235 changeSupport.firePropertyChange(STARTSEQ, oldstartseq, startSeq);
236 if (oldstartseq == startSeq)
238 // event won't be fired if start positions are the same
239 // fire in case the end positions changed
240 changeSupport.firePropertyChange(ENDSEQ, oldendseq, endSeq);
245 * Set the last sequence visible in the viewport. Fires a property change
251 public void setEndSeq(int seq)
253 int height = getViewportHeight();
254 setStartEndSeq(seq - height + 1, seq);
258 * Get start residue of viewport
260 public int getStartRes()
266 * Get end residue of viewport
268 public int getEndRes()
274 * Get start sequence of viewport
276 public int getStartSeq()
282 * Get end sequence of viewport
284 public int getEndSeq()
290 * Set viewport width in residues, without changing startRes. Use in
291 * preference to calculating endRes from the width, to avoid out by one
292 * errors! Fires a property change event.
297 public void setViewportWidth(int w)
299 setStartEndRes(startRes, startRes + w - 1);
303 * Set viewport height in residues, without changing startSeq. Use in
304 * preference to calculating endSeq from the height, to avoid out by one
305 * errors! Fires a property change event.
308 * height in sequences
310 public void setViewportHeight(int h)
312 setStartEndSeq(startSeq, startSeq + h - 1);
316 * Set viewport horizontal start position and width. Use in preference to
317 * calculating endRes from the width, to avoid out by one errors! Fires a
318 * property change event.
325 public void setViewportStartAndWidth(int start, int w)
334 * if not wrapped, don't leave white space at the right margin
338 if ((w <= getVisibleAlignmentWidth())
339 && (vpstart + w - 1 > getVisibleAlignmentWidth() - 1))
341 vpstart = getVisibleAlignmentWidth() - w;
345 setStartEndRes(vpstart, vpstart + w - 1);
349 * Set viewport vertical start position and height. Use in preference to
350 * calculating endSeq from the height, to avoid out by one errors! Fires a
351 * property change event.
356 * height in sequences
358 public void setViewportStartAndHeight(int start, int h)
365 else if ((h <= getVisibleAlignmentHeight())
366 && (vpstart + h - 1 > getVisibleAlignmentHeight() - 1))
367 // viewport height is less than the full alignment and we are running off
370 vpstart = getVisibleAlignmentHeight() - h;
372 setStartEndSeq(vpstart, vpstart + h - 1);
376 * Get width of viewport in residues
378 * @return width of viewport
380 public int getViewportWidth()
382 return (endRes - startRes + 1);
386 * Get height of viewport in residues
388 * @return height of viewport
390 public int getViewportHeight()
392 return (endSeq - startSeq + 1);
396 * Scroll the viewport range vertically. Fires a property change event.
399 * true if scrolling up, false if down
401 * @return true if the scroll is valid
403 public boolean scrollUp(boolean up)
412 setStartSeq(startSeq - 1);
416 if (endSeq >= getVisibleAlignmentHeight() - 1)
421 setStartSeq(startSeq + 1);
427 * Scroll the viewport range horizontally. Fires a property change event.
430 * true if scrolling right, false if left
432 * @return true if the scroll is valid
434 public boolean scrollRight(boolean right)
443 setStartRes(startRes - 1);
447 if (endRes >= getVisibleAlignmentWidth() - 1)
452 setStartRes(startRes + 1);
459 * Scroll a wrapped alignment so that the specified residue is visible. Fires
460 * a property change event.
463 * residue position to scroll to
465 public void scrollToWrappedVisible(int res)
467 // get the start residue of the wrapped row which res is in
468 // and set that as our start residue
469 int width = getViewportWidth();
470 setStartRes((res / width) * width);
474 * Scroll so that (x,y) is visible. Fires a property change event.
477 * x position in alignment
479 * y position in alignment
481 public void scrollToVisible(int x, int y)
492 HiddenColumns hidden = al.getHiddenColumns();
493 while (x < hidden.adjustForHiddenColumns(startRes))
495 if (!scrollRight(false))
500 while (x > hidden.adjustForHiddenColumns(endRes))
502 if (!scrollRight(true))
510 * Adjust sequence position for page up. Fires a property change event.
516 setStartRes(Math.max(0, getStartRes() - getViewportWidth()));
520 setViewportStartAndHeight(startSeq - (endSeq - startSeq),
521 getViewportHeight());
526 * Adjust sequence position for page down. Fires a property change event.
528 public void pageDown()
533 * if height is more than width (i.e. not all sequences fit on screen),
534 * increase page down to height
536 int newStart = getStartRes()
537 + Math.max(getViewportHeight(), getViewportWidth());
540 * don't page down beyond end of alignment, or if not all
541 * sequences fit in the visible height
543 if (newStart < getVisibleAlignmentWidth())
545 setStartRes(newStart);
550 setViewportStartAndHeight(endSeq, getViewportHeight());
554 public void setWrappedMode(boolean wrapped)
556 wrappedMode = wrapped;
559 public boolean isWrappedMode()
565 * Answers the vertical scroll position (0..) to set, given the visible column
566 * that is at top left.
570 * viewport width 40 columns (0-39, 40-79, 80-119...)
571 * column 0 returns scroll position 0
572 * columns 1-40 return scroll position 1
573 * columns 41-80 return scroll position 2
577 * @param topLeftColumn
581 public int getWrappedScrollPosition(final int topLeftColumn)
583 int w = getViewportWidth();
586 * visible whole widths
588 int scroll = topLeftColumn / w;
591 * add 1 for a part width if there is one
593 scroll += topLeftColumn % w > 0 ? 1 : 0;
599 * Answers the maximum wrapped vertical scroll value, given the column
600 * position (0..) to show at top left of the visible region.
602 * @param topLeftColumn
605 public int getWrappedMaxScroll(int topLeftColumn)
607 int scrollPosition = getWrappedScrollPosition(topLeftColumn);
610 * how many more widths could be drawn after this one?
612 int columnsRemaining = getVisibleAlignmentWidth() - topLeftColumn;
613 int width = getViewportWidth();
614 int widthsRemaining = columnsRemaining / width
615 + (columnsRemaining % width > 0 ? 1 : 0) - 1;
616 int maxScroll = scrollPosition + widthsRemaining;