You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by iv...@apache.org on 2010/03/26 07:12:54 UTC
svn commit: r927702 - in /wicket/trunk/wicket/src:
main/java/org/apache/wicket/Component.java
main/java/org/apache/wicket/MarkupContainer.java
main/java/org/apache/wicket/Page.java
test/java/org/apache/wicket/ComponentInitializationTest.java
Author: ivaynberg
Date: Fri Mar 26 06:12:54 2010
New Revision: 927702
URL: http://svn.apache.org/viewvc?rev=927702&view=rev
Log:
implemented Component#onInitialize
Added:
wicket/trunk/wicket/src/test/java/org/apache/wicket/ComponentInitializationTest.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
wicket/trunk/wicket/src/main/java/org/apache/wicket/Page.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=927702&r1=927701&r2=927702&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 Fri Mar 26 06:12:54 2010
@@ -356,9 +356,9 @@ public abstract class Component implemen
/** Reserved subclass-definable flag bit */
protected static final int FLAG_RESERVED5 = 0x10000;
/** Reserved subclass-definable flag bit */
- protected static final int FLAG_RESERVED6 = 0x20000;
+ private static final int FLAG_INITIALIZED = 0x20000;
/** Reserved subclass-definable flag bit */
- protected static final int FLAG_RESERVED7 = 0x40000;
+ private static final int FLAG_NOTUSED7 = 0x40000;
/** Reserved subclass-definable flag bit */
protected static final int FLAG_RESERVED8 = 0x80000;
@@ -860,11 +860,45 @@ public abstract class Component implemen
* Callback method invoked after the component was added to its parent AND you can walk up the
* hierarchy up until the Page. That is, all parents must be have been added to their parents as
* well. Add this point in time {@link #getMarkup() getMarkup} is guaranteed to be available.
- * <p/>
+ * <p>
+ * This method is guaranteed to only be called once
+ * </p>
+ * <p>
* If you don't like constructors to initialize your component, this is the method to use.
+ * </p>
+ */
+ protected void onInitialize()
+ {
+ }
+
+ /**
+ * Checks if the component has been initialized - {@link #onInitialize()} has been called
+ *
+ * @return {@code true} if component has been initialized
+ */
+ boolean isInitialized()
+ {
+ return getFlag(FLAG_INITIALIZED);
+ }
+
+ /**
+ * Used to call {@link #onInitialize()}
+ */
+ void initialize()
+ {
+ if (!getFlag(FLAG_INITIALIZED))
+ {
+ performInitialization();
+ setFlag(FLAG_INITIALIZED, true);
+ }
+ }
+
+ /**
+ * Takes care of calling {@link #onInitialize()}
*/
- protected void onConnectedToPage()
+ void performInitialization()
{
+ onInitialize();
}
/**
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=927702&r1=927701&r2=927702&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 Fri Mar 26 06:12:54 2010
@@ -943,8 +943,6 @@ public abstract class MarkupContainer ex
// Set child's parent
child.setParent(this);
- final Page page = findPage();
-
final IDebugSettings debugSettings = Application.get().getDebugSettings();
if (debugSettings.isLinePreciseReportingOnAddComponentEnabled())
{
@@ -952,23 +950,19 @@ public abstract class MarkupContainer ex
"added")));
}
- if (page != null)
+ if (this instanceof Page)
+ { // a little icky...
+ child.initialize();
+ }
+ else if (isInitialized())
{
- child.onConnectedToPage();
+ child.initialize();
+ }
- // Tell all children of "component" as well
- if (child instanceof MarkupContainer)
- {
- MarkupContainer container = (MarkupContainer)child;
- container.visitChildren(new IVisitor<Component, Void>()
- {
- public void component(final Component component, final IVisit<Void> visit)
- {
- component.onConnectedToPage();
- }
- });
- }
+ final Page page = findPage();
+ if (page != null)
+ {
// Tell the page a component has been added
page.componentAdded(child);
}
@@ -981,6 +975,19 @@ public abstract class MarkupContainer ex
}
}
+ @Override
+ final void performInitialization()
+ {
+ super.performInitialization();
+ visitChildren(new IVisitor<Component, Void>()
+ {
+ public void component(final Component component, final IVisit<Void> visit)
+ {
+ component.initialize();
+ }
+ });
+ }
+
/**
* @param child
* Child to add
@@ -2007,4 +2014,5 @@ public abstract class MarkupContainer ex
}
}
}
+
}
Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/Page.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/Page.java?rev=927702&r1=927701&r2=927702&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/Page.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/Page.java Fri Mar 26 06:12:54 2010
@@ -1267,4 +1267,16 @@ public abstract class Page extends Marku
}
return (Page)session.getPageManager().getPage(id);
}
+
+ /**
+ * This method does nothing, it is here to prevent subclasses from overriding it since this
+ * callback is never called on the {@link Page}
+ *
+ * @see org.apache.wicket.Component#onInitialize()
+ */
+ @Override
+ protected final void onInitialize()
+ {
+ }
+
}
Added: 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=927702&view=auto
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/ComponentInitializationTest.java (added)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/ComponentInitializationTest.java Fri Mar 26 06:12:54 2010
@@ -0,0 +1,105 @@
+/*
+ * 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.WebMarkupContainer;
+import org.apache.wicket.markup.html.WebPage;
+
+public class ComponentInitializationTest extends WicketTestCase
+{
+ public void testPropagation()
+ {
+ Page page = new TestPage();
+
+ TestComponent t1 = new TestComponent("t1");
+ TestComponent t2 = new TestComponent("t2");
+ TestComponent t3 = new TestComponent("t3");
+
+ // as soon as we add to page child should be initialized
+ page.add(t1);
+ assertEquals(1, t1.getCount());
+
+ // unless the page is available no initialization takes place
+ t2.add(t3);
+ assertEquals(0, t2.getCount());
+ assertEquals(0, t3.getCount());
+
+ // initialization cascades from initialized
+ t1.add(t2);
+ assertEquals(1, t1.getCount());
+ assertEquals(1, t2.getCount());
+ assertEquals(1, t3.getCount());
+ }
+
+ public void testAtomicity()
+ {
+ Page page = new TestPage();
+
+ TestComponent t1 = new TestComponent("t1");
+ TestComponent t2 = new TestComponent("t2");
+ TestComponent t3 = new TestComponent("t3");
+
+ t1.add(t2);
+ t2.add(t3);
+
+ page.add(t1);
+
+ assertEquals(1, t1.getCount());
+ assertEquals(1, t2.getCount());
+ assertEquals(1, t3.getCount());
+
+ // test moving
+ page.add(t3);
+ assertEquals(1, t3.getCount());
+
+ // test removal and readdition
+ page.remove(t1);
+ assertEquals(1, t1.getCount());
+ page.add(t1);
+ assertEquals(1, t1.getCount());
+ assertEquals(1, t2.getCount());
+
+ }
+
+ private static class TestPage extends WebPage
+ {
+
+ }
+
+ private static class TestComponent extends WebMarkupContainer
+ {
+ private int count = 0;
+
+ public TestComponent(String id)
+ {
+ super(id);
+ }
+
+ @Override
+ protected void onInitialize()
+ {
+ count++;
+ }
+
+ public int getCount()
+ {
+ return count;
+ }
+
+
+ }
+}
Propchange: wicket/trunk/wicket/src/test/java/org/apache/wicket/ComponentInitializationTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain