You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by tv...@apache.org on 2009/05/12 16:04:58 UTC

svn commit: r773904 - /incubator/pivot/trunk/wtk/src/pivot/wtk/Container.java

Author: tvolkert
Date: Tue May 12 14:04:58 2009
New Revision: 773904

URL: http://svn.apache.org/viewvc?rev=773904&view=rev
Log:
Added check in Container#insert() to prevent callers from adding an ancestor as a child

Modified:
    incubator/pivot/trunk/wtk/src/pivot/wtk/Container.java

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/Container.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/Container.java?rev=773904&r1=773903&r2=773904&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/Container.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/Container.java Tue May 12 14:04:58 2009
@@ -152,17 +152,15 @@
             throw new IllegalArgumentException("component is null.");
         }
 
-        if (component == this) {
-            throw new IllegalArgumentException("Cannot add a container to itself.");
+        if (component instanceof Container
+            && ((Container)component).isAncestor(this)) {
+            throw new IllegalArgumentException("Component already exists in ancestry.");
         }
 
         if (component.getParent() != null) {
             throw new IllegalArgumentException("Component already has a parent.");
         }
 
-        // TODO Attempting to insert an ancestor will cause a stack overflow.
-        // Do we care to guard against that case?
-
         component.setParent(Container.this);
         components.insert(component, index);
 
@@ -439,14 +437,15 @@
     }
 
     /**
-     * Tests if this container is an ancestor of a given component.
+     * Tests if this container is an ancestor of a given component. A container
+     * is considered to be its own ancestor.
      *
      * @param component
      * The component to test.
      *
      * @return
      * <tt>true</tt> if this container is an ancestor of <tt>component</tt>;
-     * <tt>false</tt>, otherwise.
+     * <tt>false</tt> otherwise.
      */
     public boolean isAncestor(Component component) {
         boolean ancestor = false;