X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fappletgui%2FAlignmentPanel.java;h=308a550d1117470cd29d1327ff51e73a7e52a74b;hb=1dff9688cc5ebc8f0a409806c911756a4d80ce52;hp=dd7c8faa76bff21e675e3c79f46f45021969b5a4;hpb=5edb685cf52a607834a82c10cb4dd9c139e31dde;p=jalview.git diff --git a/src/jalview/appletgui/AlignmentPanel.java b/src/jalview/appletgui/AlignmentPanel.java index dd7c8fa..308a550 100644 --- a/src/jalview/appletgui/AlignmentPanel.java +++ b/src/jalview/appletgui/AlignmentPanel.java @@ -28,6 +28,8 @@ import jalview.datamodel.AlignmentI; import jalview.datamodel.SearchResultsI; import jalview.datamodel.SequenceI; import jalview.structure.StructureSelectionManager; +import jalview.viewmodel.ViewportListenerI; +import jalview.viewmodel.ViewportRanges; import java.awt.BorderLayout; import java.awt.Color; @@ -41,10 +43,11 @@ import java.awt.event.AdjustmentEvent; import java.awt.event.AdjustmentListener; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; +import java.beans.PropertyChangeEvent; import java.util.List; public class AlignmentPanel extends Panel implements AdjustmentListener, - AlignmentViewPanel + AlignmentViewPanel, ViewportListenerI { public AlignViewport av; @@ -65,6 +68,8 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, AnnotationLabels alabels; + ViewportRanges vpRanges; + // this value is set false when selection area being dragged boolean fastPaint = true; @@ -73,6 +78,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, { alignFrame = null; av = null; + vpRanges = null; seqPanel = null; seqPanelHolder = null; sequenceHolderPanel = null; @@ -96,6 +102,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, alignFrame = af; this.av = av; + vpRanges = av.getRanges(); seqPanel = new SeqPanel(av, this); idPanel = new IdPanel(av, this); scalePanel = new ScalePanel(av, this); @@ -126,7 +133,26 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, @Override public void componentResized(ComponentEvent evt) { - setScrollValues(av.getStartRes(), av.getStartSeq()); + // reset the viewport ranges when the alignment panel is resized + // in particular, this initialises the end residue value when Jalview + // is initialised + if (av.getWrapAlignment()) + { + int widthInRes = seqPanel.seqCanvas + .getWrappedCanvasWidth(seqPanel.seqCanvas.getWidth()); + vpRanges.setViewportWidth(widthInRes); + } + else + { + int widthInRes = seqPanel.seqCanvas.getWidth() + / av.getCharWidth(); + int heightInSeq = seqPanel.seqCanvas.getHeight() + / av.getCharHeight(); + + vpRanges.setViewportWidth(widthInRes); + vpRanges.setViewportHeight(heightInSeq); + } + // setScrollValues(vpRanges.getStartRes(), vpRanges.getStartSeq()); if (getSize().height > 0 && annotationPanelHolder.getSize().height > 0) { @@ -159,6 +185,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, } } }); + av.getRanges().addPropertyChangeListener(this); } @Override @@ -225,11 +252,6 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, annotationPanel.repaint(); validate(); repaint(); - - if (overviewPanel != null) - { - overviewPanel.updateOverviewImage(); - } } public void setIdWidth(int w, int h) @@ -383,7 +405,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, */ if (centre) { - int offset = (av.getEndRes() - av.getStartRes() + 1) / 2 - 1; + int offset = (vpRanges.getEndRes() - vpRanges.getStartRes() + 1) / 2 - 1; start = Math.max(start - offset, 0); end = Math.min(end + offset, seq.getEnd() - 1); } @@ -468,33 +490,34 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, // setScrollValues(start, seqIndex); // } // logic copied from jalview.gui.AlignmentPanel: - if ((startv = av.getStartRes()) >= start) + if ((startv = vpRanges.getStartRes()) >= start) { /* * Scroll left to make start of search results visible */ setScrollValues(start - 1, seqIndex); } - else if ((endv = av.getEndRes()) <= end) + else if ((endv = vpRanges.getEndRes()) <= end) { /* * Scroll right to make end of search results visible */ setScrollValues(startv + 1 + end - endv, seqIndex); } - else if ((starts = av.getStartSeq()) > seqIndex) + else if ((starts = vpRanges.getStartSeq()) > seqIndex) { /* * Scroll up to make start of search results visible */ - setScrollValues(av.getStartRes(), seqIndex); + setScrollValues(vpRanges.getStartRes(), seqIndex); } - else if ((ends = av.getEndSeq()) <= seqIndex) + else if ((ends = vpRanges.getEndSeq()) <= seqIndex) { /* * Scroll down to make end of search results visible */ - setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1); + setScrollValues(vpRanges.getStartRes(), starts + seqIndex - ends + + 1); } /* * Else results are already visible - no need to scroll @@ -502,27 +525,13 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, } else { - scrollToWrappedVisible(start); - } - if (redrawOverview && overviewPanel != null) - { - overviewPanel.setBoxPosition(); + vpRanges.scrollToWrappedVisible(start); } + paintAlignment(redrawOverview); return true; } - void scrollToWrappedVisible(int res) - { - int cwidth = seqPanel.seqCanvas - .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width); - if (res <= av.getStartRes() || res >= (av.getStartRes() + cwidth)) - { - vscroll.setValue(res / cwidth); - av.setStartRes(vscroll.getValue() * cwidth); - } - } - public OverviewPanel getOverviewPanel() { return overviewPanel; @@ -632,8 +641,8 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, public void setWrapAlignment(boolean wrap) { - av.setStartSeq(0); - av.setStartRes(0); + vpRanges.setStartSeq(0); + vpRanges.setStartRes(0); scalePanelHolder.setVisible(!wrap); hscroll.setVisible(!wrap); @@ -664,219 +673,164 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, int vextent = 0; - // return value is true if the scroll is valid - public boolean scrollUp(boolean up) + public void setScrollValues(int x, int y) { - if (up) + if (av.getWrapAlignment()) { - if (vscroll.getValue() < 1) - { - return false; - } - setScrollValues(hscroll.getValue(), vscroll.getValue() - 1); + setScrollingForWrappedPanel(x); } else { - if (vextent + vscroll.getValue() >= av.getAlignment().getHeight()) - { - return false; - } - setScrollValues(hscroll.getValue(), vscroll.getValue() + 1); - } - repaint(); - return true; - } + int width = av.getAlignment().getWidth(); + int height = av.getAlignment().getHeight(); - public boolean scrollRight(boolean right) - { - if (!right) - { - if (hscroll.getValue() < 1) + if (av.hasHiddenColumns()) { - return false; + width = av.getColumnSelection().findColumnPosition(width); } - setScrollValues(hscroll.getValue() - 1, vscroll.getValue()); - } - else - { - if (hextent + hscroll.getValue() >= av.getAlignment().getWidth()) + if (x < 0) { - return false; + x = 0; } - setScrollValues(hscroll.getValue() + 1, vscroll.getValue()); - } - - repaint(); - return true; - } - - public void setScrollValues(int x, int y) - { - int width = av.getAlignment().getWidth(); - int height = av.getAlignment().getHeight(); - - if (av.hasHiddenColumns()) - { - width = av.getColumnSelection().findColumnPosition(width); - } - if (x < 0) - { - x = 0; - } - ; - hextent = seqPanel.seqCanvas.getSize().width / av.getCharWidth(); - vextent = seqPanel.seqCanvas.getSize().height / av.getCharHeight(); + hextent = seqPanel.seqCanvas.getSize().width / av.getCharWidth(); + vextent = seqPanel.seqCanvas.getSize().height / av.getCharHeight(); - if (hextent > width) - { - hextent = width; - } - - if (vextent > height) - { - vextent = height; - } - - if ((hextent + x) > width) - { - System.err.println("hextent was " + hextent + " and x was " + x); + if (hextent > width) + { + hextent = width; + } - x = width - hextent; - } + if (vextent > height) + { + vextent = height; + } - if ((vextent + y) > height) - { - y = height - vextent; - } + if ((hextent + x) > width) + { + System.err.println("hextent was " + hextent + " and x was " + x); - if (y < 0) - { - y = 0; - } + x = width - hextent; + } - if (x < 0) - { - System.err.println("x was " + x); - x = 0; - } + if ((vextent + y) > height) + { + y = height - vextent; + } - av.setStartSeq(y); + if (y < 0) + { + y = 0; + } - int endSeq = y + vextent; - if (endSeq > av.getAlignment().getHeight()) - { - endSeq = av.getAlignment().getHeight(); - } + if (x < 0) + { + System.err.println("x was " + x); + x = 0; + } - av.setEndSeq(endSeq); - av.setStartRes(x); - av.setEndRes((x + (seqPanel.seqCanvas.getSize().width / av - .getCharWidth())) - 1); + hscroll.setValues(x, hextent, 0, width); + vscroll.setValues(y, vextent, 0, height); - hscroll.setValues(x, hextent, 0, width); - vscroll.setValues(y, vextent, 0, height); + // AWT scrollbar does not fire adjustmentValueChanged for setValues + // so also call adjustment code! + adjustHorizontal(x); + adjustVertical(y); - if (overviewPanel != null) - { - overviewPanel.setBoxPosition(); + sendViewPosition(); } - sendViewPosition(); - } @Override public void adjustmentValueChanged(AdjustmentEvent evt) { - int oldX = av.getStartRes(); - int oldY = av.getStartSeq(); - + // Note that this event is NOT fired by the AWT scrollbar when setValues is + // called. Instead manually call adjustHorizontal and adjustVertical + // directly. if (evt == null || evt.getSource() == apvscroll) { annotationPanel.setScrollOffset(apvscroll.getValue(), false); alabels.setScrollOffset(apvscroll.getValue(), false); - // annotationPanel.image=null; - // alabels.image=null; - // alabels.repaint(); - // annotationPanel.repaint(); } if (evt == null || evt.getSource() == hscroll) { int x = hscroll.getValue(); - av.setStartRes(x); - av.setEndRes(x + seqPanel.seqCanvas.getSize().width - / av.getCharWidth() - 1); + adjustHorizontal(x); } if (evt == null || evt.getSource() == vscroll) { int offy = vscroll.getValue(); - if (av.getWrapAlignment()) - { - int rowSize = seqPanel.seqCanvas - .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width); - av.setStartRes(vscroll.getValue() * rowSize); - av.setEndRes((vscroll.getValue() + 1) * rowSize); - } - else - { - av.setStartSeq(offy); - av.setEndSeq(offy + seqPanel.seqCanvas.getSize().height - / av.getCharHeight() - 1); - } + adjustVertical(offy); } - if (overviewPanel != null) + } + + private void adjustHorizontal(int x) + { + int oldX = vpRanges.getStartRes(); + int oldwidth = vpRanges.getViewportWidth(); + int width = seqPanel.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 + // ranges values are the same + if ((x == oldX) && (width == oldwidth)) { - overviewPanel.setBoxPosition(); + return; } + vpRanges.setViewportStartAndWidth(x, width); - int scrollX = av.getStartRes() - oldX; - int scrollY = av.getStartSeq() - oldY; - - if (av.getWrapAlignment() || !fastPaint || av.MAC) + if (av.getWrapAlignment() || !fastPaint) { repaint(); } - else + sendViewPosition(); + } + + private void adjustVertical(int offy) + { + int oldX = vpRanges.getStartRes(); + int oldwidth = vpRanges.getViewportWidth(); + int oldY = vpRanges.getStartSeq(); + int oldheight = vpRanges.getViewportHeight(); + + if (av.getWrapAlignment()) { - // Make sure we're not trying to draw a panel - // larger than the visible window - if (scrollX > av.getEndRes() - av.getStartRes()) + int rowSize = seqPanel.seqCanvas + .getWrappedCanvasWidth(seqPanel.seqCanvas.getWidth()); + + // if we're scrolling to the position we're already at, stop + // this prevents infinite recursion of events when the scroll/viewport + // ranges values are the same + if ((offy * rowSize == oldX) && (oldwidth == rowSize)) { - scrollX = av.getEndRes() - av.getStartRes(); + return; } - else if (scrollX < av.getStartRes() - av.getEndRes()) + else if (offy > -1) { - scrollX = av.getStartRes() - av.getEndRes(); + vpRanges.setViewportStartAndWidth(offy * rowSize, rowSize); } + } + else + { + int height = seqPanel.seqCanvas.getHeight() / av.getCharHeight(); - idPanel.idCanvas.fastPaint(scrollY); - seqPanel.seqCanvas.fastPaint(scrollX, scrollY); - - scalePanel.repaint(); - if (av.isShowAnnotation()) + // if we're scrolling to the position we're already at, stop + // this prevents infinite recursion of events when the scroll/viewport + // ranges values are the same + if ((offy == oldY) && (height == oldheight)) { - annotationPanel.fastPaint(av.getStartRes() - oldX); + return; } + vpRanges.setViewportStartAndHeight(offy, height); } - sendViewPosition(); - - /* - * If there is one, scroll the (Protein/cDNA) complementary alignment to - * match, unless we are ourselves doing that. - */ - if (isFollowingComplementScroll()) - { - setFollowingComplementScroll(false); - } - else + if (av.getWrapAlignment() || !fastPaint) { - AlignmentPanel ap = getComplementPanel(); - av.scrollComplementaryAlignment(ap); + repaint(); } - + sendViewPosition(); } /** @@ -927,7 +881,6 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, * This is like AlignmentI.findIndex(seq) but here we are matching the * dataset sequence not the aligned sequence */ - int sequenceIndex = 0; boolean matched = false; for (SequenceI seq : seqs) { @@ -936,7 +889,6 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, matched = true; break; } - sequenceIndex++; } if (!matched) { @@ -947,16 +899,15 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, * Scroll to position but centring the target residue. Also set a state flag * to prevent adjustmentValueChanged performing this recursively. */ - setFollowingComplementScroll(true); - // this should be scrollToPosition(sr,verticalOffset, scrollToPosition(sr, seqOffset, true, true); } private void sendViewPosition() { StructureSelectionManager.getStructureSelectionManager(av.applet) - .sendViewPosition(this, av.getStartRes(), av.getEndRes(), - av.getStartSeq(), av.getEndSeq()); + .sendViewPosition(this, vpRanges.getStartRes(), + vpRanges.getEndRes(), vpRanges.getStartSeq(), + vpRanges.getEndSeq()); } /** @@ -1002,30 +953,7 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, idPanel.idCanvas.setSize(d.width, canvasHeight); } - if (av.getWrapAlignment()) - { - int maxwidth = av.getAlignment().getWidth(); - - if (av.hasHiddenColumns()) - { - maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1; - } - - int canvasWidth = seqPanel.seqCanvas - .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width); - - if (canvasWidth > 0) - { - int max = maxwidth / canvasWidth; - vscroll.setMaximum(1 + max); - vscroll.setUnitIncrement(1); - vscroll.setVisibleAmount(1); - } - } - else - { - setScrollValues(av.getStartRes(), av.getStartSeq()); - } + setScrollValues(vpRanges.getStartRes(), vpRanges.getStartSeq()); seqPanel.seqCanvas.repaint(); idPanel.idCanvas.repaint(); @@ -1041,6 +969,36 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, } + /* + * Set vertical scroll bar parameters for wrapped panel + * @param res + * the residue to scroll to + */ + private void setScrollingForWrappedPanel(int res) + { + // get the width of the alignment in residues + int maxwidth = av.getAlignment().getWidth(); + if (av.hasHiddenColumns()) + { + maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1; + } + + // get the width of the canvas in residues + int canvasWidth = seqPanel.seqCanvas + .getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width); + if (canvasWidth > 0) + { + // position we want to scroll to is number of canvasWidth's to get there + int current = res / canvasWidth; + + // max scroll position: add one because extent is 1 and scrollbar value + // can only be set to at most max - extent + int max = maxwidth / canvasWidth + 1; + vscroll.setUnitIncrement(1); + vscroll.setValues(current, 1, 0, max); + } + } + protected Panel sequenceHolderPanel = new Panel(); protected Scrollbar vscroll = new Scrollbar(); @@ -1065,9 +1023,9 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, /* * Flag set while scrolling to follow complementary cDNA/protein scroll. When - * true, suppresses invoking the same method recursively. + * false, suppresses invoking the same method recursively. */ - private boolean followingComplementScroll; + private boolean scrollComplementaryPanel = true; private void jbInit() throws Exception { @@ -1176,14 +1134,37 @@ public class AlignmentPanel extends Panel implements AdjustmentListener, * * @param b */ - protected void setFollowingComplementScroll(boolean b) + protected void setToScrollComplementPanel(boolean b) { - this.followingComplementScroll = b; + this.scrollComplementaryPanel = b; } - protected boolean isFollowingComplementScroll() + protected boolean isSetToScrollComplementPanel() { - return this.followingComplementScroll; + return this.scrollComplementaryPanel; + } + + @Override + /** + * Property change event fired when a change is made to the viewport ranges + * object associated with this alignment panel's viewport + */ + public void propertyChange(PropertyChangeEvent evt) + { + // update this panel's scroll values based on the new viewport ranges values + int x = vpRanges.getStartRes(); + int y = vpRanges.getStartSeq(); + setScrollValues(x, y); + + // now update any complementary alignment (its viewport ranges object + // is different so does not get automatically updated) + if (isSetToScrollComplementPanel()) + { + setToScrollComplementPanel(false); + av.scrollComplementaryAlignment(getComplementPanel()); + setToScrollComplementPanel(true); + } + } }