From 3c36c8d6bd78fff1770222276512026fe20751fc Mon Sep 17 00:00:00 2001 From: gmungoc Date: Wed, 20 Mar 2019 16:34:37 +0000 Subject: [PATCH] JAL-3212 more use of av.getAbsoluteColumn, and unit tests --- src/jalview/gui/AlignViewport.java | 43 -------------- src/jalview/gui/AnnotationPanel.java | 11 +--- src/jalview/gui/ScalePanel.java | 20 +------ src/jalview/gui/SeqPanel.java | 79 ++++++++++++-------------- src/jalview/viewmodel/AlignmentViewport.java | 43 ++++++++++++++ test/jalview/gui/AlignViewportTest.java | 67 ++++++++++++++++++++++ 6 files changed, 149 insertions(+), 114 deletions(-) diff --git a/src/jalview/gui/AlignViewport.java b/src/jalview/gui/AlignViewport.java index 82dcacd..bc668fd 100644 --- a/src/jalview/gui/AlignViewport.java +++ b/src/jalview/gui/AlignViewport.java @@ -1043,47 +1043,4 @@ public class AlignViewport extends AlignmentViewport { this.viewName = viewName; } - - /** - * Answers the absolute column position (0..) (allowing for any hidden - * columns) for the given x position in the viewport. The position is - * restricted to the currently visible range, and to the width of the - * alignment. This method is not applicable, and throws an exception if - * called, if the view is in wrapped mode.
- * Example - * - * - * @param xPos - * @return - * @throw IllegalStateException - */ - public int getAbsoluteColumn(int xPos) - { - if (getWrapAlignment()) - { - throw new IllegalStateException("Not valid in wrapped mode"); - } - int xCords = Math.max(0, xPos); // treat negative X as zero - int res = (xCords / getCharWidth()) + ranges.getStartRes(); - res = Math.min(res, ranges.getEndRes()); - if (hasHiddenColumns()) - { - res = getAlignment().getHiddenColumns().visibleToAbsoluteColumn(res); - } - res = Math.max(0, res); - return res; - } } diff --git a/src/jalview/gui/AnnotationPanel.java b/src/jalview/gui/AnnotationPanel.java index 16db94c..9d64d2f 100755 --- a/src/jalview/gui/AnnotationPanel.java +++ b/src/jalview/gui/AnnotationPanel.java @@ -765,16 +765,7 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI, this.setToolTipText(null); return; } - - int column = (evt.getX() / av.getCharWidth()) - + av.getRanges().getStartRes(); - column = Math.min(column, av.getRanges().getEndRes()); - - if (av.hasHiddenColumns()) - { - column = av.getAlignment().getHiddenColumns() - .visibleToAbsoluteColumn(column); - } + int column = av.getAbsoluteColumn(evt.getX()); AlignmentAnnotation ann = aa[row]; if (row > -1 && ann.annotations != null diff --git a/src/jalview/gui/ScalePanel.java b/src/jalview/gui/ScalePanel.java index 5e7e4a0..714a5aa 100755 --- a/src/jalview/gui/ScalePanel.java +++ b/src/jalview/gui/ScalePanel.java @@ -108,23 +108,7 @@ public class ScalePanel extends JPanel @Override public void mousePressed(MouseEvent evt) { - int x = (evt.getX() / av.getCharWidth()) + av.getRanges().getStartRes(); - final int res; - - if (av.hasHiddenColumns()) - { - x = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(x); - } - - if (x >= av.getAlignment().getWidth()) - { - res = av.getAlignment().getWidth() - 1; - } - else - { - res = x; - } - + final int res = av.getAbsoluteColumn(evt.getX()); min = res; max = res; @@ -401,8 +385,6 @@ public class ScalePanel extends JPanel reveal = av.getAlignment().getHiddenColumns() .getRegionWithEdgeAtRes(res); - res = av.getAlignment().getHiddenColumns().visibleToAbsoluteColumn(res); - ToolTipManager.sharedInstance().registerComponent(this); this.setToolTipText( MessageManager.getString("label.reveal_hidden_columns")); diff --git a/src/jalview/gui/SeqPanel.java b/src/jalview/gui/SeqPanel.java index 1176df5..97fa19b 100644 --- a/src/jalview/gui/SeqPanel.java +++ b/src/jalview/gui/SeqPanel.java @@ -331,61 +331,56 @@ public class SeqPanel extends JPanel */ int findColumn(MouseEvent evt) { - int res = 0; int x = evt.getX(); + if (!av.getWrapAlignment()) + { + return av.getAbsoluteColumn(x); + } + + /* + * wrapped mode calculation + */ final int startRes = av.getRanges().getStartRes(); final int charWidth = av.getCharWidth(); - if (av.getWrapAlignment()) + int res = 0; + int hgap = av.getCharHeight(); + if (av.getScaleAboveWrapped()) { - int hgap = av.getCharHeight(); - if (av.getScaleAboveWrapped()) - { - hgap += av.getCharHeight(); - } - - int cHeight = av.getAlignment().getHeight() * av.getCharHeight() - + hgap + seqCanvas.getAnnotationHeight(); + hgap += av.getCharHeight(); + } - int y = evt.getY(); - y = Math.max(0, y - hgap); - x -= seqCanvas.getLabelWidthWest(); - if (x < 0) - { - // mouse is over left scale - return -1; - } + int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap + + seqCanvas.getAnnotationHeight(); - int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth()); - if (cwidth < 1) - { - return 0; - } - if (x >= cwidth * charWidth) - { - // mouse is over right scale - return -1; - } + int y = evt.getY(); + y = Math.max(0, y - hgap); + x -= seqCanvas.getLabelWidthWest(); + if (x < 0) + { + // mouse is over left scale + return -1; + } - wrappedBlock = y / cHeight; - wrappedBlock += startRes / cwidth; - // allow for wrapped view scrolled right (possible from Overview) - int startOffset = startRes % cwidth; - res = wrappedBlock * cwidth + startOffset - + Math.min(cwidth - 1, x / charWidth); + int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth()); + if (cwidth < 1) + { + return 0; } - else + if (x >= cwidth * charWidth) { - /* - * make sure we calculate relative to visible alignment, - * rather than right-hand gutter - */ - x = Math.min(x, seqCanvas.getX() + seqCanvas.getWidth()); - res = (x / charWidth) + startRes; - res = Math.min(res, av.getRanges().getEndRes()); + // mouse is over right scale + return -1; } + wrappedBlock = y / cHeight; + wrappedBlock += startRes / cwidth; + // allow for wrapped view scrolled right (possible from Overview) + int startOffset = startRes % cwidth; + res = wrappedBlock * cwidth + startOffset + + Math.min(cwidth - 1, x / charWidth); + if (av.hasHiddenColumns()) { res = av.getAlignment().getHiddenColumns() diff --git a/src/jalview/viewmodel/AlignmentViewport.java b/src/jalview/viewmodel/AlignmentViewport.java index 148ea16..e9e53e7 100644 --- a/src/jalview/viewmodel/AlignmentViewport.java +++ b/src/jalview/viewmodel/AlignmentViewport.java @@ -3020,4 +3020,47 @@ public abstract class AlignmentViewport codingComplement.setUpdateStructures(needToUpdateStructureViews); } } + + /** + * Answers the absolute column position (0..) (allowing for any hidden + * columns) for the given x position in the viewport. The position is + * restricted to the currently visible range, and to the width of the + * alignment. This method is not applicable, and throws an exception if + * called, if the view is in wrapped mode.
+ * Example + * + * + * @param xPos + * @return + * @throw IllegalStateException + */ + public int getAbsoluteColumn(int xPos) + { + if (getWrapAlignment()) + { + throw new IllegalStateException("Not valid in wrapped mode"); + } + int xCords = Math.max(0, xPos); // treat negative X as zero + int res = (xCords / getCharWidth()) + ranges.getStartRes(); + res = Math.min(res, ranges.getEndRes()); + if (hasHiddenColumns()) + { + res = getAlignment().getHiddenColumns().visibleToAbsoluteColumn(res); + } + res = Math.max(0, res); + return res; + } } diff --git a/test/jalview/gui/AlignViewportTest.java b/test/jalview/gui/AlignViewportTest.java index 959abb0..fc6a80d 100644 --- a/test/jalview/gui/AlignViewportTest.java +++ b/test/jalview/gui/AlignViewportTest.java @@ -511,4 +511,71 @@ public class AlignViewportTest assertEquals(0, ranges.getStartSeq()); assertEquals(2, ranges.getEndSeq()); } + + @Test(groups = { "Functional" }) + public void testGetAbsoluteColumn() + { + AlignFrame af = new FileLoader().LoadFileWaitTillLoaded( + "examples/uniref50.fa", DataSourceType.FILE); + AlignViewport av = af.getViewport(); + ViewportRanges ranges = av.getRanges(); + assertEquals(0, ranges.getStartRes()); + assertEquals(59, ranges.getEndRes()); // 60 columns wide + + int cw = av.getCharWidth(); + assertEquals(0, av.getAbsoluteColumn(0)); + assertEquals(0, av.getAbsoluteColumn(cw - 1)); + assertEquals(1, av.getAbsoluteColumn(cw)); + + // negative x is treated as zero: + assertEquals(0, av.getAbsoluteColumn(-20)); + + // x to the right of panel is restricted to panel width: + assertEquals(59, av.getAbsoluteColumn(100 * cw)); + + // scroll right by 10 columns + af.alignPanel.setScrollValues(10, 0); + assertEquals(10, av.getAbsoluteColumn(0)); + assertEquals(10, av.getAbsoluteColumn(cw - 1)); + assertEquals(11, av.getAbsoluteColumn(cw)); + assertEquals(10, av.getAbsoluteColumn(-20)); + assertEquals(69, av.getAbsoluteColumn(100 * cw)); + } + + @Test( + groups = + { "Functional" }, + expectedExceptions = + { IllegalStateException.class }) + public void testGetAbsoluteColumn_wrapped() + { + testee.setWrapAlignment(true); + testee.getAbsoluteColumn(0); + } + + @Test(groups = { "Functional" }) + public void testGetAbsoluteColumn_hiddenColumns() + { + AlignFrame af = new FileLoader().LoadFileWaitTillLoaded( + "examples/uniref50.fa", DataSourceType.FILE); + AlignViewport av = af.getViewport(); + ViewportRanges ranges = av.getRanges(); + assertEquals(0, ranges.getStartRes()); + assertEquals(59, ranges.getEndRes()); // 60 columns wide + + av.hideColumns(11, 40); // 30 columns hidden, cols 0-10 visible + int cw = av.getCharWidth(); + assertEquals(0, av.getAbsoluteColumn(0)); + assertEquals(10, av.getAbsoluteColumn(10 * cw)); // left of hidden cols + assertEquals(41, av.getAbsoluteColumn(11 * cw)); // skipped 30 hidden + + // scroll right by 11 columns - to just past hidden columns marker + af.alignPanel.setScrollValues(11, 0); + assertEquals(41, av.getAbsoluteColumn(0)); + + // hide absolute columns 60-70 (visible columns 19-29) + av.hideColumns(60, 70); + assertEquals(59, av.getAbsoluteColumn(18 * cw)); + assertEquals(71, av.getAbsoluteColumn(19 * cw)); // skipped 11 hidden + } } -- 1.7.10.2