X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fviewmodel%2FViewportRanges.java;h=4eb8c9508ae9bb9547c6b52219317c8eaee7be5a;hb=76b5df40b2ae218a21d54b30bf726684fe660971;hp=0583a8d00e5bda254030920e3e55310a73352235;hpb=1c595b732e537e1b9ba60dec40f8e34f93d4cb74;p=jalview.git diff --git a/src/jalview/viewmodel/ViewportRanges.java b/src/jalview/viewmodel/ViewportRanges.java index 0583a8d..4eb8c95 100644 --- a/src/jalview/viewmodel/ViewportRanges.java +++ b/src/jalview/viewmodel/ViewportRanges.java @@ -21,6 +21,7 @@ package jalview.viewmodel; import jalview.datamodel.AlignmentI; +import jalview.datamodel.HiddenColumns; /** * Slightly less embryonic class which: Supplies and updates viewport properties @@ -79,7 +80,24 @@ public class ViewportRanges extends ViewportProperties } /** + * Get alignment width in cols, excluding hidden cols + */ + public int getVisibleAlignmentWidth() + { + return al.getWidth() - al.getHiddenColumns().getSize(); + } + + /** + * Get alignment height in rows, excluding hidden rows + */ + public int getVisibleAlignmentHeight() + { + return al.getHeight(); + } + + /** * Set first residue visible in the viewport, and retain the current width. + * Fires a property change event. * * @param res * residue position @@ -102,10 +120,10 @@ public class ViewportRanges extends ViewportProperties */ public void setStartEndRes(int start, int end) { - int oldres = this.startRes; - if (start > al.getWidth() - 1) + int oldstartres = this.startRes; + if (start > getVisibleAlignmentWidth() - 1) { - startRes = al.getWidth() - 1; + startRes = Math.max(getVisibleAlignmentWidth() - 1, 0); } else if (start < 0) { @@ -116,46 +134,69 @@ public class ViewportRanges extends ViewportProperties startRes = start; } + int oldendres = this.endRes; if (end < 0) { endRes = 0; } + else if (end > getVisibleAlignmentWidth() - 1) + { + endRes = Math.max(getVisibleAlignmentWidth() - 1, 0); + } else { endRes = end; } - changeSupport.firePropertyChange("startres", oldres, start); + changeSupport.firePropertyChange("startres", oldstartres, startRes); + if (oldstartres == startRes) + { + // event won't be fired if start positions are same + // fire an event for the end positions in case they changed + changeSupport.firePropertyChange("endres", oldendres, endRes); + } } /** - * Set last residue visible in the viewport + * Set last residue visible in the viewport. Fires a property change event. * * @param res * residue position */ public void setEndRes(int res) { + int startres = res; int width = getViewportWidth(); - setStartEndRes(res - width + 1, res); + if (startres + width - 1 > getVisibleAlignmentWidth() - 1) + { + startres = getVisibleAlignmentWidth() - width; + } + setStartEndRes(startres - width + 1, startres); } /** - * Set the first sequence visible in the viewport + * Set the first sequence visible in the viewport, maintaining the height. If + * the viewport would extend past the last sequence, sets the viewport so it + * sits at the bottom of the alignment. Fires a property change event. * * @param seq * sequence position */ public void setStartSeq(int seq) { + int startseq = seq; int height = getViewportHeight(); - setStartEndSeq(seq, seq + height - 1); + if (startseq + height - 1 > getVisibleAlignmentHeight() - 1) + { + startseq = getVisibleAlignmentHeight() - height; + } + setStartEndSeq(startseq, startseq + height - 1); } /** - * Set start and end sequences at the same time. This method only fires one - * event for the two changes, and should be used in preference to separate - * calls to setStartSeq and setEndSeq. + * Set start and end sequences at the same time. The viewport height may + * change. This method only fires one event for the two changes, and should be + * used in preference to separate calls to setStartSeq and setEndSeq. * * @param start * the start sequence @@ -164,10 +205,10 @@ public class ViewportRanges extends ViewportProperties */ public void setStartEndSeq(int start, int end) { - int oldseq = this.startSeq; - if (start > al.getHeight() - 1) + int oldstartseq = this.startSeq; + if (start > getVisibleAlignmentHeight() - 1) { - startSeq = al.getHeight() - 1; + startSeq = Math.max(getVisibleAlignmentHeight() - 1, 0); } else if (start < 0) { @@ -178,9 +219,10 @@ public class ViewportRanges extends ViewportProperties startSeq = start; } - if (end >= al.getHeight()) + int oldendseq = this.endSeq; + if (end >= getVisibleAlignmentHeight()) { - endSeq = al.getHeight() - 1; + endSeq = Math.max(getVisibleAlignmentHeight() - 1, 0); } else if (end < 0) { @@ -191,11 +233,18 @@ public class ViewportRanges extends ViewportProperties endSeq = end; } - changeSupport.firePropertyChange("startseq", oldseq, start); + changeSupport.firePropertyChange("startseq", oldstartseq, startSeq); + if (oldstartseq == startSeq) + { + // event won't be fired if start positions are the same + // fire in case the end positions changed + changeSupport.firePropertyChange("endseq", oldendseq, endSeq); + } } /** - * Set the last sequence visible in the viewport + * Set the last sequence visible in the viewport. Fires a property change + * event. * * @param seq * sequence position @@ -241,7 +290,7 @@ public class ViewportRanges extends ViewportProperties /** * Set viewport width in residues, without changing startRes. Use in * preference to calculating endRes from the width, to avoid out by one - * errors! + * errors! Fires a property change event. * * @param w * width in residues @@ -254,7 +303,7 @@ public class ViewportRanges extends ViewportProperties /** * Set viewport height in residues, without changing startSeq. Use in * preference to calculating endSeq from the height, to avoid out by one - * errors! + * errors! Fires a property change event. * * @param h * height in sequences @@ -266,7 +315,8 @@ public class ViewportRanges extends ViewportProperties /** * Set viewport horizontal start position and width. Use in preference to - * calculating endRes from the width, to avoid out by one errors! + * calculating endRes from the width, to avoid out by one errors! Fires a + * property change event. * * @param start * start residue @@ -275,12 +325,25 @@ public class ViewportRanges extends ViewportProperties */ public void setViewportStartAndWidth(int start, int w) { - setStartEndRes(start, start + w - 1); + int vpstart = start; + if (vpstart < 0) + { + vpstart = 0; + } + else if ((w <= getVisibleAlignmentWidth()) + && (vpstart + w - 1 > getVisibleAlignmentWidth() - 1)) + // viewport width is less than the full alignment and we are running off the + // RHS edge + { + vpstart = getVisibleAlignmentWidth() - w; + } + setStartEndRes(vpstart, vpstart + w - 1); } /** * Set viewport vertical start position and height. Use in preference to - * calculating endSeq from the height, to avoid out by one errors! + * calculating endSeq from the height, to avoid out by one errors! Fires a + * property change event. * * @param start * start sequence @@ -289,7 +352,19 @@ public class ViewportRanges extends ViewportProperties */ public void setViewportStartAndHeight(int start, int h) { - setStartEndSeq(start, start + h - 1); + int vpstart = start; + if (vpstart < 0) + { + vpstart = 0; + } + else if ((h <= getVisibleAlignmentHeight()) + && (vpstart + h - 1 > getVisibleAlignmentHeight() - 1)) + // viewport height is less than the full alignment and we are running off + // the bottom + { + vpstart = getVisibleAlignmentHeight() - h; + } + setStartEndSeq(vpstart, vpstart + h - 1); } /** @@ -313,7 +388,7 @@ public class ViewportRanges extends ViewportProperties } /** - * Scroll the viewport range vertically + * Scroll the viewport range vertically. Fires a property change event. * * @param up * true if scrolling up, false if down @@ -333,7 +408,7 @@ public class ViewportRanges extends ViewportProperties } else { - if (endSeq >= al.getHeight() - 1) + if (endSeq >= getVisibleAlignmentHeight() - 1) { return false; } @@ -344,7 +419,7 @@ public class ViewportRanges extends ViewportProperties } /** - * Scroll the viewport range horizontally + * Scroll the viewport range horizontally. Fires a property change event. * * @param right * true if scrolling right, false if left @@ -364,7 +439,7 @@ public class ViewportRanges extends ViewportProperties } else { - if (endRes > al.getWidth() - 1) + if (endRes >= getVisibleAlignmentWidth() - 1) { return false; } @@ -376,7 +451,8 @@ public class ViewportRanges extends ViewportProperties } /** - * Scroll a wrapped alignment so that the specified residue is visible + * Scroll a wrapped alignment so that the specified residue is visible. Fires + * a property change event. * * @param res * residue position to scroll to @@ -389,4 +465,55 @@ public class ViewportRanges extends ViewportProperties setStartRes((res / width) * width); } + /** + * Scroll so that (x,y) is visible. Fires a property change event. + * + * @param x + * x position in alignment + * @param y + * y position in alignment + */ + public void scrollToVisible(int x, int y) + { + while (y < startSeq) + { + scrollUp(true); + } + while (y > endSeq) + { + scrollUp(false); + } + + HiddenColumns hidden = al.getHiddenColumns(); + while (x < hidden.adjustForHiddenColumns(startRes)) + { + if (!scrollRight(false)) + { + break; + } + } + while (x > hidden.adjustForHiddenColumns(endRes)) + { + if (!scrollRight(true)) + { + break; + } + } + } + + /** + * Adjust sequence position for page up. Fires a property change event. + */ + public void pageUp() + { + setViewportStartAndHeight(2 * startSeq - endSeq, getViewportHeight()); + } + + /** + * Adjust sequence position for page down. Fires a property change event. + */ + public void pageDown() + { + setViewportStartAndHeight(endSeq, getViewportHeight()); + } }