WICKET-6348 small improvements

- check host component type
- getFormComponent() final
- new onError()
- support ListMultipleChoice

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

Branch: refs/heads/master
Commit: c1d389d09ee75f8fcbb58a1ebe9e1d5644563313
Parents: 9bc7ee4
Author: Sven Meier <svenme...@apache.org>
Authored: Thu Mar 30 14:55:00 2017 +0200
Committer: Sven Meier <svenme...@apache.org>
Committed: Tue Apr 4 17:30:04 2017 +0200

----------------------------------------------------------------------
 .../form/FormComponentUpdatingBehavior.java     | 73 +++++++++++++++-----
 1 file changed, 56 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/c1d389d0/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponentUpdatingBehavior.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponentUpdatingBehavior.java
 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponentUpdatingBehavior.java
index e57828b..89a2174 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponentUpdatingBehavior.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponentUpdatingBehavior.java
@@ -18,6 +18,7 @@ package org.apache.wicket.markup.html.form;
 
 import org.apache.wicket.Component;
 import org.apache.wicket.IRequestListener;
+import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
 import org.apache.wicket.behavior.Behavior;
 import org.apache.wicket.markup.ComponentTag;
@@ -44,18 +45,24 @@ public class FormComponentUpdatingBehavior extends Behavior 
implements IRequestL
        }
 
        @Override
-       public final void bind(final Component hostComponent)
+       public final void bind(final Component component)
        {
-               Args.notNull(hostComponent, "hostComponent");
+               Args.notNull(component, "component");
+
+               if (!(component instanceof FormComponent))
+               {
+                       throw new WicketRuntimeException("Behavior " + 
getClass().getName()
+                               + " can only be added to an instance of a 
FormComponent");
+               }
 
                if (formComponent != null)
                {
                        throw new IllegalStateException("this kind of handler 
cannot be attached to " +
                                "multiple components; it is already attached to 
component " + formComponent +
-                               ", but component " + hostComponent + " wants to 
be attached too");
+                               ", but component " + component + " wants to be 
attached too");
                }
 
-               this.formComponent = (FormComponent<?>)hostComponent;
+               this.formComponent = (FormComponent<?>)component;
 
                formComponent.setRenderBodyOnly(false);
 
@@ -63,11 +70,20 @@ public class FormComponentUpdatingBehavior extends Behavior 
implements IRequestL
                onBind();
        }
 
+       /**
+        * Called when the component was bound to it's host component. You can 
get the bound host
+        * component by calling {@link #getFormComponent()}.
+        */
        protected void onBind()
        {
        }
 
-       public FormComponent<?> getFormComponent()
+       /**
+        * Get the hosting component.
+        * 
+        * @return hosting component
+        */
+       public final FormComponent<?> getFormComponent()
        {
                return formComponent;
        }
@@ -101,7 +117,7 @@ public class FormComponentUpdatingBehavior extends Behavior 
implements IRequestL
         */
        private String getJSEvent()
        {
-               if (formComponent instanceof DropDownChoice || formComponent 
instanceof AbstractTextComponent)
+               if (formComponent instanceof DropDownChoice || formComponent 
instanceof ListMultipleChoice|| formComponent instanceof AbstractTextComponent)
                {
                        return "onchange";
                }
@@ -135,23 +151,30 @@ public class FormComponentUpdatingBehavior extends 
Behavior implements IRequestL
         */
        private void process()
        {
-               formComponent.validate();
-               if (formComponent.isValid())
+               try
                {
-                       if (getUpdateModel())
+                       formComponent.validate();
+                       if (formComponent.isValid())
                        {
-                               formComponent.valid();
-                               formComponent.updateModel();
+                               if (getUpdateModel())
+                               {
+                                       formComponent.valid();
+                                       formComponent.updateModel();
+                               }
+       
+                               onUpdate();
+                       }
+                       else
+                       {
+                               formComponent.invalid();
+                               
+                               onError(null);
                        }
-
-                       onUpdate();
                }
-               else
+               catch (RuntimeException e)
                {
-                       formComponent.invalid();
+                       onError(e);
                }
-               
-               onUpdate();
        }
 
        /**
@@ -173,6 +196,22 @@ public class FormComponentUpdatingBehavior extends 
Behavior implements IRequestL
        {
        }
 
+       /**
+        * Hook method invoked when updating of the component resulted in an 
error.
+        * <p>
+        * The {@link RuntimeException} will be null if it was just a 
validation or conversion error of the
+        * FormComponent.
+        * 
+        * @param e optional runtime exception
+        */
+       protected void onError(RuntimeException e)
+       {
+               if (e != null)
+               {
+                       throw e;
+               }
+       }
+       
        @Override
        public final void onRequest()
        {

Reply via email to