From 1c595b732e537e1b9ba60dec40f8e34f93d4cb74 Mon Sep 17 00:00:00 2001 From: kiramt Date: Tue, 9 May 2017 16:14:50 +0100 Subject: [PATCH] JAL-2491 Tidies and more refactoring --- src/jalview/gui/AlignmentPanel.java | 35 +++--- src/jalview/gui/SeqPanel.java | 1 - src/jalview/viewmodel/ViewportRanges.java | 165 +++++++++++++++++++++++------ 3 files changed, 149 insertions(+), 52 deletions(-) diff --git a/src/jalview/gui/AlignmentPanel.java b/src/jalview/gui/AlignmentPanel.java index 8619ad0..e545b8f 100644 --- a/src/jalview/gui/AlignmentPanel.java +++ b/src/jalview/gui/AlignmentPanel.java @@ -156,19 +156,17 @@ public class AlignmentPanel extends GAlignmentPanel implements { int widthInRes = getSeqPanel().seqCanvas .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth()); - vpRanges.setStartEndRes(vpRanges.getStartRes(), - vpRanges.getStartRes() + widthInRes); + vpRanges.setViewportWidth(widthInRes); } else { - int widthInRes = (getSeqPanel().seqCanvas.getWidth() / av - .getCharWidth()) - 1; - int heightInSeq = (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight()) - 1; + int widthInRes = getSeqPanel().seqCanvas.getWidth() + / av.getCharWidth(); + int heightInSeq = getSeqPanel().seqCanvas.getHeight() + / av.getCharHeight(); - vpRanges.setStartEndRes(vpRanges.getStartRes(), - vpRanges.getStartRes() + widthInRes); - vpRanges.setStartEndSeq(vpRanges.getStartSeq(), - vpRanges.getStartSeq() + heightInSeq); + vpRanges.setViewportWidth(widthInRes); + vpRanges.setViewportHeight(heightInSeq); } } @@ -633,8 +631,7 @@ public class AlignmentPanel extends GAlignmentPanel implements { int widthInRes = getSeqPanel().seqCanvas .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth()); - vpRanges.setStartEndRes(vpRanges.getStartRes(), - vpRanges.getStartRes() + widthInRes - 1); + vpRanges.setViewportWidth(widthInRes); } else { @@ -643,8 +640,8 @@ public class AlignmentPanel extends GAlignmentPanel implements int heightInSeq = (getSeqPanel().seqCanvas.getHeight() / av .getCharHeight()) - 1; - vpRanges.setStartEndRes(vpRanges.getStartRes(), widthInRes); - vpRanges.setStartEndSeq(vpRanges.getStartSeq(), heightInSeq); + vpRanges.setViewportWidth(widthInRes); + vpRanges.setViewportHeight(heightInSeq); } idSpaceFillerPanel1.setVisible(!wrap); @@ -759,7 +756,7 @@ public class AlignmentPanel extends GAlignmentPanel implements } else if (offy > -1) { - vpRanges.setStartEndRes(offy * rowSize, (offy + 1) * rowSize - 1); + vpRanges.setViewportStartAndWidth(offy * rowSize, rowSize); } else { @@ -785,7 +782,7 @@ public class AlignmentPanel extends GAlignmentPanel implements if (evt.getSource() == hscroll) { int x = hscroll.getValue(); - int width = (getSeqPanel().seqCanvas.getWidth() / av.getCharWidth()) - 1; + int width = getSeqPanel().seqCanvas.getWidth() / av.getCharWidth(); // if we're scrolling to the position we're already at, stop // this prevents infinite recursion of events when the scroll/viewport @@ -794,13 +791,13 @@ public class AlignmentPanel extends GAlignmentPanel implements { return; } - vpRanges.setStartEndRes(x, x + width); + vpRanges.setViewportStartAndWidth(x, width); } else if (evt.getSource() == vscroll) { int y = vscroll.getValue(); - int height = (getSeqPanel().seqCanvas.getHeight() / av - .getCharHeight()) - 1; + int height = getSeqPanel().seqCanvas.getHeight() + / av.getCharHeight(); // if we're scrolling to the position we're already at, stop // this prevents infinite recursion of events when the scroll/viewport @@ -809,7 +806,7 @@ public class AlignmentPanel extends GAlignmentPanel implements { return; } - vpRanges.setStartEndSeq(y, y + height); + vpRanges.setViewportStartAndHeight(y, height); } if (!fastPaint) { diff --git a/src/jalview/gui/SeqPanel.java b/src/jalview/gui/SeqPanel.java index c14854b..f1e4d13 100644 --- a/src/jalview/gui/SeqPanel.java +++ b/src/jalview/gui/SeqPanel.java @@ -384,7 +384,6 @@ public class SeqPanel extends JPanel implements MouseListener, if (av.getWrapAlignment()) { av.getRanges().scrollToWrappedVisible(seqCanvas.cursorX); - // ap.scrollToWrappedVisible(seqCanvas.cursorX); } else { diff --git a/src/jalview/viewmodel/ViewportRanges.java b/src/jalview/viewmodel/ViewportRanges.java index 54bbfd8..0583a8d 100644 --- a/src/jalview/viewmodel/ViewportRanges.java +++ b/src/jalview/viewmodel/ViewportRanges.java @@ -23,10 +23,10 @@ package jalview.viewmodel; import jalview.datamodel.AlignmentI; /** - * Embryonic class which: Supplies and updates viewport properties relating to - * position such as: start and end residues and sequences; ideally will serve - * hidden columns/rows too. Intention also to support calculations for - * positioning, scrolling etc. such as finding the middle of the viewport, + * Slightly less embryonic class which: Supplies and updates viewport properties + * relating to position such as: start and end residues and sequences; ideally + * will serve hidden columns/rows too. Intention also to support calculations + * for positioning, scrolling etc. such as finding the middle of the viewport, * checking for scrolls off screen */ public class ViewportRanges extends ViewportProperties @@ -90,26 +90,42 @@ public class ViewportRanges extends ViewportProperties setStartEndRes(res, res + width - 1); } - public void setStartEndRes(int startres, int endres) + /** + * Set start and end residues at the same time. This method only fires one + * event for the two changes, and should be used in preference to separate + * calls to setStartRes and setEndRes. + * + * @param start + * the start residue + * @param end + * the end residue + */ + public void setStartEndRes(int start, int end) { int oldres = this.startRes; - if (startres > al.getWidth() - 1) + if (start > al.getWidth() - 1) { - startres = al.getWidth() - 1; + startRes = al.getWidth() - 1; } - else if (startres < 0) + else if (start < 0) { - startres = 0; + startRes = 0; + } + else + { + startRes = start; } - this.startRes = startres; - if (endres < 0) + if (end < 0) { - endres = 0; + endRes = 0; + } + else + { + endRes = end; } - this.endRes = endres; - changeSupport.firePropertyChange("startres", oldres, startres); + changeSupport.firePropertyChange("startres", oldres, start); } /** @@ -136,29 +152,46 @@ public class ViewportRanges extends ViewportProperties setStartEndSeq(seq, seq + height - 1); } - public void setStartEndSeq(int startseq, int endseq) + /** + * 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. + * + * @param start + * the start sequence + * @param end + * the end sequence + */ + public void setStartEndSeq(int start, int end) { int oldseq = this.startSeq; - if (startseq > al.getHeight() - 1) + if (start > al.getHeight() - 1) + { + startSeq = al.getHeight() - 1; + } + else if (start < 0) { - startseq = al.getHeight() - 1; + startSeq = 0; } - else if (startseq < 0) + else { - startseq = 0; + startSeq = start; } - this.startSeq = startseq; - if (endseq >= al.getHeight()) + if (end >= al.getHeight()) { - endseq = al.getHeight() - 1; + endSeq = al.getHeight() - 1; } - else if (endseq < 0) + else if (end < 0) { - endseq = 0; + endSeq = 0; } - this.endSeq = endseq; - changeSupport.firePropertyChange("startseq", oldseq, startseq); + else + { + endSeq = end; + } + + changeSupport.firePropertyChange("startseq", oldseq, start); } /** @@ -206,13 +239,67 @@ 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! + * + * @param w + * width in residues + */ + public void setViewportWidth(int w) + { + setStartEndRes(startRes, startRes + w - 1); + } + + /** + * Set viewport height in residues, without changing startSeq. Use in + * preference to calculating endSeq from the height, to avoid out by one + * errors! + * + * @param h + * height in sequences + */ + public void setViewportHeight(int h) + { + setStartEndSeq(startSeq, startSeq + h - 1); + } + + /** + * Set viewport horizontal start position and width. Use in preference to + * calculating endRes from the width, to avoid out by one errors! + * + * @param start + * start residue + * @param w + * width in residues + */ + public void setViewportStartAndWidth(int start, int w) + { + setStartEndRes(start, start + w - 1); + } + + /** + * Set viewport vertical start position and height. Use in preference to + * calculating endSeq from the height, to avoid out by one errors! + * + * @param start + * start sequence + * @param h + * height in sequences + */ + public void setViewportStartAndHeight(int start, int h) + { + setStartEndSeq(start, start + h - 1); + } + + /** * Get width of viewport in residues * * @return width of viewport */ public int getViewportWidth() { - return (endRes - startRes + 1); // TODO get for wrapped alignments too + return (endRes - startRes + 1); } /** @@ -225,7 +312,14 @@ public class ViewportRanges extends ViewportProperties return (endSeq - startSeq + 1); } - // return value is true if the scroll is valid + /** + * Scroll the viewport range vertically + * + * @param up + * true if scrolling up, false if down + * + * @return true if the scroll is valid + */ public boolean scrollUp(boolean up) { if (up) @@ -250,12 +344,12 @@ public class ViewportRanges extends ViewportProperties } /** - * DOCUMENT ME! + * Scroll the viewport range horizontally * * @param right - * DOCUMENT ME! + * true if scrolling right, false if left * - * @return DOCUMENT ME! + * @return true if the scroll is valid */ public boolean scrollRight(boolean right) { @@ -281,11 +375,18 @@ public class ViewportRanges extends ViewportProperties return true; } + /** + * Scroll a wrapped alignment so that the specified residue is visible + * + * @param res + * residue position to scroll to + */ public void scrollToWrappedVisible(int res) { // get the start residue of the wrapped row which res is in // and set that as our start residue - setStartRes((res / getViewportWidth()) * getViewportWidth()); + int width = getViewportWidth(); + setStartRes((res / width) * width); } } -- 1.7.10.2