You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wicket.apache.org by Pekka Enberg <pe...@ri.fi> on 2006/10/04 14:52:36 UTC

[RFC/PATCH 3/3] Listener method for form processing

From: Pekka Enberg <pe...@cs.helsinki.fi>

Add a listener method processChildren() to IFormListener which can be used to
stop traversal of child components for form processing.  Needed for
implementing components that should not always be validated.

Cc: Igor Vaynberg <ig...@gmail.com>
Cc: Janne Hietamäki <ja...@ri.fi>
Cc: Joni Freeman <jo...@ri.fi>
Signed-off-by: Pekka Enberg <pe...@cs.helsinki.fi>
---

 src/java/wicket/markup/html/form/Form.java          |   25 ++++++--
 src/java/wicket/markup/html/form/FormComponent.java |    7 ++
 src/java/wicket/markup/html/form/IFormListener.java |    7 ++
 src/test/wicket/markup/html/form/FormTest.java      |   58 ++++++++++++++++++++
 4 files changed, 90 insertions(+), 7 deletions(-)

Index: wicket/src/test/wicket/markup/html/form/FormTest.java
===================================================================
--- /dev/null
+++ wicket/src/test/wicket/markup/html/form/FormTest.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed 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 wicket.markup.html.form;
+
+import junit.framework.TestCase;
+import wicket.Component;
+
+/**
+ * @author Pekka Enberg
+ */
+public class FormTest extends TestCase
+{
+	private FormComponent.IVisitor visitor;
+
+	@Override
+	protected void setUp() throws Exception
+	{
+		visitor = new Form.ValidationVisitor()
+		{
+			@Override
+			public void validate(FormComponent formComponent)
+			{
+			}
+		};
+	}
+
+	public void testShouldContinueTraversalIfListenerAllowsChildProcessing()
+	{
+		assertTraversalStatus(Component.IVisitor.CONTINUE_TRAVERSAL, true);
+	}
+
+	public void testShouldContinueTraversalButDontGoDeeperIfListenerDisallowsChildProcessing()
+	{
+		assertTraversalStatus(Component.IVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER, false);
+	}
+
+	private void assertTraversalStatus(Object expected, final boolean processChildren)
+	{
+		assertEquals(expected, visitor.formComponent(new IFormListener()
+		{
+			public boolean processChildren()
+			{
+				return processChildren;
+			}
+		}));
+	}
+}
Index: wicket/src/java/wicket/markup/html/form/FormComponent.java
===================================================================
--- wicket.orig/src/java/wicket/markup/html/form/FormComponent.java
+++ wicket/src/java/wicket/markup/html/form/FormComponent.java
@@ -1300,4 +1300,11 @@ public abstract class FormComponent<T> e
 		}
 	}
 
+	/**
+	 * @see wicket.markup.html.form.IFormListener#processChildren(boolean)
+	 */
+	public boolean processChildren()
+	{
+	    return true;
+	}
 }
Index: wicket/src/java/wicket/markup/html/form/IFormListener.java
===================================================================
--- wicket.orig/src/java/wicket/markup/html/form/IFormListener.java
+++ wicket/src/java/wicket/markup/html/form/IFormListener.java
@@ -20,4 +20,11 @@ package wicket.markup.html.form;
  */
 public interface IFormListener
 {
+	/**
+	 * Should children be traversed during form processing.
+	 *
+	 * @return <tt>true</tt> if children should be traversed during form
+	 *         processing; <tt>false</tt> otherwise.
+	 */
+	boolean processChildren();
 }
Index: wicket/src/java/wicket/markup/html/form/Form.java
===================================================================
--- wicket.orig/src/java/wicket/markup/html/form/Form.java
+++ wicket/src/java/wicket/markup/html/form/Form.java
@@ -144,18 +144,29 @@ public class Form<T> extends WebMarkupCo
 	 * 
 	 * @author Igor Vaynberg (ivaynberg)
 	 */
-	private static abstract class ValidationVisitor extends FormComponent.AbstractVisitor
+	static abstract class ValidationVisitor implements FormComponent.IVisitor
 	{
 		/**
-		 * @see wicket.markup.html.form.FormComponent.AbstractVisitor#onFormComponent(wicket.markup.html.form.FormComponent)
+		 * @see wicket.markup.html.form.FormComponent.IVisitor#formComponent(wicket.markup.html.form.IFormListener)
 		 */
-		@Override
-		public void onFormComponent(FormComponent formComponent)
+		public Object formComponent(IFormListener component)
 		{
-			if (formComponent.isVisibleInHierarchy() && formComponent.isValid()
-					&& formComponent.isEnabled() && formComponent.isEnableAllowed())
+			if (component instanceof FormComponent)
+			{
+				FormComponent formComponent = (FormComponent) component;
+				if (formComponent.isVisibleInHierarchy() && formComponent.isValid()
+						&& formComponent.isEnabled() && formComponent.isEnableAllowed())
+				{
+					validate(formComponent);
+				}
+			}
+			if (component.processChildren())
+			{
+				return Component.IVisitor.CONTINUE_TRAVERSAL;
+			}
+			else
 			{
-				validate(formComponent);
+				return Component.IVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
 			}
 		}