Repository: wicket
Updated Branches:
  refs/heads/WICKET-6348-selection-change 7f08fab8f -> 75ec36271


WICKET-6348 added javadoc; #getUpdateModel(); restored example

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

Branch: refs/heads/WICKET-6348-selection-change
Commit: 75ec362716674227817906d18c50d530f584041e
Parents: 7f08fab
Author: Sven Meier <svenme...@apache.org>
Authored: Wed Mar 29 20:22:15 2017 +0200
Committer: Sven Meier <svenme...@apache.org>
Committed: Wed Mar 29 20:22:15 2017 +0200

----------------------------------------------------------------------
 .../html/form/SelectionChangeBehavior.java      | 130 +++++++++++++++----
 .../wicket/examples/compref/CheckGroupPage.html |   3 +
 .../wicket/examples/compref/CheckGroupPage.java |  14 +-
 3 files changed, 117 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/75ec3627/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SelectionChangeBehavior.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SelectionChangeBehavior.java
 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SelectionChangeBehavior.java
index 8943d12..8298c06 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SelectionChangeBehavior.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SelectionChangeBehavior.java
@@ -18,10 +18,20 @@ package org.apache.wicket.markup.html.form;
 
 import org.apache.wicket.Component;
 import org.apache.wicket.IRequestListener;
+import org.apache.wicket.ajax.form.AjaxFormChoiceComponentUpdatingBehavior;
 import org.apache.wicket.behavior.Behavior;
 import org.apache.wicket.markup.ComponentTag;
 import org.apache.wicket.request.mapper.parameter.PageParameters;
+import org.apache.wicket.util.lang.Args;
 
+/**
+ * A behavior to get change notifications when a choice component changes its 
selection.
+ * <p>
+ * Contrary to {@link AjaxFormChoiceComponentUpdatingBehavior} all 
notification are send via
+ * standard HTTP requests and the full page is rendered as the response.
+ * 
+ * @see SelectionChangeBehavior#onSelectionChanged()
+ */
 public class SelectionChangeBehavior extends Behavior implements 
IRequestListener
 {
 
@@ -32,29 +42,46 @@ public class SelectionChangeBehavior extends Behavior 
implements IRequestListene
        {
                return false;
        }
-       
+
        @Override
-       public void bind(Component component)
+       public final void bind(final Component hostComponent)
        {
-               this.formComponent = (FormComponent<?>)component;
-               
+               Args.notNull(hostComponent, "hostComponent");
+
+               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");
+               }
+
+               this.formComponent = (FormComponent<?>)hostComponent;
+
                formComponent.setRenderBodyOnly(false);
+
+               // call the callback
+               onBind();
+       }
+
+       protected void onBind()
+       {
        }
 
        public FormComponent<?> getFormComponent()
        {
                return formComponent;
        }
-       
+
        @Override
        public void onComponentTag(Component component, ComponentTag tag)
        {
                CharSequence url = component.urlForListener(this, new 
PageParameters());
 
                String event = getJSEvent();
-               
-               String condition = String.format("if (event.target.name !== 
'%s') return; ", formComponent.getInputName());
-               
+
+               String condition = String.format("if (event.target.name !== 
'%s') return; ",
+                       formComponent.getInputName());
+
                Form<?> form = component.findParent(Form.class);
                if (form != null)
                {
@@ -63,39 +90,85 @@ public class SelectionChangeBehavior extends Behavior 
implements IRequestListene
                else
                {
                        char separator = url.toString().indexOf('?') > -1 ? '&' 
: '?';
-                               
-                       tag.put(event,
-                               condition + 
String.format("window.location.href='%s%s%s=' + %s;", url, separator, 
formComponent.getInputName(), getJSValue()));
+
+                       tag.put(event, condition + 
String.format("window.location.href='%s%s%s=' + %s;", url,
+                               separator, formComponent.getInputName(), 
getJSValue()));
                }
        }
 
-       
+       /**
+        * Which JavaScript event triggers notification. 
+        */
        private String getJSEvent()
        {
-               if (formComponent instanceof DropDownChoice) {
+               if (formComponent instanceof DropDownChoice)
+               {
                        return "onchange";
-               } else {
+               }
+               else
+               {
                        return "onclick";
                }
        }
 
+       /**
+        * How to get the current value via JavaScript. 
+        */
        private String getJSValue()
        {
-               if (formComponent instanceof DropDownChoice) {
+               if (formComponent instanceof DropDownChoice)
+               {
                        return "this.options[this.selectedIndex].value";
-               } else if (formComponent instanceof CheckBox) {
+               }
+               else if (formComponent instanceof CheckBox)
+               {
                        return "this.checked";
-               } else {
+               }
+               else
+               {
                        return "event.target.value";
                }
        }
 
-       private void process() {
-               formComponent.convertInput();
-               formComponent.updateModel();
+       /**
+        * Process the form component.
+        */
+       private void process()
+       {
+               formComponent.validate();
+               if (formComponent.isValid())
+               {
+                       if (getUpdateModel())
+                       {
+                               formComponent.valid();
+                               formComponent.updateModel();
+                       }
+
+                       onSelectionChanged();
+               }
+               else
+               {
+                       formComponent.invalid();
+               }
+               
                onSelectionChanged();
        }
-       
+
+       /**
+        * Gives the control to the application to decide whether the form 
component model should
+        * be updated automatically or not. Make sure to call {@link 
org.apache.wicket.markup.html.form.FormComponent#valid()}
+        * additionally in case the application want to update the model 
manually.
+        *
+        * @return true if the model of form component should be updated, false 
otherwise
+        */
+       protected boolean getUpdateModel()
+       {
+               return true;
+       }
+
+       /**
+        * Hook method invoked when selection has changed.
+        */
        protected void onSelectionChanged()
        {
        }
@@ -104,9 +177,12 @@ public class SelectionChangeBehavior extends Behavior 
implements IRequestListene
        public final void onRequest()
        {
                Form<?> form = formComponent.findParent(Form.class);
-               if (form == null) {
+               if (form == null)
+               {
                        process();
-               } else {
+               }
+               else
+               {
                        form.getRootForm().onFormSubmitted(new IFormSubmitter()
                        {
                                @Override
@@ -114,23 +190,23 @@ public class SelectionChangeBehavior extends Behavior 
implements IRequestListene
                                {
                                        process();
                                }
-                               
+
                                @Override
                                public void onError()
                                {
                                }
-                               
+
                                @Override
                                public void onAfterSubmit()
                                {
                                }
-                               
+
                                @Override
                                public Form<?> getForm()
                                {
                                        return formComponent.getForm();
                                }
-                               
+
                                @Override
                                public boolean getDefaultFormProcessing()
                                {

http://git-wip-us.apache.org/repos/asf/wicket/blob/75ec3627/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckGroupPage.html
----------------------------------------------------------------------
diff --git 
a/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckGroupPage.html
 
b/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckGroupPage.html
index 260a2c7..b10bf3f 100644
--- 
a/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckGroupPage.html
+++ 
b/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckGroupPage.html
@@ -14,11 +14,13 @@
        A CheckBoxGroup and CheckBoxComponnet components let users select 
multiple values from a group of checkboxes. These components are more flexible 
then the CheckBoxMultipleChoice component in that individual checkboxes are 
full components, unlike with CheckBoxMultipleChoice, and thus can be used 
anywhere in the markup.
        </p>
        <p>
+        <form wicket:id="form">
                <span wicket:id="group">
                        <table style="border: 2px dotted #fc0; width: 400px; 
padding: 5px;">
                         <tr>
                          <td valign="top">Select persons</td>
                          <td>
+                                 <input type="checkbox" 
wicket:id="groupselector">check/uncheck all</input><br/>
                                  <table cellspacing="0" cellpadding="2">
                                        <tr>
                                                <td><b>Select</b></td>
@@ -42,6 +44,7 @@
                         </tr>
                        </table>
                </span>
+        </form>
         <span wicket:id="feedback">feedbackmessages will be put here</span>
        </p>
     <span wicket:id="explainPanel">panel contents come here</span>

http://git-wip-us.apache.org/repos/asf/wicket/blob/75ec3627/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckGroupPage.java
----------------------------------------------------------------------
diff --git 
a/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckGroupPage.java
 
b/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckGroupPage.java
index 12c99fb..738920e 100644
--- 
a/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckGroupPage.java
+++ 
b/wicket-examples/src/main/java/org/apache/wicket/examples/compref/CheckGroupPage.java
@@ -24,7 +24,6 @@ import org.apache.wicket.markup.html.form.Check;
 import org.apache.wicket.markup.html.form.CheckGroup;
 import org.apache.wicket.markup.html.form.CheckGroupSelector;
 import org.apache.wicket.markup.html.form.Form;
-import org.apache.wicket.markup.html.form.SelectionChangeBehavior;
 import org.apache.wicket.markup.html.list.ListItem;
 import org.apache.wicket.markup.html.list.ListView;
 import org.apache.wicket.markup.html.panel.FeedbackPanel;
@@ -44,9 +43,18 @@ public class CheckGroupPage extends WicketExamplePage
        public CheckGroupPage()
        {
                final CheckGroup<Person> group = new CheckGroup<>("group", new 
ArrayList<Person>());
-               group.add(new SelectionChangeBehavior());
+               Form<Void> form = new Form<Void>("form")
+               {
+                       @Override
+                       protected void onSubmit()
+                       {
+                               info("selected person(s): " + 
group.getDefaultModelObjectAsString());
+                       }
+               };
 
-               add(group);
+               add(form);
+               form.add(group);
+               group.add(new CheckGroupSelector("groupselector"));
                ListView<Person> persons = new ListView<Person>("persons",
                        ComponentReferenceApplication.getPersons())
                {

Reply via email to