From: kiramt Date: Mon, 27 Feb 2017 11:45:30 +0000 (+0000) Subject: JAL-2388 Minor refactoring X-Git-Tag: Release_2_10_2~3^2~92^2~64 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=fce4313f5daea7f5b5914bfaefd11e946664942c;p=jalview.git JAL-2388 Minor refactoring --- diff --git a/src/jalview/appletgui/OverviewPanel.java b/src/jalview/appletgui/OverviewPanel.java index 743d217..1b6df1d 100755 --- a/src/jalview/appletgui/OverviewPanel.java +++ b/src/jalview/appletgui/OverviewPanel.java @@ -123,33 +123,23 @@ public class OverviewPanel extends Panel implements Runnable, @Override public void mousePressed(MouseEvent evt) { - od.setBoxX(evt.getX()); - od.setBoxY(evt.getY()); - checkValid(); + od.setBoxPositionByMouse(evt.getX(), evt.getY()); + ap.setScrollValues(od.getScrollCol(), od.getScrollRow()); + ap.paintAlignment(false); } @Override public void mouseReleased(MouseEvent evt) { - od.setBoxX(evt.getX()); - od.setBoxY(evt.getY()); - checkValid(); + od.setBoxPositionByMouse(evt.getX(), evt.getY()); + ap.setScrollValues(od.getScrollCol(), od.getScrollRow()); + ap.paintAlignment(false); } @Override public void mouseDragged(MouseEvent evt) { - od.setBoxX(evt.getX()); - od.setBoxY(evt.getY()); - checkValid(); - } - - /** - * Check box dimensions and scroll positions and correct if necessary - */ - private void checkValid() - { - od.checkValid(); + od.setBoxPositionByMouse(evt.getX(), evt.getY()); ap.setScrollValues(od.getScrollCol(), od.getScrollRow()); ap.paintAlignment(false); } @@ -383,10 +373,7 @@ public class OverviewPanel extends Panel implements Runnable, { og.drawImage(miniMe, 0, 0, this); og.setColor(Color.red); - og.drawRect(od.getBoxX(), od.getBoxY(), od.getBoxWidth(), - od.getBoxHeight()); - og.drawRect(od.getBoxX() + 1, od.getBoxY() + 1, od.getBoxWidth() - 2, - od.getBoxHeight() - 2); + od.drawBox(og); g.drawImage(offscreen, 0, 0, this); } } diff --git a/src/jalview/gui/OverviewPanel.java b/src/jalview/gui/OverviewPanel.java index fc3cc4b..d87285c 100755 --- a/src/jalview/gui/OverviewPanel.java +++ b/src/jalview/gui/OverviewPanel.java @@ -36,7 +36,8 @@ import java.awt.image.BufferedImage; import javax.swing.JPanel; /** - * DOCUMENT ME! + * Panel displaying an overview of the full alignment, with an interactive box + * representing the viewport onto the alignment. * * @author $author$ * @version $Revision$ @@ -73,19 +74,19 @@ public class OverviewPanel extends JPanel implements Runnable /** * Creates a new OverviewPanel object. * - * @param ap + * @param alPanel * The alignment panel which is shown in the overview panel */ - public OverviewPanel(AlignmentPanel ap) + public OverviewPanel(AlignmentPanel alPanel) { - this.av = ap.av; - this.ap = ap; + this.av = alPanel.av; + this.ap = alPanel; setLayout(null); sr = new SequenceRenderer(av); sr.renderGaps = false; sr.forOverview = true; - fr = new FeatureRenderer(ap); + fr = new FeatureRenderer(alPanel); od = new OverviewDimensions(av); @@ -111,9 +112,8 @@ public class OverviewPanel extends JPanel implements Runnable { // TODO: feature: jv2.5 detect shift drag and update selection from // it. - od.setBoxX(evt.getX()); - od.setBoxY(evt.getY()); - checkValid(); + od.setBoxPositionByMouse(evt.getX(), evt.getY()); + ap.setScrollValues(od.getScrollCol(), od.getScrollRow()); } } }); @@ -125,9 +125,8 @@ public class OverviewPanel extends JPanel implements Runnable { if (!av.getWrapAlignment()) { - od.setBoxX(evt.getX()); - od.setBoxY(evt.getY()); - checkValid(); + od.setBoxPositionByMouse(evt.getX(), evt.getY()); + ap.setScrollValues(od.getScrollCol(), od.getScrollRow()); } } }); @@ -136,16 +135,6 @@ public class OverviewPanel extends JPanel implements Runnable } /** - * Check box dimensions and scroll positions and correct if necessary - */ - private void checkValid() - { - od.checkValid(); - ap.setScrollValues(od.getScrollCol(), od.getScrollRow()); - - } - - /** * Updates the overview image when the related alignment panel is updated */ public void updateOverviewImage() @@ -391,9 +380,6 @@ public class OverviewPanel extends JPanel implements Runnable } // TODO: render selected regions g.setColor(Color.red); - g.drawRect(od.getBoxX(), od.getBoxY(), od.getBoxWidth(), - od.getBoxHeight()); - g.drawRect(od.getBoxX() + 1, od.getBoxY() + 1, od.getBoxWidth() - 2, - od.getBoxHeight() - 2); + od.drawBox(g); } } diff --git a/src/jalview/viewmodel/OverviewDimensions.java b/src/jalview/viewmodel/OverviewDimensions.java index 17f56c9..9b55e55 100644 --- a/src/jalview/viewmodel/OverviewDimensions.java +++ b/src/jalview/viewmodel/OverviewDimensions.java @@ -22,6 +22,8 @@ package jalview.viewmodel; import jalview.api.AlignViewportI; +import java.awt.Graphics; + public class OverviewDimensions { // Default width and height values @@ -108,14 +110,15 @@ public class OverviewDimensions /** * Check box dimensions and scroll positions and correct if necessary */ - public void checkValid() + public void setBoxPositionByMouse(int x, int y) { + boxX = x; + boxY = y; if (boxY < 0) { boxY = 0; } - - if (boxY > (sequencesHeight - boxHeight)) + else if (boxY > (sequencesHeight - boxHeight)) { boxY = sequencesHeight - boxHeight + 1; } @@ -124,8 +127,7 @@ public class OverviewDimensions { boxX = 0; } - - if (boxX > (width - boxWidth)) + else if (boxX > (width - boxWidth)) { if (av.hasHiddenColumns()) { @@ -184,21 +186,12 @@ public class OverviewDimensions endSeq = av.getAlignment().getHiddenSequences() .adjustForHiddenSeqs(endSeq); - } boxX = (int) (startRes * av.getCharWidth() * scalew); boxY = (int) (startSeq * av.getCharHeight() * scaleh); - if (av.hasHiddenColumns()) - { - boxWidth = (int) ((endRes - startRes + 1) * av.getCharWidth() * scalew); - } - else - { - boxWidth = (int) ((endRes - startRes + 1) * av.getCharWidth() * scalew); - } - + boxWidth = (int) ((endRes - startRes + 1) * av.getCharWidth() * scalew); boxHeight = (int) ((endSeq - startSeq) * av.getCharHeight() * scaleh); } @@ -218,6 +211,18 @@ public class OverviewDimensions scaleh = (float) sequencesHeight / fullsizeHeight; } + /** + * Draw the overview panel's viewport box on a graphics object + * + * @param g + * the graphics object to draw on + */ + public void drawBox(Graphics g) + { + g.drawRect(boxX, boxY, boxWidth, boxHeight); + g.drawRect(boxX + 1, boxY + 1, boxWidth - 2, boxHeight - 2); + } + // don't like this, scroll vals are separate from setting code public int getScrollCol() { @@ -229,11 +234,15 @@ public class OverviewDimensions return scrollRow; } + // TODO should be removed, when unit test has mock Graphics object available + // to check boxX/boxY public int getBoxX() { return boxX; } + // TODO should be removed, when unit test has mock Graphics object available + // to check boxX/boxY public int getBoxY() { return boxY; diff --git a/test/jalview/viewmodel/OverviewDimensionsTest.java b/test/jalview/viewmodel/OverviewDimensionsTest.java index a9770bd..7c40fe9 100644 --- a/test/jalview/viewmodel/OverviewDimensionsTest.java +++ b/test/jalview/viewmodel/OverviewDimensionsTest.java @@ -257,7 +257,7 @@ public class OverviewDimensionsTest { @Test(groups = { "Functional" }) public void testSetBoxFromMouseClick() { - od.checkValid(); + od.setBoxPositionByMouse(0, 0); assertEquals(od.getBoxX(), 0); assertEquals(od.getBoxY(), 0); assertEquals(od.getBoxWidth(), boxWidth); @@ -347,7 +347,7 @@ public class OverviewDimensionsTest { @Test(groups = { "Functional" }) public void testFromMouseWithHiddenColsAtStart() { - od.checkValid(); + od.setBoxPositionByMouse(0, 0); assertEquals(od.getBoxX(), 0); assertEquals(od.getBoxY(), 0); assertEquals(od.getBoxWidth(), boxWidth); @@ -409,7 +409,7 @@ public class OverviewDimensionsTest { @Test(groups = { "Functional" }) public void testFromMouseWithHiddenColsInMiddle() { - od.checkValid(); + od.setBoxPositionByMouse(0, 0); assertEquals(od.getBoxX(), 0); assertEquals(od.getBoxY(), 0); assertEquals(od.getBoxWidth(), boxWidth); @@ -510,7 +510,7 @@ public class OverviewDimensionsTest { @Test(groups = { "Functional" }) public void testFromMouseWithHiddenColsAtEnd() { - od.checkValid(); + od.setBoxPositionByMouse(0, 0); assertEquals(od.getBoxX(), 0); assertEquals(od.getBoxY(), 0); assertEquals(od.getBoxWidth(), boxWidth); @@ -840,7 +840,7 @@ public class OverviewDimensionsTest { @Test(groups = { "Functional" }) public void testFromMouseWithHiddenRowsAtStart() { - od.checkValid(); + od.setBoxPositionByMouse(0, 0); assertEquals(od.getBoxX(), 0); assertEquals(od.getBoxY(), 0); assertEquals(od.getBoxWidth(), boxWidth); @@ -884,7 +884,7 @@ public class OverviewDimensionsTest { @Test(groups = { "Functional" }) public void testFromMouseWithHiddenRowsInMiddle() { - od.checkValid(); + od.setBoxPositionByMouse(0, 0); assertEquals(od.getBoxX(), 0); assertEquals(od.getBoxY(), 0); assertEquals(od.getBoxWidth(), boxWidth); @@ -941,7 +941,7 @@ public class OverviewDimensionsTest { @Test(groups = { "Functional" }) public void testFromMouseWithHiddenRowsAtEnd() { - od.checkValid(); + od.setBoxPositionByMouse(0, 0); assertEquals(od.getBoxX(), 0); assertEquals(od.getBoxY(), 0); assertEquals(od.getBoxWidth(), boxWidth); @@ -1036,9 +1036,7 @@ public class OverviewDimensionsTest { */ private void mouseClick(OverviewDimensions od, int x, int y) { - od.setBoxX(x); - od.setBoxY(y); - od.checkValid(); + od.setBoxPositionByMouse(x, y); // updates require an OverviewPanel to exist which it doesn't here // so call setBoxPosition() as it would be called by the AlignmentPanel // normally