You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2011/06/16 19:26:49 UTC

svn commit: r1136553 - in /myfaces/core/trunk/api/src: main/java/javax/faces/component/_ComponentChildrenList.java main/java/javax/faces/component/_ComponentFacetMap.java test/java/javax/faces/component/UIComponentBaseGetChildrenTest.java

Author: lu4242
Date: Thu Jun 16 17:26:48 2011
New Revision: 1136553

URL: http://svn.apache.org/viewvc?rev=1136553&view=rev
Log:
MYFACES-3175 Both child and facet list should be checked while moving an existing child

Modified:
    myfaces/core/trunk/api/src/main/java/javax/faces/component/_ComponentChildrenList.java
    myfaces/core/trunk/api/src/main/java/javax/faces/component/_ComponentFacetMap.java
    myfaces/core/trunk/api/src/test/java/javax/faces/component/UIComponentBaseGetChildrenTest.java

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/component/_ComponentChildrenList.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/component/_ComponentChildrenList.java?rev=1136553&r1=1136552&r2=1136553&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/component/_ComponentChildrenList.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/component/_ComponentChildrenList.java Thu Jun 16 17:26:48 2011
@@ -21,7 +21,9 @@ package javax.faces.component;
 import java.io.Serializable;
 import java.util.AbstractList;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @author Manfred Geiler (latest modification by $Author$)
@@ -61,7 +63,7 @@ class _ComponentChildrenList extends Abs
             updateParent(value);
             if (child != null)
             {
-                child.setParent(null);
+                childRemoved(child);
             }
         }
         
@@ -112,10 +114,11 @@ class _ComponentChildrenList extends Abs
             throw new NullPointerException("value");
         }
         
-        if (!(value instanceof UIComponent))
-        {
-            throw new ClassCastException("value is not a UIComponent");
-        }
+        // Not necessary anymore because  
+        //if (!(value instanceof UIComponent))
+        //{
+        //    throw new ClassCastException("value is not a UIComponent");
+        //}
     }
 
     private void childRemoved(UIComponent child)
@@ -133,15 +136,37 @@ class _ComponentChildrenList extends Abs
         UIComponent oldParent = child.getParent();
         if (oldParent != null)
         {
-            oldParent.getChildren().remove(child);
+            if (!oldParent.getChildren().remove(child))
+            {
+                // Check if the component is inside a facet and remove from there
+                if (oldParent.getFacetCount() > 0)
+                {
+                    for (Iterator< Map.Entry<String, UIComponent > > it = 
+                        oldParent.getFacets().entrySet().iterator() ; it.hasNext() ; )
+                    {
+                        Map.Entry<String, UIComponent > entry = it.next();
+                        
+                        if (entry.getValue().equals(child))
+                        {
+                            it.remove();
+                            break;
+                        }
+                    }
+                }
+            }
         }
     }
 
     @Override
     public boolean remove(Object value)
     {
-        checkValue(value);
+        if (!(value instanceof UIComponent))
+        {
+            throw new ClassCastException("value is not a UIComponent");
+        }
         
+        checkValue(value);
+
         if (_list.remove(value))
         {
             childRemoved((UIComponent)value);

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/component/_ComponentFacetMap.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/component/_ComponentFacetMap.java?rev=1136553&r1=1136552&r2=1136553&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/component/_ComponentFacetMap.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/component/_ComponentFacetMap.java Thu Jun 16 17:26:48 2011
@@ -103,10 +103,19 @@ class _ComponentFacetMap<V extends UICom
 
     public V put(String key, V value)
     {
-        checkKey(key);
-        checkValue(value);
+        //checkKey(key);
+        if (key == null)
+            throw new NullPointerException("key");
+        //checkValue(value);
+        if (value == null)
+            throw new NullPointerException("value");
         setNewParent(key, value);
-        return _map.put(key, value);
+        V previousValue = _map.put(key, value);
+        if (previousValue != null)
+        {
+            previousValue.setParent(null);
+        }
+        return previousValue; 
     }
 
     private void setNewParent(String facetName, UIComponent facet)
@@ -114,7 +123,24 @@ class _ComponentFacetMap<V extends UICom
         UIComponent oldParent = facet.getParent();
         if (oldParent != null)
         {
-            oldParent.getFacets().remove(facetName);
+            if (!oldParent.getChildren().remove(facet))
+            {
+                // Check if the component is inside a facet and remove from there
+                if (oldParent.getFacetCount() > 0)
+                {
+                    for (Iterator< Map.Entry<String, UIComponent > > it = 
+                        oldParent.getFacets().entrySet().iterator() ; it.hasNext() ; )
+                    {
+                        Map.Entry<String, UIComponent > entry = it.next();
+                        
+                        if (entry.getValue().equals(facet))
+                        {
+                            it.remove();
+                            break;
+                        }
+                    }
+                }
+            }
         }
         facet.setParent(_component);
     }

Modified: myfaces/core/trunk/api/src/test/java/javax/faces/component/UIComponentBaseGetChildrenTest.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/test/java/javax/faces/component/UIComponentBaseGetChildrenTest.java?rev=1136553&r1=1136552&r2=1136553&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/test/java/javax/faces/component/UIComponentBaseGetChildrenTest.java (original)
+++ myfaces/core/trunk/api/src/test/java/javax/faces/component/UIComponentBaseGetChildrenTest.java Thu Jun 16 17:26:48 2011
@@ -86,4 +86,106 @@ public class UIComponentBaseGetChildrenT
         panel.getChildren().remove(input);
         assertNull(input.getParent());
     }
+    
+    /** Whenever a new child component is added, the parent property 
+     * of the child must be set to this component instance. 
+     * If the parent property of the child was already non-null, 
+     * the child must first be removed from its previous parent 
+     * (where it may have been either a child or a facet).
+     */
+    public void testSetChild1()
+    {
+        UIInput input = new UIInput();
+        input.setId("input0");
+
+        UIInput input1 = new UIInput();
+        input.setId("input1");
+        
+        UIPanel panel = new UIPanel();
+        panel.getChildren().add(input);
+        assertEquals(panel, input.getParent());
+        
+        panel.getChildren().set(0, input1);
+        
+        assertEquals(panel, input1.getParent());
+        assertNull(input.getParent());
+    }
+    
+    public void testSetChild2()
+    {
+        UIInput input = new UIInput();
+        input.setId("input0");
+
+        UIInput input1 = new UIInput();
+        input.setId("input1");
+        
+        UIPanel panel = new UIPanel();
+        panel.getChildren().add(input);
+        assertEquals(panel, input.getParent());
+        
+        UIViewRoot root = new UIViewRoot();
+        root.getChildren().add(panel);
+        root.getFacets().put("customFacet", input1);
+        
+        panel.getChildren().set(0, input1);
+        
+        assertEquals(panel, input1.getParent());
+        assertNull(input.getParent());
+        
+        assertTrue(root.getFacets().isEmpty());
+    }
+    
+    
+    
+    /** Whenever an existing child component is removed, the parent 
+     * property of the child must be set to null.
+     */
+    public void testSetFacetClearChild()
+    {
+        UIInput input = new UIInput();
+        input.setId("input0");
+
+        UIInput input1 = new UIInput();
+        input.setId("input1");
+        
+        UIPanel panel = new UIPanel();
+        panel.getChildren().add(input);
+        assertEquals(panel, input.getParent());
+
+        UIViewRoot root = new UIViewRoot();
+        root.getChildren().add(panel);
+        root.getFacets().put("customFacet", input1);
+
+        root.getFacets().put("customFacet", input);
+        assertEquals(root, input.getParent());
+        assertNull(input1.getParent());
+        
+        assertFalse(root.getFacets().isEmpty());
+        assertTrue(panel.getChildCount() == 0);
+    }
+
+    
+    public void testSetFacetClearFacet()
+    {
+        UIInput input = new UIInput();
+        input.setId("input0");
+
+        UIInput input1 = new UIInput();
+        input.setId("input1");
+        
+        UIPanel panel = new UIPanel();
+        panel.getFacets().put("header", input);
+        assertEquals(panel, input.getParent());
+
+        UIViewRoot root = new UIViewRoot();
+        root.getChildren().add(panel);
+        root.getFacets().put("customFacet", input1);
+
+        root.getFacets().put("customFacet", input);
+        assertEquals(root, input.getParent());
+        assertNull(input1.getParent());
+        
+        assertFalse(root.getFacets().isEmpty());
+        assertTrue(panel.getChildCount() == 0);
+    }
 }