Author: ivaynberg
Date: Tue Jun 16 19:01:58 2009
New Revision: 785343

URL: http://svn.apache.org/viewvc?rev=785343&view=rev
Log:
Wicket-2321 create a Component#onRemove() method
Issue: WICKET-2321

Added:
    wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTest.java   (with 
props)
    wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTestPage.html   
(with props)
    wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTestPage.java   
(with props)
Modified:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/MarkupContainer.java

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=785343&r1=785342&r2=785343&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 Jun 
16 19:01:58 2009
@@ -630,6 +630,9 @@
         */
        private static final int FLAG_MODEL_SET = 0x100000;
 
+       /** True when a component is being removed from the hierarchy */
+       protected static final int FLAG_REMOVING_FROM_HIERARCHY = 0x200000;
+
        private static final int FLAG_BEFORE_RENDERING_SUPER_CALL_VERIFIED = 
0x1000000;
 
        /**
@@ -1134,6 +1137,23 @@
        }
 
        /**
+        * Signals this Component that it is removed from the Component 
hierarchy.
+        */
+       final void internalOnRemove()
+       {
+               setFlag(FLAG_REMOVING_FROM_HIERARCHY, true);
+               onRemove();
+               if (getFlag(FLAG_REMOVING_FROM_HIERARCHY))
+               {
+                       throw new 
IllegalStateException(Component.class.getName() +
+                               " has not been properly removed from hierachy. 
Something in the hierarchy of " +
+                               getClass().getName() +
+                               " has not called super.onRemovalFromHierarchy() 
in the override of onRemovalFromHierarchy() method");
+               }
+               removeChildren();
+       }
+
+       /**
         * Detaches the component. This is called at the end of the request for 
all the pages that are
         * touched in that request.
         */
@@ -3962,6 +3982,19 @@
        }
 
        /**
+        * Called to notify the component it is being removed from the 
component hierarchy
+        * 
+        * Overrides of this method MUST call the super implementation, the 
most logical place to do
+        * this is the last line of the override method.
+        * 
+        * 
+        */
+       protected void onRemove()
+       {
+               setFlag(FLAG_REMOVING_FROM_HIERARCHY, false);
+       }
+
+       /**
         * @deprecated use onDetach() instead
         */
        // TODO remove after the deprecation release
@@ -4200,6 +4233,13 @@
        }
 
        /**
+        * Signals this components removal from hierarchy to all its children.
+        */
+       void removeChildren()
+       {
+       }
+
+       /**
         * Gets the component at the given path.
         * 
         * @param path

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=785343&r1=785342&r2=785343&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 Jun 16 19:01:58 2009
@@ -1370,6 +1370,8 @@
 
                component.detach();
 
+               component.internalOnRemove();
+
                // Component is removed
                component.setParent(null);
        }
@@ -1630,6 +1632,26 @@
        }
 
        /**
+        * @see org.apache.wicket.Component#removeChildren()
+        */
+       @Override
+       void removeChildren()
+       {
+               super.removeChildren();
+
+               for (int i = children_size(); i-- > 0;)
+               {
+                       Object child = children_get(i, false);
+                       if (child instanceof Component)
+                       {
+                               Component component = (Component)child;
+                               component.internalOnRemove();
+                       }
+               }
+       }
+
+
+       /**
         * 
         * @see org.apache.wicket.Component#detachChildren()
         */

Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTest.java?rev=785343&view=auto
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTest.java (added)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTest.java Tue Jun 
16 19:01:58 2009
@@ -0,0 +1,80 @@
+/*
+ * 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;
+
+import junit.framework.TestCase;
+
+import org.apache.wicket.util.tester.WicketTester;
+
+/**
+ * Test the {...@link Component#onRemove()}. Test if it gets called and 
propagated to the
+ * Components children.
+ */
+public class RemoveTest extends TestCase
+{
+
+       static final String PATH = RemoveTestPage.COMPONENT + 
Component.PATH_SEPARATOR +
+               RemoveTestPage.LINK;
+
+       WicketTester tester;
+
+       @Override
+       protected void setUp() throws Exception
+       {
+               tester = new WicketTester();
+       }
+
+       @Override
+       protected void tearDown() throws Exception
+       {
+               tester.destroy();
+       }
+
+       /**
+        * The test
+        */
+       public void testOnRemovalFromHierarchy()
+       {
+               Session session = tester.setupRequestAndResponse().getSession();
+               final RemoveTestPage page = new RemoveTestPage();
+               tester.startPage(page);
+               // on initial load of the page no calls should have occurred.
+               assertEquals("OnRemovalFromHierarchy was called.", 0,
+                       page.getComponentOnRemovalFromHierarchyCalls());
+               assertEquals("OnRemovalFromHierarchy was called.", 0,
+                       page.getLinkOnRemovalFromHierarchyCalls());
+
+               tester.clickLink(PATH);
+               // first click provoked a remove, so one call.
+               assertEquals("OnRemovalFromHierarchy wasn't called.", 1,
+                       page.getComponentOnRemovalFromHierarchyCalls());
+               // test if it got propagated to the children.
+               assertEquals("OnRemovalFromHierarchy wasn't called.", 1,
+                       page.getLinkOnRemovalFromHierarchyCalls());
+
+               try
+               {
+                       tester.clickLink(PATH);
+                       fail("Missing Exception");
+               }
+               catch (WicketRuntimeException wre)
+               {
+                       // do nothing.
+                       // This exception was expected.
+               }
+       }
+}

Propchange: wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTestPage.html
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTestPage.html?rev=785343&view=auto
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTestPage.html 
(added)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTestPage.html Tue 
Jun 16 19:01:58 2009
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
+<html xmlns="http://www.w3.org/1999/xhtml";>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+<title>Insert title here</title>
+</head>
+<body>
+<span wicket:id="component"><a wicket:id="link" href="#"></a></span>
+</body>
+</html>
\ No newline at end of file

Propchange: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTestPage.html
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTestPage.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTestPage.java?rev=785343&view=auto
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTestPage.java 
(added)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTestPage.java Tue 
Jun 16 19:01:58 2009
@@ -0,0 +1,115 @@
+/*
+ * 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;
+
+import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.markup.html.link.Link;
+
+/**
+ * Test page used by the {...@link RemoveTest}
+ */
+public class RemoveTestPage extends WebPage
+{
+       private static final long serialVersionUID = 1L;
+
+       static final String COMPONENT = "component";
+       static final String LINK = "link";
+
+       MarkupContainer _1;
+       MarkupContainer _2;
+
+       private int componentOnRemovalFromHierarchyCalls = 0;
+       private int linkOnRemovalFromHierarchyCalls = 0;
+
+       /**
+        * Construct.
+        */
+       @SuppressWarnings("unchecked")
+       public RemoveTestPage()
+       {
+               _1 = new MarkupContainer(COMPONENT)
+               {
+                       private static final long serialVersionUID = 1L;
+
+                       @Override
+                       protected void onRemove()
+                       {
+                               componentOnRemovalFromHierarchyCalls++;
+                               super.onRemove();
+                       }
+               };
+               _1.add(new Link(LINK)
+               {
+                       private static final long serialVersionUID = 1L;
+
+                       @Override
+                       protected void onRemove()
+                       {
+                               linkOnRemovalFromHierarchyCalls++;
+                               super.onRemove();
+                       }
+
+                       @Override
+                       public void onClick()
+                       {
+                               _1.replaceWith(_2);
+                       }
+               });
+
+               _2 = new MarkupContainer(COMPONENT)
+               {
+                       private static final long serialVersionUID = 1L;
+               };
+               _2.add(new Link(LINK)
+               {
+                       private static final long serialVersionUID = 1L;
+
+                       @Override
+                       protected void onRemove()
+                       {
+                               linkOnRemovalFromHierarchyCalls++;
+                               // disable the super call on purpose
+                               // to provoke an exception.
+                               // super.onRemovalFromHierarchy();
+                       }
+
+                       @Override
+                       public void onClick()
+                       {
+                               _2.replaceWith(_1);
+                       }
+               });
+
+               add(_1);
+       }
+
+       /**
+        * @return componentOnRemovalFromHierarchyCalls
+        */
+       public int getComponentOnRemovalFromHierarchyCalls()
+       {
+               return componentOnRemovalFromHierarchyCalls;
+       }
+
+       /**
+        * @return linkOnRemovalFromHierarchyCalls
+        */
+       public int getLinkOnRemovalFromHierarchyCalls()
+       {
+               return linkOnRemovalFromHierarchyCalls;
+       }
+}

Propchange: 
wicket/trunk/wicket/src/test/java/org/apache/wicket/RemoveTestPage.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to