Author: allain.lalonde
Date: Fri Jul 17 17:11:06 2009
New Revision: 496

Modified:
    piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/PCanvas.java

Log:
Fixed a memory leak with PCanvas instances registering themselves with  
KeyboardFocus and never unregistering themselves.

Added a Hierarchy listener so that the inputsources are installeded and  
uninstalled when a canvas gets added or removed to a parent.

Modified:  
piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/PCanvas.java
==============================================================================
--- piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/PCanvas.java     
 
(original)
+++ piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/PCanvas.java     
 
Fri Jul 17 17:11:06 2009
@@ -35,6 +35,10 @@
  import java.awt.Graphics2D;
  import java.awt.KeyEventPostProcessor;
  import java.awt.event.ActionListener;
+import java.awt.event.ComponentAdapter;
+import java.awt.event.ComponentEvent;
+import java.awt.event.HierarchyEvent;
+import java.awt.event.HierarchyListener;
  import java.awt.event.InputEvent;
  import java.awt.event.KeyEvent;
  import java.awt.event.MouseEvent;
@@ -42,6 +46,8 @@
  import java.awt.event.MouseMotionListener;
  import java.awt.event.MouseWheelEvent;
  import java.awt.event.MouseWheelListener;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;

  import javax.swing.FocusManager;
  import javax.swing.JComponent;
@@ -90,21 +96,31 @@

      /**
       * Construct a canvas with the basic scene graph consisting of a root,
-     * camera, and layer. Event handlers for zooming and panning are
-     * automatically installed.
+     * camera, and layer. Zooming and panning are automatically installed.
       */
      public PCanvas() {
          CURRENT_ZCANVAS = this;
          cursorStack = new PStack();
-        setCamera(createDefaultCamera());
-        installInputSources();
+        setCamera(createDefaultCamera());
          setDefaultRenderQuality(PPaintContext.HIGH_QUALITY_RENDERING);
          setAnimatingRenderQuality(PPaintContext.LOW_QUALITY_RENDERING);
          setInteractingRenderQuality(PPaintContext.LOW_QUALITY_RENDERING);
          setPanEventHandler(new PPanEventHandler());
          setZoomEventHandler(new PZoomEventHandler());
          setBackground(Color.WHITE);
-        setOpaque( true );
+        setOpaque(true);
+
+        addHierarchyListener(new HierarchyListener() {
+            public void hierarchyChanged(HierarchyEvent e) {
+                if (e.getComponent() == PCanvas.this) {
+                    if (getParent() == null) {
+                        removeInputSources();
+                    } else if (isEnabled()) {
+                        installInputSources();
+                    }
+                }
+            }
+        });
      }

      protected PCamera createDefaultCamera() {
@@ -353,7 +369,7 @@
      public void setEnabled(boolean enabled) {
          super.setEnabled(enabled);

-        if (isEnabled()) {
+        if (isEnabled() && getParent() != null) {
              installInputSources();
          }
          else {
@@ -608,12 +624,13 @@
          PDebug.startProcessingOutput();

          Graphics2D g2 = (Graphics2D) g.create();
-
-               // support for non-opaque canvases
-        // see  
http://groups.google.com/group/piccolo2d-dev/browse_thread/thread/134e2792d3a54cf
-        if ( isOpaque() ) {
-            g2.setColor( getBackground() );
-            g2.fillRect( 0, 0, getWidth(), getHeight() );
+
+        // support for non-opaque canvases
+        // see
+        //  
http://groups.google.com/group/piccolo2d-dev/browse_thread/thread/134e2792d3a54cf
+        if (isOpaque()) {
+            g2.setColor(getBackground());
+            g2.fillRect(0, 0, getWidth(), getHeight());
          }

          // create new paint context and set render quality to lowest common

--~--~---------~--~----~------------~-------~--~----~
Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to