Author: ivaynberg
Date: Tue Sep 28 18:30:57 2010
New Revision: 1002311

URL: http://svn.apache.org/viewvc?rev=1002311&view=rev
Log:

Issue: WICKET-3082

Added:
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/application/IComponentInitializationListener.java
   (with props)
Modified:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/MarkupContainer.java
    
wicket/trunk/wicket/src/test/java/org/apache/wicket/ComponentInitializationTest.java

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java?rev=1002311&r1=1002310&r2=1002311&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java 
(original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java Tue 
Sep 28 18:30:57 2010
@@ -28,7 +28,9 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
+import java.util.concurrent.CopyOnWriteArrayList;
 
+import org.apache.wicket.application.IComponentInitializationListener;
 import org.apache.wicket.application.IComponentInstantiationListener;
 import org.apache.wicket.application.IComponentOnAfterRenderListener;
 import org.apache.wicket.application.IComponentOnBeforeRenderListener;
@@ -173,6 +175,9 @@ public abstract class Application implem
        /** list of {...@link IComponentInstantiationListener}s. */
        private IComponentInstantiationListener[] 
componentInstantiationListeners = new IComponentInstantiationListener[0];
 
+       /** list of {...@link IComponentInitializationListener}s. */
+       private CopyOnWriteArrayList<IComponentInitializationListener> 
componentInitializationListeners = new 
CopyOnWriteArrayList<IComponentInitializationListener>();
+
        /** The converter locator instance. */
        private IConverterLocator converterLocator;
 
@@ -323,6 +328,49 @@ public abstract class Application implem
        }
 
        /**
+        * Adds a component initialization listener. This method should 
typically only be called during
+        * application startup; it is not thread safe.
+        * <p>
+        * Each added listener will be notified after Component's {...@link 
Component#onInitialize()}
+        * method has been executed.
+        * </p>
+        * <p>
+        * Note: wicket does not guarantee the execution order of added 
listeners
+        * 
+        * @param listener
+        *            the listener to add
+        */
+       public final void addComponentInitializationListener(
+               final IComponentInitializationListener listener)
+       {
+               if (listener == null)
+               {
+                       throw new IllegalArgumentException("argument listener 
may not be null");
+               }
+
+               if (componentInitializationListeners.contains(listener))
+               {
+                       return;
+               }
+               componentInitializationListeners.add(listener);
+       }
+
+       /**
+        * Fires registered {...@link IComponentInitializationListener}s on the 
component
+        * 
+        * @param component
+        * 
+        * @see 
#addComponentInitializationListener(IComponentInitializationListener)
+        */
+       public final void fireComponentInitializationListeners(Component 
component)
+       {
+               for (IComponentInitializationListener listener : 
componentInitializationListeners)
+               {
+                       listener.onInitialize(component);
+               }
+       }
+
+       /**
         * Configures application settings to good defaults.
         */
        public final void configure()

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java?rev=1002311&r1=1002310&r2=1002311&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java 
(original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java Tue Sep 
28 18:30:57 2010
@@ -997,6 +997,8 @@ public abstract class Component
                                        " has not called super.onInitialize() 
in the override of onInitialize() method");
                        }
                        setFlag(FLAG_INITIALIZE_SUPER_CALL_VERIFIED, false);
+
+                       
getApplication().fireComponentInitializationListeners(this);
                }
        }
 

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/MarkupContainer.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/MarkupContainer.java?rev=1002311&r1=1002310&r2=1002311&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/MarkupContainer.java 
(original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/MarkupContainer.java 
Tue Sep 28 18:30:57 2010
@@ -972,10 +972,11 @@ public abstract class MarkupContainer ex
                final Page page = findPage();
                if (page != null)
                {
-                       child.initialize();
-
-                       // Tell the page a component has been added
+                       // tell the page a component has been added first, to 
allow it to initialize
                        page.componentAdded(child);
+
+                       // initialie the component
+                       child.initialize();
                }
 
                // if the PREPARED_FOR_RENDER flag is set, we have already 
called

Added: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/application/IComponentInitializationListener.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/application/IComponentInitializationListener.java?rev=1002311&view=auto
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/application/IComponentInitializationListener.java
 (added)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/application/IComponentInitializationListener.java
 Tue Sep 28 18:30:57 2010
@@ -0,0 +1,38 @@
+/*
+ * 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 org.apache.wicket.application;
+
+import org.apache.wicket.Component;
+
+/**
+ * Listener interface that receives messages when components are initialized 
(after
+ * Component#onInitialize method has been executed).
+ * 
+ * <strong>Implementations must be thread safe</strong>
+ * 
+ * @author Igor Vaynberg
+ */
+public interface IComponentInitializationListener
+{
+       /**
+        * Called for every component after its Component#onInitialize method 
has been executed.
+        * 
+        * @param component
+        *            the component that is being instantiated.
+        */
+       void onInitialize(Component component);
+}

Propchange: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/application/IComponentInitializationListener.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/ComponentInitializationTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/ComponentInitializationTest.java?rev=1002311&r1=1002310&r2=1002311&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/test/java/org/apache/wicket/ComponentInitializationTest.java
 (original)
+++ 
wicket/trunk/wicket/src/test/java/org/apache/wicket/ComponentInitializationTest.java
 Tue Sep 28 18:30:57 2010
@@ -16,6 +16,10 @@
  */
 package org.apache.wicket;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.wicket.application.IComponentInitializationListener;
 import org.apache.wicket.markup.IMarkupResourceStreamProvider;
 import org.apache.wicket.markup.html.WebComponent;
 import org.apache.wicket.markup.html.WebMarkupContainer;
@@ -126,6 +130,56 @@ public class ComponentInitializationTest
                assertTrue(illegalState);
        }
 
+       public void testInitListeners()
+       {
+               TestInitListener listener1 = new TestInitListener();
+               TestInitListener listener2 = new TestInitListener();
+               
tester.getApplication().addComponentInitializationListener(listener1);
+               
tester.getApplication().addComponentInitializationListener(listener2);
+
+               WebPage page = new WebPage()
+               {
+               };
+               TestComponent t1 = new TestComponent("t1");
+               TestComponent t2 = new TestComponent("t2");
+
+               t1.add(t2);
+               page.add(t1);
+
+               assertTrue(listener1.getComponents().contains(page));
+               assertTrue(listener1.getComponents().contains(t1));
+               assertTrue(listener1.getComponents().contains(t2));
+               assertTrue(listener2.getComponents().contains(page));
+               assertTrue(listener2.getComponents().contains(t1));
+               assertTrue(listener2.getComponents().contains(t2));
+       }
+
+       public void testInitializationOrder()
+       {
+               TestInitListener listener1 = new TestInitListener();
+               
tester.getApplication().addComponentInitializationListener(listener1);
+
+               WebPage page = new WebPage()
+               {
+               };
+               TestComponent t1 = new TestComponent("t1");
+               TestComponent t2 = new TestComponent("t2");
+               TestComponent t3 = new TestComponent("t3");
+               TestComponent t4 = new TestComponent("t4");
+
+               t1.add(t2);
+               page.add(t1);
+               t1.add(t3);
+               t3.add(t4);
+
+               assertTrue(page == listener1.getComponents().get(0));
+               assertTrue(t1 == listener1.getComponents().get(1));
+               assertTrue(t2 == listener1.getComponents().get(2));
+               assertTrue(t3 == listener1.getComponents().get(3));
+               assertTrue(t4 == listener1.getComponents().get(4));
+       }
+
+
        public static class TestPage extends WebPage implements 
IMarkupResourceStreamProvider
        {
                private int count = 0;
@@ -197,4 +251,22 @@ public class ComponentInitializationTest
                        // missing super call
                }
        }
+
+       private static class TestInitListener implements 
IComponentInitializationListener
+       {
+               private List<Component> components = new ArrayList<Component>();
+
+               public void onInitialize(Component component)
+               {
+                       System.out.println(component);
+                       components.add(component);
+               }
+
+               public List<Component> getComponents()
+               {
+                       return components;
+               }
+
+
+       }
 }


Reply via email to