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 483cad1..23c08e4 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 @@ -240,11 +240,25 @@ component.putClientProperty( PSWING_PROPERTY, this ); init( component ); component.revalidate(); + component.addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange( PropertyChangeEvent evt ) { reshape(); } } ); + + component.addComponentListener(new ComponentAdapter() { + public void componentHidden(ComponentEvent e) { + System.out.println("Hiding component"); + setVisible(false); + } + + public void componentShown(ComponentEvent e) { + System.out.println("Showing component"); + setVisible(true); + } + }); + reshape(); listenForCanvas( this ); } @@ -388,6 +402,11 @@ manager.unlockRepaint( component ); } + + public void setVisible(boolean visible) { + super.setVisible(visible); + component.setVisible(visible); + } /** * Tells whether the buffer for the image of the Swing components @@ -497,7 +516,7 @@ init( component ); } - //////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////// ///////Start methods for automatic canvas detection //////////////////////////////////////////////////////////// /** 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 83fef78..f8fcfe5 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 @@ -110,4 +110,25 @@ pSwing.paint(graphics); assertEquals(Color.RED.getRGB(), img.getRGB(50, 50)); } + + public void testHidingComponentHidesPSwing() throws InterruptedException { + JPanel panel = new JPanel(); + 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 next line. 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 + Thread.sleep(50); + assertFalse(pSwing.getVisible()); + } + + public void testHidingPNodeHidesComponent() { + JPanel panel = new JPanel(); + PSwing pSwing = new PSwing(panel); + pSwing.setVisible(false); + assertFalse(panel.isVisible()); + } }