Revision: 736 Author: allain.lalonde Date: Fri Oct 16 12:15:32 2009 Log: Fixing some checkstyle warnings and adding more tests for PSWTHandle. http://code.google.com/p/piccolo2d/source/detail?r=736
Modified: /piccolo2d.java/trunk/swt/src/main/java/edu/umd/cs/piccolox/swt/PSWTBoundsHandle.java /piccolo2d.java/trunk/swt/src/main/java/edu/umd/cs/piccolox/swt/PSWTHandle.java /piccolo2d.java/trunk/swt/src/main/java/edu/umd/cs/piccolox/swt/PSWTImage.java /piccolo2d.java/trunk/swt/src/test/java/edu/umd/cs/piccolox/swt/PSWTHandleTest.java ======================================= --- /piccolo2d.java/trunk/swt/src/main/java/edu/umd/cs/piccolox/swt/PSWTBoundsHandle.java Wed Oct 14 14:57:34 2009 +++ /piccolo2d.java/trunk/swt/src/main/java/edu/umd/cs/piccolox/swt/PSWTBoundsHandle.java Fri Oct 16 12:15:32 2009 @@ -279,6 +279,14 @@ l.getNode().endResizeBounds(); } + /** + * Iterates over all of this node's handles flipping them if necessary. This + * is needed since a node can become inverted when it's width or height + * becomes negative. + * + * @param flipX whether to allow flipping in the horizontal direction + * @param flipY whether to allow flipping in the vertical direction + */ public void flipSiblingBoundsHandles(final boolean flipX, final boolean flipY) { final Iterator i = getParent().getChildrenIterator(); while (i.hasNext()) { @@ -290,8 +298,8 @@ } /** - * Flips the handles around if needed. this is necessary since a node can - * become inverted when it's width or height becomes negative. + * Flips this particular handle around if needed. This is necessary since a + * node can become inverted when it's width or height becomes negative. * * @param flipX whether to allow flipping in the horizontal direction * @param flipY whether to allow flipping in the vertical direction ======================================= --- /piccolo2d.java/trunk/swt/src/main/java/edu/umd/cs/piccolox/swt/PSWTHandle.java Tue Jul 28 12:46:54 2009 +++ /piccolo2d.java/trunk/swt/src/main/java/edu/umd/cs/piccolox/swt/PSWTHandle.java Fri Oct 16 12:15:32 2009 @@ -177,11 +177,13 @@ // position. // **************************************************************** + /** {...@inheritdoc} */ public void setParent(final PNode newParent) { - super.setParent(newParent); + super.setParent(newParent); relocateHandle(); } + /** {...@inheritdoc} */ public void parentBoundsChanged() { relocateHandle(); } ======================================= --- /piccolo2d.java/trunk/swt/src/main/java/edu/umd/cs/piccolox/swt/PSWTImage.java Fri Oct 16 09:14:56 2009 +++ /piccolo2d.java/trunk/swt/src/main/java/edu/umd/cs/piccolox/swt/PSWTImage.java Fri Oct 16 12:15:32 2009 @@ -45,130 +45,128 @@ * @author Jesse Grosjean */ public class PSWTImage extends PNode { - private static final long serialVersionUID = 1L; - - private transient final PSWTCanvas canvas; - - private transient Image image; - - /** - * Constructs a PSWTImage attached to the provided canvas and with a null - * image. - * - * The developer will need to call setImage for this node to be useful. - * - * TODO: determine if canvas is actually necessary - * - * @param canvas - * canvas to associate with the image node - */ - public PSWTImage(final PSWTCanvas canvas) { - this.canvas = canvas; - canvas.addDisposeListener(new DisposeListener() { - public void widgetDisposed(final DisposeEvent de) { - disposeImage(); - } - }); - } - - /** - * Constructs a PSWTImage wrapping the provided image. - * - * @param canvas - * canvas to associate with the image node - * @param image - * image to be wrapped by this PSWTImage - */ - public PSWTImage(final PSWTCanvas canvas, final Image image) { - this(canvas); - setImage(image); - } - - /** - * Constructs a PSWTImage wrapping the provided image after loading it from - * the file. - * - * @param canvas - * canvas to associate with the image node - * @param fileName - * path to the image, will be loaded and converted to an Image - * internally - */ - public PSWTImage(final PSWTCanvas canvas, final String fileName) { - this(canvas); - setImage(fileName); - } - - /** - * Returns the image that is shown by this node, may be null. - * - * @return the image that is shown by this node - */ - public Image getImage() { - return image; - } - - /** - * Set the image that is wrapped by this PImage node. This method will also - * load the image using a MediaTracker before returning. And if the this - * PImage is accelerated that image will be copied into an accelerated image - * if needed. Note that this may cause undesired results with images that - * have transparent regions, for those cases you may want to set the PImage - * to be not accelerated. - */ - public void setImage(final String fileName) { - setImage(new Image(canvas.getDisplay(), fileName)); - } - - /** - * Set the image that is wrapped by this PImage node. This method will also - * load the image using a MediaTracker before returning. And if the this - * PImage is accelerated that I'm will be copied into an accelerated image - * if needed. Note that this may cause undesired results with images that - * have transparent regions, for those cases you may want to set the PImage - * to be not accelerated. - */ - public void setImage(final Image newImage) { - final Image old = image; - image = newImage; - - if (image != null) { - final Rectangle bounds = getImage().getBounds(); - setBounds(0, 0, bounds.width, bounds.height); - invalidatePaint(); - } - - firePropertyChange(PImage.PROPERTY_CODE_IMAGE, PImage.PROPERTY_IMAGE, - old, image); - } - - /** - * Subclasses may override this method to provide different image dispose - * behavior. - */ - protected void disposeImage() { - if (image != null) { - image.dispose(); - } - } - - /** {...@inheritdoc} */ - protected void paint(final PPaintContext paintContext) { - if (getImage() != null) { - final Rectangle r = image.getBounds(); - final PBounds b = getBoundsReference(); - final SWTGraphics2D g2 = (SWTGraphics2D) paintContext.getGraphics(); - - if (b.x == 0 && b.y == 0 && b.width == r.width - && b.height == r.height) { - g2.drawImage(image, 0, 0); - } else { - g2.translate(b.x, b.y); - g2.scale(b.width / r.width, b.height / r.height); - g2.drawImage(image, 0, 0); - g2.scale(r.width / b.width, r.height / b.height); - g2.translate(-b.x, -b.y); - } - } - } -} + private static final long serialVersionUID = 1L; + + private final transient PSWTCanvas canvas; + + private transient Image image; + + /** + * Constructs a PSWTImage attached to the provided canvas and with a null + * image. + * + * The developer will need to call setImage for this node to be useful. + * + * TODO: determine if canvas is actually necessary + * + * @param canvas canvas to associate with the image node + */ + public PSWTImage(final PSWTCanvas canvas) { + this.canvas = canvas; + canvas.addDisposeListener(new DisposeListener() { + public void widgetDisposed(final DisposeEvent de) { + disposeImage(); + } + }); + } + + /** + * Constructs a PSWTImage wrapping the provided image. + * + * @param canvas canvas to associate with the image node + * @param image image to be wrapped by this PSWTImage + */ + public PSWTImage(final PSWTCanvas canvas, final Image image) { + this(canvas); + setImage(image); + } + + /** + * Constructs a PSWTImage wrapping the provided image after loading it from + * the file. + * + * @param canvas canvas to associate with the image node + * @param fileName path to the image, will be loaded and converted to an + * Image internally + */ + public PSWTImage(final PSWTCanvas canvas, final String fileName) { + this(canvas); + setImage(fileName); + } + + /** + * Returns the image that is shown by this node, may be null. + * + * @return the image that is shown by this node + */ + public Image getImage() { + return image; + } + + /** + * Set the image that is wrapped by this PImage node. This method will also + * load the image using a MediaTracker before returning. And if the this + * PImage is accelerated that image will be copied into an accelerated image + * if needed. Note that this may cause undesired results with images that + * have transparent regions, for those cases you may want to set the PImage + * to be not accelerated. + * + * @param filePath path to the file to load as an image + */ + public void setImage(final String filePath) { + setImage(new Image(canvas.getDisplay(), filePath)); + } + + /** + * Set the image that is wrapped by this PImage node. This method will also + * load the image using a MediaTracker before returning. And if the this + * PImage is accelerated that I'm will be copied into an accelerated image + * if needed. Note that this may cause undesired results with images that + * have transparent regions, for those cases you may want to set the PImage + * to be not accelerated. + * + * @param newImage the image that should replace the current one + */ + public void setImage(final Image newImage) { + final Image old = image; + image = newImage; + + if (image != null) { + final Rectangle bounds = getImage().getBounds(); + setBounds(0, 0, bounds.width, bounds.height); + invalidatePaint(); + } + + firePropertyChange(PImage.PROPERTY_CODE_IMAGE, PImage.PROPERTY_IMAGE, old, image); + } + + /** + * Subclasses may override this method to provide different image dispose + * behavior. + */ + protected void disposeImage() { + if (image != null) { + image.dispose(); + } + } + + /** {...@inheritdoc} */ + protected void paint(final PPaintContext paintContext) { + if (getImage() != null) { + final Rectangle r = image.getBounds(); + final PBounds b = getBoundsReference(); + final SWTGraphics2D g2 = (SWTGraphics2D) paintContext.getGraphics(); + + if (b.x == 0 && b.y == 0 && b.width == r.width && b.height == r.height) { + g2.drawImage(image, 0, 0); + } + else { + g2.translate(b.x, b.y); + g2.scale(b.width / r.width, b.height / r.height); + g2.drawImage(image, 0, 0); + g2.scale(r.width / b.width, r.height / b.height); + g2.translate(-b.x, -b.y); + } + } + } +} ======================================= --- /piccolo2d.java/trunk/swt/src/test/java/edu/umd/cs/piccolox/swt/PSWTHandleTest.java Fri Oct 16 09:32:38 2009 +++ /piccolo2d.java/trunk/swt/src/test/java/edu/umd/cs/piccolox/swt/PSWTHandleTest.java Fri Oct 16 12:15:32 2009 @@ -5,46 +5,62 @@ import edu.umd.cs.piccolox.util.PBoundsLocator; import edu.umd.cs.piccolox.util.PLocator; - public class PSWTHandleTest extends SWTTest { - private PNode node; - private PSWTHandle handle; - private PBoundsLocator locator; - - public void setUp() throws Exception { - if (hasHead()) { - node = new PNode(); - node.setBounds(0, 0, 100, 100); - locator = PBoundsLocator.createEastLocator(node); - handle = new PSWTHandle(locator); - } - } - - public void testDefaultsAreCorrect() { - if (hasHead()) { - assertEquals(PSWTHandle.DEFAULT_COLOR, handle.getPaint()); - assertEquals(PSWTHandle.DEFAULT_HANDLE_SIZE + 2 /** for border pen */, handle.getHeight(), Float.MIN_VALUE); - } - } - - public void testLocatorPersists() { - if (hasHead()) { - assertSame(locator, handle.getLocator()); - - PLocator newLocator = PBoundsLocator.createWestLocator(node); - handle.setLocator(newLocator); - assertSame(newLocator, handle.getLocator()); - } - } - - public void testHandleHasDragHandlerInstalled() { - if (hasHead()) { - PInputEventListener dragHandler = handle.getHandleDraggerHandler(); - assertNotNull(dragHandler); - - PInputEventListener[] installedListeners = handle.getInputEventListeners(); - assertEquals(1, installedListeners.length); - assertSame(dragHandler, installedListeners[0]); - } - } -} + private PNode node; + private PSWTHandle handle; + private PBoundsLocator locator; + + public void setUp() throws Exception { + if (hasHead()) { + node = new PNode(); + locator = PBoundsLocator.createEastLocator(node); + handle = new PSWTHandle(locator); + node.setBounds(0, 0, 100, 100); + node.addChild(handle); + } + } + + public void testDefaultsAreCorrect() { + if (hasHead()) { + assertEquals(PSWTHandle.DEFAULT_COLOR, handle.getPaint()); + assertEquals(PSWTHandle.DEFAULT_HANDLE_SIZE + 2 /** for border pen */ + , handle.getHeight(), Float.MIN_VALUE); + } + } + + public void testLocatorPersists() { + if (hasHead()) { + assertSame(locator, handle.getLocator()); + + PLocator newLocator = PBoundsLocator.createWestLocator(node); + handle.setLocator(newLocator); + assertSame(newLocator, handle.getLocator()); + } + } + + public void testHandleHasDragHandlerInstalled() { + if (hasHead()) { + PInputEventListener dragHandler = handle.getHandleDraggerHandler(); + assertNotNull(dragHandler); + + PInputEventListener[] installedListeners = handle.getInputEventListeners(); + assertEquals(1, installedListeners.length); + assertSame(dragHandler, installedListeners[0]); + } + } + + public void testChangingParentDoesNotChangeLocatorNode() { + if (hasHead()) { + handle.relocateHandle(); + PNode newParent = new PNode(); + newParent.setBounds(50, 50, 100, 100); + + final double originalX = handle.getX(); + handle.setParent(newParent); + + final double newX = handle.getX(); + + assertEquals(newX, originalX, Double.MIN_VALUE); + } + } +} --~--~---------~--~----~------------~-------~--~----~ Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en -~----------~----~----~----~------~----~------~--~---