From 6fb07f0a97888d9590b4fa7b9993955e30080782 Mon Sep 17 00:00:00 2001 From: kjvdheide Date: Tue, 3 Oct 2017 09:26:35 +0100 Subject: [PATCH] Added checks for X mouse coordinate being >= 0 and a test for JAL-2750 --- src/jalview/datamodel/SequenceGroup.java | 43 +++++++----------------- src/jalview/gui/ScalePanel.java | 4 ++- test/jalview/gui/ValidColumnSelectionTest.java | 35 ++++++++++++++++++- 3 files changed, 49 insertions(+), 33 deletions(-) diff --git a/src/jalview/datamodel/SequenceGroup.java b/src/jalview/datamodel/SequenceGroup.java index 20b23a9..221b031 100755 --- a/src/jalview/datamodel/SequenceGroup.java +++ b/src/jalview/datamodel/SequenceGroup.java @@ -102,7 +102,7 @@ public class SequenceGroup implements AnnotatedCollectionI public ResidueShaderI cs; // start column (base 0) - int startRes = 0; + private int startRes = 0; // end column (base 0) int endRes = 0; @@ -184,19 +184,7 @@ public class SequenceGroup implements AnnotatedCollectionI this.displayText = displayText; this.colourText = colourText; this.cs = new ResidueShader(scheme); - - if (start > 0) // sanity check for negative start column positions - { - startRes = start; - } - else - { - jalview.bin.Cache.log.warn( - "Negative start column index detected, resetting to default index: " - + startRes); - // start = startRes; - } - + startRes = start; endRes = end; recalcConservation(); } @@ -221,19 +209,7 @@ public class SequenceGroup implements AnnotatedCollectionI displayText = seqsel.displayText; colourText = seqsel.colourText; - if (seqsel.startRes > 0) // sanity check for negative start column - // positions - { - startRes = seqsel.startRes; - } - else - { - jalview.bin.Cache.log.warn( - "Negative start column index detected, resetting to default index: " - + startRes); - // seqsel.startRes = startRes; - } - + startRes = seqsel.startRes; endRes = seqsel.endRes; cs = new ResidueShader((ResidueShader) seqsel.cs); if (seqsel.description != null) @@ -778,11 +754,16 @@ public class SequenceGroup implements AnnotatedCollectionI * * @param i */ - public void setStartRes(int i) + public void setStartRes(int newStart) { - int before = startRes; - startRes = i; - changeSupport.firePropertyChange(SEQ_GROUP_CHANGED, before, startRes); + newStart= Math.max(0,newStart); // sanity check for negative start column positions + int before = startRes; + startRes = newStart; + + changeSupport.firePropertyChange(SEQ_GROUP_CHANGED, before, startRes); + + + } /** diff --git a/src/jalview/gui/ScalePanel.java b/src/jalview/gui/ScalePanel.java index 1db4051..ef2392c 100755 --- a/src/jalview/gui/ScalePanel.java +++ b/src/jalview/gui/ScalePanel.java @@ -275,7 +275,9 @@ public class ScalePanel extends JPanel { mouseDragging = false; - int res = (evt.getX() / av.getCharWidth()) + int xCords = Math.max(0, evt.getX()); // prevent negative X coordinates + + int res = (xCords / av.getCharWidth()) + av.getRanges().getStartRes(); if (av.hasHiddenColumns()) diff --git a/test/jalview/gui/ValidColumnSelectionTest.java b/test/jalview/gui/ValidColumnSelectionTest.java index 81d35f2..c65f236 100644 --- a/test/jalview/gui/ValidColumnSelectionTest.java +++ b/test/jalview/gui/ValidColumnSelectionTest.java @@ -1,5 +1,15 @@ package jalview.gui; +import static org.testng.Assert.assertTrue; + +import jalview.datamodel.Alignment; +import jalview.datamodel.AlignmentI; +import jalview.datamodel.Sequence; +import jalview.datamodel.SequenceGroup; +import jalview.datamodel.SequenceI; + +import java.awt.event.MouseEvent; + import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -13,8 +23,31 @@ public class ValidColumnSelectionTest } @Test(groups = "Functional") - public void testColumnSelection() + public void testPreventNegativeStartColumn() { + SequenceI seq1 = new Sequence("Seq1", "MATRESS"); + SequenceI seq2 = new Sequence("Seq2", "MADNESS"); + AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 }); + + AlignFrame alignFrame = new AlignFrame(al, al.getWidth(), + al.getHeight()); + ScalePanel scalePanel = new ScalePanel( + alignFrame.getViewport(), alignFrame.alignPanel + ); + + MouseEvent mouse = new MouseEvent( + scalePanel, 0, 1, 0, 4, 0, 1, false + ); + scalePanel.mousePressed(mouse); + scalePanel.mouseDragged(mouse); + mouse = new MouseEvent(scalePanel, 0, 1, 0, -30, 0, 1, false); + scalePanel.mouseReleased(mouse); + + SequenceGroup sg = scalePanel.av.getSelectionGroup(); + int startCol = sg.getStartRes(); + + assertTrue(startCol >= 0); + } -- 1.7.10.2