diff --git a/extras/src/main/java/edu/umd/cs/piccolox/pswing/PSwing.java b/extras/src/main/java/edu/umd/cs/piccolox/pswing/PSwing.java index 31eecad..ba0f9cf 100644 --- a/extras/src/main/java/edu/umd/cs/piccolox/pswing/PSwing.java +++ b/extras/src/main/java/edu/umd/cs/piccolox/pswing/PSwing.java @@ -442,7 +442,10 @@ /** {@inheritDoc} */ public void setVisible(final boolean visible) { super.setVisible(visible); - component.setVisible(visible); + + if (component.isVisible() != visible) { + component.setVisible(visible); + } } /** diff --git a/extras/src/test/java/edu/umd/cs/piccolox/pswing/PSwingTest.java b/extras/src/test/java/edu/umd/cs/piccolox/pswing/PSwingTest.java index d9538f2..0ae6ae3 100644 --- a/extras/src/test/java/edu/umd/cs/piccolox/pswing/PSwingTest.java +++ b/extras/src/test/java/edu/umd/cs/piccolox/pswing/PSwingTest.java @@ -44,297 +44,300 @@ import edu.umd.cs.piccolo.util.PPaintContext; public class PSwingTest extends TestCase { - public void setUp() { - RepaintManager.setCurrentManager(new PSwingRepaintManager()); - } + public void setUp() { + RepaintManager.setCurrentManager(new PSwingRepaintManager()); + } - public void testConstructorFailsOnNullComponent() { - try { - new PSwing(null); - } - catch (final NullPointerException e) { - // expected - } - } + public void testConstructorFailsOnNullComponent() { + try { + new PSwing(null); + } catch (final NullPointerException e) { + // expected + } + } - public void testPSwingRegistersItselfWithComponent() { - final JPanel panel = new JPanel(); - final PSwing pSwing = new PSwing(panel); + public void testPSwingRegistersItselfWithComponent() { + final JPanel panel = new JPanel(); + final PSwing pSwing = new PSwing(panel); - assertEquals(pSwing, panel.getClientProperty(PSwing.PSWING_PROPERTY)); - } + assertEquals(pSwing, panel.getClientProperty(PSwing.PSWING_PROPERTY)); + } - public void testGetComponentReturnsValidComponent() { - final JPanel panel = new JPanel(); - final PSwing pSwing = new PSwing(panel); - assertEquals(panel, pSwing.getComponent()); - } + public void testGetComponentReturnsValidComponent() { + final JPanel panel = new JPanel(); + final PSwing pSwing = new PSwing(panel); + assertEquals(panel, pSwing.getComponent()); + } - public void testPSwingResizesItselfWhenComponentIsResized() { - final boolean[] reshaped = new boolean[1]; - final JPanel panel = new JPanel(); + public void testPSwingResizesItselfWhenComponentIsResized() { + final boolean[] reshaped = new boolean[1]; + final JPanel panel = new JPanel(); - new PSwing(panel) { - public void updateBounds() { - super.updateBounds(); + new PSwing(panel) { + public void updateBounds() { + super.updateBounds(); - reshaped[0] = true; - } - }; - panel.setSize(100, 100); - assertTrue(reshaped[0]); - } + reshaped[0] = true; + } + }; + panel.setSize(100, 100); + assertTrue(reshaped[0]); + } - public void testPSwingDelegatesPaintingToItsComponent() throws IOException { - final JPanel panel = new JPanel(); - final MockPaintingPSwing pSwing = new MockPaintingPSwing(panel); - panel.setBackground(Color.RED); - panel.setPreferredSize(new Dimension(100, 100)); + public void testPSwingDelegatesPaintingToItsComponent() throws IOException { + final JPanel panel = new JPanel(); + final MockPaintingPSwing pSwing = new MockPaintingPSwing(panel); + panel.setBackground(Color.RED); + panel.setPreferredSize(new Dimension(100, 100)); - final BufferedImage img = pSwing.paintComponent(); + final BufferedImage img = pSwing.paintComponent(); - assertEquals(Color.RED.getRGB(), img.getRGB(50, 50)); - } + assertEquals(Color.RED.getRGB(), img.getRGB(50, 50)); + } - public void testHidingComponentHidesPSwing() { - final JPanel panel = new JPanel(); - final PSwing pSwing = new PSwing(panel); - panel.setPreferredSize(new Dimension(100, 100)); - pSwing.setBounds(0, 0, 00, 100); - panel.setVisible(false); + public void testHidingComponentHidesPSwing() { + final JPanel panel = new JPanel(); + final PSwing pSwing = new PSwing(panel); + panel.setPreferredSize(new Dimension(100, 100)); + pSwing.setBounds(0, 0, 00, 100); + panel.setVisible(false); - // Wow, do I hate this chunk of code. Turns out that the event dispatch - // thread needs time to push the component hidden method before this - // test passes - // There has to be a way of forcing this without a sleep - assertDelayedSuccess("setting component to invisible did not reflect in associated PSwing", 500, - new Predicate() { + // Wow, do I hate this chunk of code. Turns out that the event dispatch + // thread needs time to push the component hidden method before this + // test passes + // There has to be a way of forcing this without a sleep + assertDelayedSuccess( + "setting component to invisible did not reflect in associated PSwing", + 500, new Predicate() { - public boolean isTrue() { - return !pSwing.getVisible(); - } - }); + public boolean isTrue() { + return !pSwing.getVisible(); + } + }); + } - } - - public void testAddingSwingComponentToWrappedHierarchyMakesItNotDoubleBuffer() { - final JPanel panel = new JPanel(); - final PSwing pSwing = new PSwing(panel); - final JComponent child = new JLabel("Test Component"); - child.setDoubleBuffered(true); - panel.add(child); - assertFalse(child.isDoubleBuffered()); - } + public void testHidingPNodeHidesComponent() { + final JPanel panel = new JPanel(); + final PSwing pSwing = new PSwing(panel); + pSwing.setVisible(false); + assertFalse(panel.isVisible()); + } - public void assertDelayedSuccess(String message, int delay, Predicate p) { - int remainingTries = delay / 50; - while (remainingTries > 0) { - if (p.isTrue()) { - return; - } - remainingTries--; - try { - Thread.sleep(50); - } - catch (InterruptedException e) { - // do nothing - } - } - fail(message); - } - - public void assertDelayedSuccess(int delay, Predicate p) { - assertDelayedSuccess("Failed asserting delayed success", delay, p); - } + public void testAddingSwingComponentToWrappedHierarchyMakesItNotDoubleBuffer() { + final JPanel panel = new JPanel(); + final PSwing pSwing = new PSwing(panel); + final JComponent child = new JLabel("Test Component"); + child.setDoubleBuffered(true); + panel.add(child); + assertFalse(child.isDoubleBuffered()); + } - private interface Predicate { - boolean isTrue(); - } + public void assertDelayedSuccess(String message, int delay, Predicate p) { + int remainingTries = delay / 50; + while (remainingTries > 0) { + if (p.isTrue()) { + return; + } + remainingTries--; + try { + Thread.sleep(50); + } catch (InterruptedException e) { + // do nothing + } + } + fail(message); + } - public void testHidingPNodeHidesComponent() { - final JPanel panel = new JPanel(); - final PSwing pSwing = new PSwing(panel); - pSwing.setVisible(false); - assertFalse(panel.isVisible()); - } + public void assertDelayedSuccess(int delay, Predicate p) { + assertDelayedSuccess("Failed asserting delayed success", delay, p); + } - public void testPaintTooSmallPaintsGreek() { - final JPanel panel = new JPanel(); - panel.setBounds(0, 0, 100, 100); - final MockPaintingPSwing pSwing = new MockPaintingPSwing(panel); + private interface Predicate { + boolean isTrue(); + } - BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); - Graphics2D graphics = image.createGraphics(); - graphics.setTransform(AffineTransform.getScaleInstance(0.01, 0.01)); - PPaintContext paintContext = new PPaintContext(graphics); + public void testPaintTooSmallPaintsGreek() { + final JPanel panel = new JPanel(); + panel.setBounds(0, 0, 100, 100); + final MockPaintingPSwing pSwing = new MockPaintingPSwing(panel); - pSwing.paint(paintContext); - assertTrue(pSwing.isPaintedGreek()); - assertFalse(pSwing.isPaintedComponent()); + BufferedImage image = new BufferedImage(100, 100, + BufferedImage.TYPE_INT_RGB); + Graphics2D graphics = image.createGraphics(); + graphics.setTransform(AffineTransform.getScaleInstance(0.01, 0.01)); + PPaintContext paintContext = new PPaintContext(graphics); - } + pSwing.paint(paintContext); + assertTrue(pSwing.isPaintedGreek()); + assertFalse(pSwing.isPaintedComponent()); - public void testPaintBigPaintsComponent() { - final JPanel panel = new JPanel(); - panel.setBounds(0, 0, 100, 100); - final MockPaintingPSwing pSwing = new MockPaintingPSwing(panel); + } - BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); - Graphics2D graphics = image.createGraphics(); - graphics.setTransform(AffineTransform.getScaleInstance(5, 5)); - PPaintContext paintContext = new PPaintContext(graphics); + public void testPaintBigPaintsComponent() { + final JPanel panel = new JPanel(); + panel.setBounds(0, 0, 100, 100); + final MockPaintingPSwing pSwing = new MockPaintingPSwing(panel); - pSwing.paint(paintContext); - assertFalse(pSwing.isPaintedGreek()); - assertTrue(pSwing.isPaintedComponent()); - } + BufferedImage image = new BufferedImage(100, 100, + BufferedImage.TYPE_INT_RGB); + Graphics2D graphics = image.createGraphics(); + graphics.setTransform(AffineTransform.getScaleInstance(5, 5)); + PPaintContext paintContext = new PPaintContext(graphics); - public void testGreekThresholdIsHonoured() { - final JPanel panel = new JPanel(); - panel.setBounds(0, 0, 100, 100); - final MockPaintingPSwing pSwing = new MockPaintingPSwing(panel); - pSwing.setGreekThreshold(2); - BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); - Graphics2D graphics = image.createGraphics(); - PPaintContext paintContext = new PPaintContext(graphics); + pSwing.paint(paintContext); + assertFalse(pSwing.isPaintedGreek()); + assertTrue(pSwing.isPaintedComponent()); + } - pSwing.paint(paintContext); - assertTrue(pSwing.isPaintedGreek()); - assertFalse(pSwing.isPaintedComponent()); - } + public void testGreekThresholdIsHonoured() { + final JPanel panel = new JPanel(); + panel.setBounds(0, 0, 100, 100); + final MockPaintingPSwing pSwing = new MockPaintingPSwing(panel); + pSwing.setGreekThreshold(2); + BufferedImage image = new BufferedImage(100, 100, + BufferedImage.TYPE_INT_RGB); + Graphics2D graphics = image.createGraphics(); + PPaintContext paintContext = new PPaintContext(graphics); - public void testGreekThresholdIsPersisted() { - final JPanel panel = new JPanel(); - final MockPaintingPSwing pSwing = new MockPaintingPSwing(panel); - pSwing.setGreekThreshold(2); - assertEquals(2, pSwing.getGreekThreshold(), Double.MIN_VALUE); - pSwing.setGreekThreshold(0.5); - assertEquals(0.5, pSwing.getGreekThreshold(), Double.MIN_VALUE); - } + pSwing.paint(paintContext); + assertTrue(pSwing.isPaintedGreek()); + assertFalse(pSwing.isPaintedComponent()); + } - public void testAssertSettingJLabelWidthTooSmallGrowsIt() { - final JLabel label = new JLabel("Hello"); - PSwingCanvas canvas = new PSwingCanvas(); - canvas.setBounds(0, 0, 100, 100); - final MockPaintingPSwing swing = new MockPaintingPSwing(label); - assertDelayedSuccess(500, - new Predicate() { + public void testGreekThresholdIsPersisted() { + final JPanel panel = new JPanel(); + final MockPaintingPSwing pSwing = new MockPaintingPSwing(panel); + pSwing.setGreekThreshold(2); + assertEquals(2, pSwing.getGreekThreshold(), Double.MIN_VALUE); + pSwing.setGreekThreshold(0.5); + assertEquals(0.5, pSwing.getGreekThreshold(), Double.MIN_VALUE); + } - public boolean isTrue() { - return label.getMinimumSize().getWidth() != 0; - } - }); - swing.setWidth(10); - canvas.getLayer().addChild(swing); - canvas.doLayout(); - // While paint, it uses the graphics element to determine the font's - // display size and hence determine minimum size of JLabel. - swing.paint(); - - assertFalse(10 == swing.getWidth()); - } + public void testAssertSettingJLabelWidthTooSmallGrowsIt() { + final JLabel label = new JLabel("Hello"); + PSwingCanvas canvas = new PSwingCanvas(); + canvas.setBounds(0, 0, 100, 100); + final MockPaintingPSwing swing = new MockPaintingPSwing(label); + assertDelayedSuccess(500, new Predicate() { - public void testAssertSettingJButtonWidthTooSmallGrowsIt() { - JButton label = new JButton("Hello"); - PSwingCanvas canvas = new PSwingCanvas(); - canvas.setBounds(0, 0, 100, 100); - MockPaintingPSwing swing = new MockPaintingPSwing(label); - assertFalse(label.getMinimumSize().getWidth() == 0); - swing.setWidth(10); - canvas.getLayer().addChild(swing); - canvas.doLayout(); - // While paint, it uses the graphics element to determine the font's - // display size and hence determine minimum size of JLabel. - swing.paint(); - assertFalse(10 == swing.getWidth()); - } + public boolean isTrue() { + return label.getMinimumSize().getWidth() != 0; + } + }); + swing.setWidth(10); + canvas.getLayer().addChild(swing); + canvas.doLayout(); + // While paint, it uses the graphics element to determine the font's + // display size and hence determine minimum size of JLabel. + swing.paint(); - public void testPSwingAttachesItselfToItsCanvasWhenAddedToItsSceneGraph() { - PSwingCanvas canvas1 = new PSwingCanvas(); - PSwing label = new PSwing(new JLabel("Hello")); - assertEquals(0, canvas1.getSwingWrapper().getComponentCount()); - canvas1.getLayer().addChild(label); - assertEquals(1, canvas1.getSwingWrapper().getComponentCount()); - } + assertFalse(10 == swing.getWidth()); + } - public void testPSwingRemovesItselfFromItsCanvasWhenRemovedFromScene() { - PSwingCanvas canvas1 = new PSwingCanvas(); - PSwing label = new PSwing(new JLabel("Hello")); - canvas1.getLayer().addChild(label); - assertEquals(1, canvas1.getSwingWrapper().getComponentCount()); - label.removeFromParent(); - assertEquals(0, canvas1.getSwingWrapper().getComponentCount()); - } + public void testAssertSettingJButtonWidthTooSmallGrowsIt() { + JButton label = new JButton("Hello"); + PSwingCanvas canvas = new PSwingCanvas(); + canvas.setBounds(0, 0, 100, 100); + MockPaintingPSwing swing = new MockPaintingPSwing(label); + assertFalse(label.getMinimumSize().getWidth() == 0); + swing.setWidth(10); + canvas.getLayer().addChild(swing); + canvas.doLayout(); + // While paint, it uses the graphics element to determine the font's + // display size and hence determine minimum size of JLabel. + swing.paint(); + assertFalse(10 == swing.getWidth()); + } - public void testPSwingReattachesItselfWhenMovedFromCanvasToCanvas() { - PSwingCanvas canvas1 = new PSwingCanvas(); - PSwingCanvas canvas2 = new PSwingCanvas(); - PSwing label = new PSwing(new JLabel("Hello")); - canvas1.getLayer().addChild(label); - canvas2.getLayer().addChild(label); - assertEquals(0, canvas1.getSwingWrapper().getComponentCount()); - assertEquals(1, canvas2.getSwingWrapper().getComponentCount()); - } - - public void testPSwingRegistersWithCanvasThroughoutItsLifeCycle() { - PSwingCanvas canvas = new PSwingCanvas(); - PSwing label = new PSwing(new JLabel("Hello")); - - canvas.getLayer().addChild(label); - assertEquals(1, canvas.getSwingWrapper().getComponentCount()); - - label.removeFromParent(); - assertEquals(0, canvas.getSwingWrapper().getComponentCount()); - - canvas.getLayer().addChild(label); - assertEquals(1, canvas.getSwingWrapper().getComponentCount()); - } + public void testPSwingAttachesItselfToItsCanvasWhenAddedToItsSceneGraph() { + PSwingCanvas canvas1 = new PSwingCanvas(); + PSwing label = new PSwing(new JLabel("Hello")); + assertEquals(0, canvas1.getSwingWrapper().getComponentCount()); + canvas1.getLayer().addChild(label); + assertEquals(1, canvas1.getSwingWrapper().getComponentCount()); + } - public class MockPaintingPSwing extends PSwing { - private boolean paintedGreek; - private boolean paintedComponent; + public void testPSwingRemovesItselfFromItsCanvasWhenRemovedFromScene() { + PSwingCanvas canvas1 = new PSwingCanvas(); + PSwing label = new PSwing(new JLabel("Hello")); + canvas1.getLayer().addChild(label); + assertEquals(1, canvas1.getSwingWrapper().getComponentCount()); + label.removeFromParent(); + assertEquals(0, canvas1.getSwingWrapper().getComponentCount()); + } - public MockPaintingPSwing(JComponent component) { - super(component); - } + public void testPSwingReattachesItselfWhenMovedFromCanvasToCanvas() { + PSwingCanvas canvas1 = new PSwingCanvas(); + PSwingCanvas canvas2 = new PSwingCanvas(); + PSwing label = new PSwing(new JLabel("Hello")); + canvas1.getLayer().addChild(label); + canvas2.getLayer().addChild(label); + assertEquals(0, canvas1.getSwingWrapper().getComponentCount()); + assertEquals(1, canvas2.getSwingWrapper().getComponentCount()); + } - public void paintOnto(BufferedImage image) { - PPaintContext paintContext = new PPaintContext(image.createGraphics()); - paint(paintContext); - } + public void testPSwingRegistersWithCanvasThroughoutItsLifeCycle() { + PSwingCanvas canvas = new PSwingCanvas(); + PSwing label = new PSwing(new JLabel("Hello")); - public BufferedImage paint() { - BufferedImage image = new BufferedImage((int) getWidth(), (int) getHeight(), BufferedImage.TYPE_INT_RGB); - paintOnto(image); - return image; - } + canvas.getLayer().addChild(label); + assertEquals(1, canvas.getSwingWrapper().getComponentCount()); - public BufferedImage paintComponent() { - BufferedImage image = new BufferedImage((int) getWidth(), (int) getHeight(), BufferedImage.TYPE_INT_RGB); - paintComponentOnto(image); - return image; - } + label.removeFromParent(); + assertEquals(0, canvas.getSwingWrapper().getComponentCount()); - public void paintComponentOnto(BufferedImage image) { - paint(image.createGraphics()); - } + canvas.getLayer().addChild(label); + assertEquals(1, canvas.getSwingWrapper().getComponentCount()); + } - public void paint(Graphics2D paintContext) { - super.paint(paintContext); - paintedComponent = true; - } + public class MockPaintingPSwing extends PSwing { + private boolean paintedGreek; + private boolean paintedComponent; - public void paintAsGreek(Graphics2D paintContext) { - super.paintAsGreek(paintContext); - paintedGreek = true; - } + public MockPaintingPSwing(JComponent component) { + super(component); + } - public boolean isPaintedGreek() { - return paintedGreek; - } + public void paintOnto(BufferedImage image) { + PPaintContext paintContext = new PPaintContext(image + .createGraphics()); + paint(paintContext); + } - public boolean isPaintedComponent() { - return paintedComponent; - } - } + public BufferedImage paint() { + BufferedImage image = new BufferedImage((int) getWidth(), + (int) getHeight(), BufferedImage.TYPE_INT_RGB); + paintOnto(image); + return image; + } + + public BufferedImage paintComponent() { + BufferedImage image = new BufferedImage((int) getWidth(), + (int) getHeight(), BufferedImage.TYPE_INT_RGB); + paintComponentOnto(image); + return image; + } + + public void paintComponentOnto(BufferedImage image) { + paint(image.createGraphics()); + } + + public void paint(Graphics2D paintContext) { + super.paint(paintContext); + paintedComponent = true; + } + + public void paintAsGreek(Graphics2D paintContext) { + super.paintAsGreek(paintContext); + paintedGreek = true; + } + + public boolean isPaintedGreek() { + return paintedGreek; + } + + public boolean isPaintedComponent() { + return paintedComponent; + } + } }