Updated Branches:
  refs/heads/master ec1bc9ca3 -> b8082ca3a

WICKET-4575 allow form submitters to choose whether to run before or after 
form#onsubmit


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/b8082ca3
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/b8082ca3
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/b8082ca3

Branch: refs/heads/master
Commit: b8082ca3a0261e8b34f83543fa737fcff067e55f
Parents: ec1bc9c
Author: Carl-Eric Menzel <cmen...@wicketbuch.de>
Authored: Fri Jun 15 16:13:36 2012 +0200
Committer: Carl-Eric Menzel <cmen...@wicketbuch.de>
Committed: Fri Jun 15 16:13:36 2012 +0200

----------------------------------------------------------------------
 .../wicket/ajax/form/AjaxFormSubmitBehavior.java   |   20 +++
 .../markup/html/form/AbstractSubmitLink.java       |   14 ++-
 .../org/apache/wicket/markup/html/form/Button.java |   16 +++-
 .../org/apache/wicket/markup/html/form/Form.java   |   32 ++++-
 .../wicket/markup/html/form/IFormSubmitter.java    |   18 +++
 .../html/form/FormSubmitOrderTest$TestPage.html    |    8 ++
 .../markup/html/form/FormSubmitOrderTest.java      |   92 +++++++++++++++
 7 files changed, 191 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/b8082ca3/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormSubmitBehavior.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormSubmitBehavior.java
 
b/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormSubmitBehavior.java
index 8e926a5..52b067c 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormSubmitBehavior.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormSubmitBehavior.java
@@ -24,6 +24,7 @@ import 
org.apache.wicket.ajax.attributes.AjaxRequestAttributes.Method;
 import org.apache.wicket.markup.html.form.Button;
 import org.apache.wicket.markup.html.form.Form;
 import org.apache.wicket.markup.html.form.IFormSubmitter;
+import org.apache.wicket.markup.html.form.IFormSubmitter.SubmitOrder;
 import org.apache.wicket.markup.html.form.IFormSubmittingComponent;
 
 /**
@@ -171,10 +172,29 @@ public abstract class AjaxFormSubmitBehavior extends 
AjaxEventBehavior
                        {
                                AjaxFormSubmitBehavior.this.onError(target);
                        }
+
+                       @Override
+                       public SubmitOrder getSubmitOrder()
+                       {
+                               return 
AjaxFormSubmitBehavior.this.getSubmitOrder();
+                       }
                });
        }
 
        /**
+        * Defaults to running before {@link Form#onSubmit()}. Override if you 
want to run afterwards
+        * instead.
+        * 
+        * @return submit order
+        * 
+        * @see 
org.apache.wicket.markup.html.form.IFormSubmitter#getSubmitOrder()
+        */
+       public IFormSubmitter.SubmitOrder getSubmitOrder()
+       {
+               return SubmitOrder.BEFORE_FORM;
+       }
+
+       /**
         * Listener method that is invoked after the form has been submitted 
and processed without
         * errors
         * 

http://git-wip-us.apache.org/repos/asf/wicket/blob/b8082ca3/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractSubmitLink.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractSubmitLink.java
 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractSubmitLink.java
index 7be50b9..e2c7665 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractSubmitLink.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/AbstractSubmitLink.java
@@ -28,7 +28,7 @@ import org.apache.wicket.model.IModel;
 public abstract class AbstractSubmitLink extends AbstractLink implements 
IFormSubmittingComponent
 {
        /**
-        * 
+        *
         */
        private static final long serialVersionUID = 1L;
 
@@ -146,4 +146,16 @@ public abstract class AbstractSubmitLink extends 
AbstractLink implements IFormSu
        {
                return Form.getRootFormRelativeId(this);
        }
+
+       /**
+        * Defaults to running before {@link Form#onSubmit()}. Override if you 
want to run afterwards
+        * instead.
+        * 
+        * @see 
org.apache.wicket.markup.html.form.IFormSubmitter#getSubmitOrder()
+        */
+       @Override
+       public SubmitOrder getSubmitOrder()
+       {
+               return SubmitOrder.BEFORE_FORM;
+       }
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/b8082ca3/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Button.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Button.java 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Button.java
index 8a4b555..166f88f 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Button.java
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Button.java
@@ -104,7 +104,7 @@ public class Button extends FormComponent<String> 
implements IFormSubmittingComp
 
        /**
         * Override to not throw exception if there is no parent form.
-        *
+        * 
         * @return the parent form or {@code null}
         */
        @Override
@@ -229,4 +229,16 @@ public class Button extends FormComponent<String> 
implements IFormSubmittingComp
        {
 
        }
-}
\ No newline at end of file
+
+       /**
+        * Defaults to running before {@link Form#onSubmit()}. Override if you 
want to run afterwards
+        * instead.
+        * 
+        * @see 
org.apache.wicket.markup.html.form.IFormSubmitter#getSubmitOrder()
+        */
+       @Override
+       public SubmitOrder getSubmitOrder()
+       {
+               return SubmitOrder.BEFORE_FORM;
+       }
+}

http://git-wip-us.apache.org/repos/asf/wicket/blob/b8082ca3/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
index afc2600..6fe4177 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
@@ -35,6 +35,7 @@ import org.apache.wicket.markup.MarkupStream;
 import org.apache.wicket.markup.head.IHeaderResponse;
 import org.apache.wicket.markup.head.JavaScriptHeaderItem;
 import org.apache.wicket.markup.html.WebMarkupContainer;
+import org.apache.wicket.markup.html.form.IFormSubmitter.SubmitOrder;
 import org.apache.wicket.markup.html.form.upload.FileUploadField;
 import org.apache.wicket.markup.html.form.validation.FormValidatorAdapter;
 import org.apache.wicket.markup.html.form.validation.IFormValidator;
@@ -1223,15 +1224,34 @@ public class Form<T> extends WebMarkupContainer 
implements IFormSubmitListener
        {
                final Form<?> processingForm = 
findFormToProcess(submittingComponent);
 
-
-               // process submitting component (if specified)
-               if (submittingComponent != null)
+               if (submittingComponent == null)
                {
-                       // invoke submit on component
-                       submittingComponent.onSubmit();
+                       // no submitter => just process the forms
+                       submitFormsIncludingNested(processingForm);
+               }
+               else
+               {
+                       // submitter button was clicked => check order
+                       final SubmitOrder submitOrder = 
submittingComponent.getSubmitOrder();
+                       switch (submitOrder)
+                       {
+                               case BEFORE_FORM :
+                                       submittingComponent.onSubmit();
+                                       
submitFormsIncludingNested(processingForm);
+                                       break;
+                               case AFTER_FORM :
+                                       
submitFormsIncludingNested(processingForm);
+                                       submittingComponent.onSubmit();
+                                       break;
+                               default :
+                                       throw new 
IllegalStateException("unknown submitorder: " + submitOrder);
+                       }
                }
+       }
 
-               // invoke Form#onSubmit(..) going from innermost to outermost
+       // invoke Form#onSubmit(..) going from innermost to outermost
+       private void submitFormsIncludingNested(final Form<?> processingForm)
+       {
                Visits.visitPostOrder(processingForm, new IVisitor<Form<?>, 
Void>()
                {
                        @Override

http://git-wip-us.apache.org/repos/asf/wicket/blob/b8082ca3/wicket-core/src/main/java/org/apache/wicket/markup/html/form/IFormSubmitter.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/IFormSubmitter.java
 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/IFormSubmitter.java
index aad0487..68e45a7 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/IFormSubmitter.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/IFormSubmitter.java
@@ -52,4 +52,22 @@ public interface IFormSubmitter
         * errors.
         */
        void onError();
+
+       /**
+        * Returns whether this component's {@link #onSubmit()} method should 
be called before or after
+        * {@link Form#onSubmit()}.
+        * 
+        * @return submit order
+        */
+       SubmitOrder getSubmitOrder();
+
+       /**
+        * Indicates whether this component's {@link #onSubmit()} method should 
be called before or
+        * after {@link Form#onSubmit()}.
+        * 
+        * @author Carl-Eric Menzel (cmenzel)
+        */
+       static enum SubmitOrder {
+               BEFORE_FORM, AFTER_FORM
+       }
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/b8082ca3/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormSubmitOrderTest$TestPage.html
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormSubmitOrderTest$TestPage.html
 
b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormSubmitOrderTest$TestPage.html
new file mode 100644
index 0000000..834e0a3
--- /dev/null
+++ 
b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormSubmitOrderTest$TestPage.html
@@ -0,0 +1,8 @@
+<html>
+<body>
+<form wicket:id="form">
+<input type="submit" wicket:id="custom"/>
+<input type="submit" wicket:id="default"/>
+</form>
+</body>
+</html>

http://git-wip-us.apache.org/repos/asf/wicket/blob/b8082ca3/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormSubmitOrderTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormSubmitOrderTest.java
 
b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormSubmitOrderTest.java
new file mode 100644
index 0000000..649ce4e
--- /dev/null
+++ 
b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormSubmitOrderTest.java
@@ -0,0 +1,92 @@
+/*
+ * 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.markup.html.form;
+
+import org.apache.wicket.WicketTestCase;
+import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.markup.html.form.IFormSubmitter.SubmitOrder;
+import org.junit.Test;
+
+public class FormSubmitOrderTest extends WicketTestCase
+{
+       public static class TestPage extends WebPage
+       {
+               String result = "";
+
+               public TestPage(final SubmitOrder order)
+               {
+                       Form form = new Form("form")
+                       {
+                               @Override
+                               protected void onSubmit()
+                               {
+                                       super.onSubmit();
+                                       result += "form";
+                               }
+                       };
+                       this.add(form);
+                       form.add(new Button("custom")
+                       {
+                               @Override
+                               public SubmitOrder getSubmitOrder()
+                               {
+                                       return order;
+                               }
+
+                               @Override
+                               public void onSubmit()
+                               {
+                                       super.onSubmit();
+                                       result += "custom";
+                               }
+                       });
+                       form.add(new Button("default")
+                       {
+                               @Override
+                               public void onSubmit()
+                               {
+                                       super.onSubmit();
+                                       result += "default";
+                               }
+                       });
+               }
+       }
+
+       @Test
+       public void defaultOrder() throws Exception
+       {
+               TestPage page = tester.startPage(new TestPage(null));
+               tester.newFormTester("form").submit("default");
+               assertEquals("defaultform", page.result);
+       }
+
+       @Test
+       public void customOrderBefore() throws Exception
+       {
+               TestPage page = tester.startPage(new 
TestPage(SubmitOrder.BEFORE_FORM));
+               tester.newFormTester("form").submit("custom");
+               assertEquals("customform", page.result);
+       }
+
+       @Test
+       public void customOrderAfter() throws Exception
+       {
+               TestPage page = tester.startPage(new 
TestPage(SubmitOrder.AFTER_FORM));
+               tester.newFormTester("form").submit("custom");
+               assertEquals("formcustom", page.result);
+       }
+}

Reply via email to