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 2014/04/14 18:24:30 UTC

svn commit: r1587249 - /myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/DefaultFaceletsStateManagementStrategy.java

Author: lu4242
Date: Mon Apr 14 16:24:30 2014
New Revision: 1587249

URL: http://svn.apache.org/r1587249
Log:
MYFACES-3883 c:forEach with PSS enabled fails when add multiple rows

Modified:
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/DefaultFaceletsStateManagementStrategy.java

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/DefaultFaceletsStateManagementStrategy.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/DefaultFaceletsStateManagementStrategy.java?rev=1587249&r1=1587248&r2=1587249&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/DefaultFaceletsStateManagementStrategy.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/facelets/DefaultFaceletsStateManagementStrategy.java Mon Apr 14 16:24:30 2014
@@ -565,15 +565,60 @@ public class DefaultFaceletsStateManagem
                         = internalRestoreTreeStructure((TreeStructComponent)
                                                        addedState[3]);
                 child.processRestoreState(context, addedState[4]);
-                try
-                {
-                    target.getChildren().add(childIndex, child);
+                
+                boolean done = false;
+                // Is the child a facelet controlled component?
+                if (child.getAttributes().containsKey(ComponentSupport.MARK_CREATED))
+                {
+                    // By effect of c:forEach it is possible that the component can be duplicated
+                    // in the component tree, so what we need to do as a fallback is replace the
+                    // component in the spot with the restored version.
+                    UIComponent parent = target;
+                    if (parent.getChildCount() > 0)
+                    {
+                        String tagId = (String) child.getAttributes().get(ComponentSupport.MARK_CREATED);
+                        if (childIndex < parent.getChildCount())
+                        {
+                            // Try to find the component quickly 
+                            UIComponent dup = parent.getChildren().get(childIndex);
+                            if (tagId.equals(dup.getAttributes().get(ComponentSupport.MARK_CREATED)))
+                            {
+                                // Replace
+                                parent.getChildren().remove(childIndex.intValue());
+                                parent.getChildren().add(childIndex, child);
+                                done = true;
+                            }
+                        }
+                        if (!done)
+                        {
+                            // Fallback to iteration
+                            for (int i = 0, childCount = parent.getChildCount(); i < childCount; i ++)
+                            {
+                                UIComponent dup = parent.getChildren().get(i);
+                                if (tagId.equals(dup.getAttributes().get(ComponentSupport.MARK_CREATED)))
+                                {
+                                    // Replace
+                                    parent.getChildren().remove(i);
+                                    parent.getChildren().add(i, child);
+                                    done = true;
+                                    break;
+                                }
+                            }
+                        }
+                    }
                 }
-                catch (IndexOutOfBoundsException e)
+                if (!done)
                 {
-                    // We can't be sure about where should be this 
-                    // item, so just add it. 
-                    target.getChildren().add(child);
+                    try
+                    {
+                        target.getChildren().add(childIndex, child);
+                    }
+                    catch (IndexOutOfBoundsException e)
+                    {
+                        // We can't be sure about where should be this 
+                        // item, so just add it. 
+                        target.getChildren().add(child);
+                    }
                 }
             }
         }