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 2009/05/07 07:16:35 UTC

svn commit: r772505 - in /myfaces/core/branches/2_0_0/api/src/main/java/javax/faces: component/UIComponent.java component/UIComponentBase.java component/_ComponentChildrenList.java event/PreRemoveFromViewEvent.java

Author: lu4242
Date: Thu May  7 05:16:35 2009
New Revision: 772505

URL: http://svn.apache.org/viewvc?rev=772505&view=rev
Log:
MYFACES-2225 Implement PostAddToViewEvent and PreRemoveFromViewEvent thrown conditions

Added:
    myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/event/PreRemoveFromViewEvent.java   (with props)
Modified:
    myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/component/UIComponent.java
    myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/component/UIComponentBase.java
    myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/component/_ComponentChildrenList.java

Modified: myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/component/UIComponent.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/component/UIComponent.java?rev=772505&r1=772504&r2=772505&view=diff
==============================================================================
--- myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/component/UIComponent.java (original)
+++ myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/component/UIComponent.java Thu May  7 05:16:35 2009
@@ -95,6 +95,8 @@
      */
     private transient Map<String, String> _resourceBundleMap = null;
 
+    private boolean _inView = false;
+
     public UIComponent()
     {
     }
@@ -170,6 +172,14 @@
     }
 
     /**
+     * Indicate if this component is inside a view,
+     * or in other words is contained by an UIViewRoot
+     * instance (which represents the view). If this component
+     * is a UIViewRoot instance, the components "always"
+     * is on the view.
+     * 
+     * By default it is false but for UIViewRoot instances is
+     * true. 
      * 
      * @return
      * 
@@ -177,8 +187,7 @@
      */
     public boolean isInView()
     {
-        // TODO: IMPLEMENT HERE
-        return true;
+        return _inView;
     }
 
     public abstract boolean isRendered();
@@ -368,14 +377,25 @@
     public abstract void setId(String id);
 
     /**
-     * 
+     * Define if the component is on the view or not.
+     * <p>
+     * This value is set in the following conditions:
+     * </p>
+     * <ul>
+     * <li>Component / Facet added: if the parent isInView = true, 
+     *     set it to true and all their children or facets,
+     *     otherwise take no action</li>
+     * <li>Component / Facet removed: if the parent isInView = false,
+     *     set it to false and all their children or facets,
+     *     otherwise take no action</li>
+     * <ul>
      * @param isInView
      * 
      * @since 2.0
      */
     public void setInView(boolean isInView)
     {
-        // TODO: IMPLEMENT HERE
+        _inView = isInView;
     }
 
     /**

Modified: myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/component/UIComponentBase.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/component/UIComponentBase.java?rev=772505&r1=772504&r2=772505&view=diff
==============================================================================
--- myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/component/UIComponentBase.java (original)
+++ myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/component/UIComponentBase.java Thu May  7 05:16:35 2009
@@ -38,6 +38,9 @@
 import javax.faces.event.AbortProcessingException;
 import javax.faces.event.FacesEvent;
 import javax.faces.event.FacesListener;
+import javax.faces.event.PhaseId;
+import javax.faces.event.PostAddToViewEvent;
+import javax.faces.event.PreRemoveFromViewEvent;
 import javax.faces.event.PreRenderComponentEvent;
 import javax.faces.render.RenderKit;
 import javax.faces.render.RenderKitFactory;
@@ -127,7 +130,162 @@
     @Override
     public void setParent(UIComponent parent)
     {
+        boolean postAddToViewEvent = false;
+        boolean preRemoveFromViewEvent = false;
+        if (_parent != null && _parent.isInView())
+        {
+            if (!(parent != null && parent.isInView()))
+            {
+                //Component / Facet removed, set to false
+                //isInView to all parent and children
+                preRemoveFromViewEvent = true;
+                this.setInView(false);
+                if (this.getChildCount() > 0)
+                {
+                    for (Iterator<UIComponent> it = this.getFacetsAndChildren();
+                        it.hasNext();)
+                    {
+                        UIComponent comp = it.next();
+                        if (comp.isInView())
+                        {
+                            //Change to false all descendants
+                            _updateChild(comp,false);
+                        }
+                    }
+                }
+            }
+            //else
+            //{
+                // Component moved inside the same view
+                // There is no reason to call PostAddToViewEvent
+                // because there is already on the view
+            //}
+        }
+        else
+        {
+            if (parent != null && parent.isInView())
+            {
+                //Component / Facet added, set to true
+                //isInView to all parent and children
+                this.setInView(true);
+                postAddToViewEvent = true;
+                if (this.getChildCount() > 0)
+                {
+                    for (Iterator<UIComponent> it = this.getFacetsAndChildren();
+                        it.hasNext();)
+                    {
+                        UIComponent comp = it.next();
+                        if (!comp.isInView())
+                        {
+                            //Change to false all descendants
+                            _updateChild(comp,true);
+                        }
+                    }
+                }
+            }
+            //else
+            //{
+                //Component manipulated but it is not on the
+                //view yet. No event is published.
+            //}
+        }
+        
         _parent = parent;
+        
+        if (postAddToViewEvent)
+        {
+            FacesContext context = FacesContext.getCurrentInstance();
+            
+            // After the child component has been added to the view, if the following condition is not met
+            // FacesContext.isPostback() returns true and FacesContext.getCurrentPhaseId() returns PhaseId.RESTORE_VIEW
+            if (!(context.isPostback() && PhaseId.RESTORE_VIEW.equals(context.getCurrentPhaseId())))
+            {
+                // Application.publishEvent(java.lang.Class, java.lang.Object)  must be called, passing 
+                // PostAddToViewEvent.class as the first argument and the newly added component as the second 
+                // argument.
+                _publishPostAddToViewEvent(context, this);
+            }
+        }
+        
+        if (preRemoveFromViewEvent)
+        {
+            FacesContext context = FacesContext.getCurrentInstance();
+            _publishPreRemoveFromViewEvent(context, this);
+        }
+    }
+
+    /**
+     * Publish PostAddToViewEvent to the component and all facets and children.
+     * 
+     * @param context
+     * @param component
+     */
+    private static void _publishPostAddToViewEvent(FacesContext context, UIComponent component)
+    {
+        context.getApplication().publishEvent(PostAddToViewEvent.class, component);
+        
+        if (component.getChildCount() > 0)
+        {
+            for (UIComponent child : component.getChildren())
+            {
+                _publishPostAddToViewEvent(context, child);
+            }
+        }
+        if (component.getFacetCount() > 0)
+        {
+            for (UIComponent child : component.getFacets().values())
+            {
+                _publishPostAddToViewEvent(context, child);
+            }
+        }        
+    }
+    
+    /**
+     * Publish PostAddToViewEvent to the component and all facets and children.
+     * 
+     * @param context
+     * @param component
+     */
+    private static void _publishPreRemoveFromViewEvent(FacesContext context, UIComponent component)
+    {
+        context.getApplication().publishEvent(PreRemoveFromViewEvent.class, component);
+        
+        if (component.getChildCount() > 0)
+        {
+            for (UIComponent child : component.getChildren())
+            {
+                _publishPreRemoveFromViewEvent(context, child);
+            }
+        }
+        if (component.getFacetCount() > 0)
+        {
+            for (UIComponent child : component.getFacets().values())
+            {
+                _publishPreRemoveFromViewEvent(context, child);
+            }
+        }        
+    }    
+    
+    private static void _updateChild(UIComponent component, boolean isInView)
+    {
+        if (component.getChildCount() > 0)
+        {
+            for (UIComponent child : component.getChildren())
+            {
+                child.setInView(isInView);
+                //recursive call to set to all descendants
+                _updateChild(child, isInView);
+            }
+        }
+        if (component.getFacetCount() > 0)
+        {
+            for (UIComponent child : component.getFacets().values())
+            {
+                child.setInView(isInView);
+                //recursive call to set to all descendants
+                _updateChild(child, isInView);
+            }
+        }         
     }
 
     /**

Modified: myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/component/_ComponentChildrenList.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/component/_ComponentChildrenList.java?rev=772505&r1=772504&r2=772505&view=diff
==============================================================================
--- myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/component/_ComponentChildrenList.java (original)
+++ myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/component/_ComponentChildrenList.java Thu May  7 05:16:35 2009
@@ -88,8 +88,10 @@
     public void add(int index, UIComponent value)
     {
         checkValue(value);
-        childAdded(value);
+        
         _list.add(index, value);
+        
+        childAdded(value);
     }
 
     @Override
@@ -121,6 +123,7 @@
     {
         updateParent(child);
         
+        /*
         FacesContext context = FacesContext.getCurrentInstance();
         
         // After the child component has been added to the view, if the following condition is not met
@@ -132,6 +135,7 @@
             // argument. TODO: Deal with isInView
             context.getApplication().publishEvent(PostAddToViewEvent.class, child);
         }
+        */
     }
 
     private void childRemoved(UIComponent child)
@@ -149,4 +153,10 @@
         
         child.setParent(_component);
     }
+
+    @Override
+    public boolean remove(Object o)
+    {
+        return _list.remove(o);
+    }
 }

Added: myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/event/PreRemoveFromViewEvent.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/event/PreRemoveFromViewEvent.java?rev=772505&view=auto
==============================================================================
--- myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/event/PreRemoveFromViewEvent.java (added)
+++ myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/event/PreRemoveFromViewEvent.java Thu May  7 05:16:35 2009
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package javax.faces.event;
+
+import javax.faces.component.UIComponent;
+
+/**
+ * @author Leonardo Uribe (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ *
+ * @since 2.0
+ */
+public class PreRemoveFromViewEvent extends ComponentSystemEvent
+{
+    /**
+     * @param component
+     */
+    public PreRemoveFromViewEvent(UIComponent component)
+    {
+        super(component);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean isAppropriateListener(FacesListener listener)
+    {
+        return listener instanceof SystemEventListener;
+    }
+}

Propchange: myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/event/PreRemoveFromViewEvent.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/core/branches/2_0_0/api/src/main/java/javax/faces/event/PreRemoveFromViewEvent.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL