Revision: 947
Author: allain.lalonde
Date: Sat Jan 23 17:27:27 2010
Log: Scrubbing PNode so for some slight optimzations and clearer flow control.
http://code.google.com/p/piccolo2d/source/detail?r=947

Modified:
/piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PNode.java

=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PNode.java Wed Jan 20 14:13:34 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PNode.java Sat Jan 23 17:27:27 2010
@@ -112,18 +112,18 @@
     private static final long serialVersionUID = 1L;

     /**
-     * The property name that identifies a change in this node's client
-     * property (see {...@link #getClientProperty getClientProperty}). In an
- * property change event the new value will be a reference to the map of
-     * client properties but old value will always be null.
+ * The property name that identifies a change in this node's client property + * (see {...@link #getClientProperty getClientProperty}). In an property change + * event the new value will be a reference to the map of client properties
+     * but old value will always be null.
      */
public static final String PROPERTY_CLIENT_PROPERTIES = "clientProperties";

     /**
-     * The property code that identifies a change in this node's client
-     * property (see {...@link #getClientProperty getClientProperty}). In an
- * property change event the new value will be a reference to the map of
-     * client properties but old value will always be null.
+ * The property code that identifies a change in this node's client property + * (see {...@link #getClientProperty getClientProperty}). In an property change + * event the new value will be a reference to the map of client properties
+     * but old value will always be null.
      */
     public static final int PROPERTY_CODE_CLIENT_PROPERTIES = 1 << 0;

@@ -397,8 +397,8 @@
             return null;
         }
     };
-
- private static final List<PNode> EMPTY_CHILDREN = Collections.<PNode>emptyList();
+
+ private static final List<PNode> EMPTY_CHILDREN = Collections.<PNode> emptyList();

     /**
      * Creates a new PNode with the given name.
@@ -493,6 +493,7 @@
         };

         addActivity(interpolatingActivity);
+
         return interpolatingActivity;
     }

@@ -570,22 +571,21 @@
             setTransform(destTransform);
             return null;
         }
-        else {
- final PTransformActivity.Target t = new PTransformActivity.Target() { - public void setTransform(final AffineTransform aTransform) {
-                    PNode.this.setTransform(aTransform);
-                }
-
-                public void getSourceMatrix(final double[] aSource) {
- PNode.this.getTransformReference(true).getMatrix(aSource);
-                }
-            };
-
- final PTransformActivity ta = new PTransformActivity(duration, PUtil.DEFAULT_ACTIVITY_STEP_RATE, t,
-                    destTransform);
-            addActivity(ta);
-            return ta;
-        }
+
+ final PTransformActivity.Target t = new PTransformActivity.Target() {
+            public void setTransform(final AffineTransform aTransform) {
+                PNode.this.setTransform(aTransform);
+            }
+
+            public void getSourceMatrix(final double[] aSource) {
+                PNode.this.getTransformReference(true).getMatrix(aSource);
+            }
+        };
+
+ final PTransformActivity ta = new PTransformActivity(duration, PUtil.DEFAULT_ACTIVITY_STEP_RATE, t,
+                destTransform);
+        addActivity(ta);
+        return ta;
     }

     /**
@@ -607,21 +607,20 @@
             setPaint(destColor);
             return null;
         }
-        else {
-            final PColorActivity.Target t = new PColorActivity.Target() {
-                public Color getColor() {
-                    return (Color) getPaint();
-                }
-
-                public void setColor(final Color color) {
-                    setPaint(color);
-                }
-            };
-
- final PColorActivity ca = new PColorActivity(duration, PUtil.DEFAULT_ACTIVITY_STEP_RATE, t, destColor);
-            addActivity(ca);
-            return ca;
-        }
+
+        final PColorActivity.Target t = new PColorActivity.Target() {
+            public Color getColor() {
+                return (Color) getPaint();
+            }
+
+            public void setColor(final Color color) {
+                setPaint(color);
+            }
+        };
+
+ final PColorActivity ca = new PColorActivity(duration, PUtil.DEFAULT_ACTIVITY_STEP_RATE, t, destColor);
+        addActivity(ca);
+        return ca;
     }

     /**
@@ -643,25 +642,24 @@
             setTransparency(zeroToOne);
             return null;
         }
-        else {
-            final float dest = zeroToOne;
-
- final PInterpolatingActivity ta = new PInterpolatingActivity(duration, PUtil.DEFAULT_ACTIVITY_STEP_RATE) {
-                private float source;
-
-                protected void activityStarted() {
-                    source = getTransparency();
-                    super.activityStarted();
-                }
-
-                public void setRelativeTargetValue(final float zeroToOne) {
- PNode.this.setTransparency(source + zeroToOne * (dest - source));
-                }
-            };
-
-            addActivity(ta);
-            return ta;
-        }
+
+        final float dest = zeroToOne;
+
+ final PInterpolatingActivity ta = new PInterpolatingActivity(duration, PUtil.DEFAULT_ACTIVITY_STEP_RATE) {
+            private float source;
+
+            protected void activityStarted() {
+                source = getTransparency();
+                super.activityStarted();
+            }
+
+            public void setRelativeTargetValue(final float zeroToOne) {
+ PNode.this.setTransparency(source + zeroToOne * (dest - source));
+            }
+        };
+
+        addActivity(ta);
+        return ta;
     }

     /**
@@ -674,10 +672,11 @@
      */
     public boolean addActivity(final PActivity activity) {
         final PRoot r = getRoot();
-        if (r != null) {
-            return r.addActivity(activity);
-        }
-        return false;
+        if (r == null) {
+            return false;
+        }
+
+        return r.addActivity(activity);
     }

     // ****************************************************************
@@ -715,9 +714,8 @@
         if (clientProperties == null || key == null) {
             return null;
         }
-        else {
-            return clientProperties.getAttribute(key);
-        }
+
+        return clientProperties.getAttribute(key);
     }

     /**
@@ -733,31 +731,33 @@
      * @param value value to associate to the new attribute
      */
     public void addAttribute(final Object key, final Object value) {
-        if (value == null && clientProperties == null) {
-            return;
-        }
-
-        final Object oldValue = getAttribute(key);
-
-        if (value != oldValue) {
-            if (clientProperties == null) {
-                clientProperties = new SimpleAttributeSet();
-            }
-
-            if (value == null) {
-                clientProperties.removeAttribute(key);
-            }
-            else {
-                clientProperties.addAttribute(key, value);
-            }
-
- if (clientProperties.getAttributeCount() == 0 && clientProperties.getResolveParent() == null) {
-                clientProperties = null;
-            }
-
- firePropertyChange(PROPERTY_CODE_CLIENT_PROPERTIES, PROPERTY_CLIENT_PROPERTIES, null, clientProperties); - firePropertyChange(PROPERTY_CODE_CLIENT_PROPERTIES, key.toString(), oldValue, value);
-        }
+        if (value != null || clientProperties != null) {
+            final Object oldValue = getAttribute(key);
+
+            if (value != oldValue) {
+                addOrChangeAttribute(key, value, oldValue);
+            }
+        }
+    }
+
+ private void addOrChangeAttribute(final Object key, final Object value, final Object oldValue) {
+        if (clientProperties == null) {
+            clientProperties = new SimpleAttributeSet();
+        }
+
+        if (value == null) {
+            clientProperties.removeAttribute(key);
+        }
+        else {
+            clientProperties.addAttribute(key, value);
+        }
+
+ if (clientProperties.getAttributeCount() == 0 && clientProperties.getResolveParent() == null) {
+            clientProperties = null;
+        }
+
+ firePropertyChange(PROPERTY_CODE_CLIENT_PROPERTIES, PROPERTY_CLIENT_PROPERTIES, null, clientProperties); + firePropertyChange(PROPERTY_CODE_CLIENT_PROPERTIES, key.toString(), oldValue, value);
     }

     /**
@@ -773,8 +773,6 @@
             return clientProperties.getAttributeNames();
         }
     }
-
-    // convenience methods for attributes

     /**
* Fetches the value of the requested attribute, returning defaultValue is
@@ -1014,10 +1012,12 @@
      */
     public Point2D localToGlobal(final Point2D localPoint) {
         PNode n = this;
-        while (n != null) {
+
+        do {
             n.localToParent(localPoint);
             n = n.parent;
-        }
+        } while (n != null);
+
         return localPoint;
     }

@@ -1032,10 +1032,12 @@
      */
     public Dimension2D localToGlobal(final Dimension2D localDimension) {
         PNode n = this;
-        while (n != null) {
+
+        do {
             n.localToParent(localDimension);
             n = n.parent;
-        }
+        } while (n != null);
+
         return localDimension;
     }

@@ -1533,7 +1535,7 @@
     }

     /**
-     * Return the height (in local coords) of this node's bounds.
+     * Return the height (in local coordinates) of this node.
      *
      * @return local width of bounds
      */
@@ -3151,7 +3153,7 @@
      * @param child the new child to add to this node
      */
     public void addChild(final PNode child) {
-        int insertIndex = getChildrenCount();
+        int insertIndex = children.size();
         if (child.parent == this) {
             insertIndex--;
         }
@@ -3167,16 +3169,14 @@
      * @param child the new child to add to this node
      */
     public void addChild(final int index, final PNode child) {
-        final PNode oldParent = child.getParent();
-
-        if (oldParent != null) {
-            oldParent.removeChild(child);
-        }
+        child.removeFromParent();

         child.setParent(this);
+
         if (children == Collections.EMPTY_LIST) {
             children = new LinkedList<PNode>();
         }
+
         children.add(index, child);
         child.invalidatePaint();
         invalidateFullBounds();

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

Reply via email to