WICKET-6348 renamed to FormComponentUpdatingBehavior keep deprecated getJsForInterfaceUrl()
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/9bc7ee47 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/9bc7ee47 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/9bc7ee47 Branch: refs/heads/master Commit: 9bc7ee4742715d83cf4d384151cbbb4bd7e2e2d2 Parents: 8f8ad37 Author: Sven Meier <svenme...@apache.org> Authored: Thu Mar 30 09:44:21 2017 +0200 Committer: Sven Meier <svenme...@apache.org> Committed: Tue Apr 4 17:30:04 2017 +0200 ---------------------------------------------------------------------- .../apache/wicket/markup/html/form/Form.java | 7 + .../form/FormComponentUpdatingBehavior.java | 218 +++++++++++++++++++ .../html/form/SelectionChangeBehavior.java | 218 ------------------- .../apache/wicket/util/tester/FormTester.java | 4 +- .../org/apache/wicket/MockPageWithForm.java | 6 +- .../wicket/util/tester/FormTesterTest.java | 6 +- .../extensions/yui/calendar/DatesPage1.java | 4 +- .../apache/wicket/examples/dates/DatesPage.java | 4 +- .../wicket/examples/forminput/FormInput.java | 6 +- .../wicket/examples/tree/AdvancedTreePage.java | 6 +- 10 files changed, 243 insertions(+), 236 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/9bc7ee47/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 6edd242..13aadf8 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 @@ -509,6 +509,13 @@ public class Form<T> extends WebMarkupContainer } /** + * @deprecated use {@link #getJsForListenerUrl(CharSequence)} instead. + */ + public final CharSequence getJsForInterfaceUrl(CharSequence url) { + return getJsForListenerUrl(url); + } + + /** * Generate a piece of JavaScript that submits the form to the given URL. * * Warning: This code should only be called in the rendering phase for form components inside http://git-wip-us.apache.org/repos/asf/wicket/blob/9bc7ee47/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 new file mode 100644 index 0000000..e57828b --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/FormComponentUpdatingBehavior.java @@ -0,0 +1,218 @@ +/* + * 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.Component; +import org.apache.wicket.IRequestListener; +import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior; +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 notifications when a {@link FormComponent} changes its value. + * <p> + * Contrary to {@link AjaxFormComponentUpdatingBehavior} all notification are send via + * standard HTTP requests and the full page is rendered as a response. + * + * @see FormComponentUpdatingBehavior#onUpdate() + */ +public class FormComponentUpdatingBehavior extends Behavior implements IRequestListener +{ + + private FormComponent<?> formComponent; + + @Override + public boolean getStatelessHint(Component component) + { + return false; + } + + @Override + public final void bind(final Component hostComponent) + { + 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()); + + Form<?> form = component.findParent(Form.class); + if (form != null) + { + tag.put(event, condition + form.getJsForListenerUrl(url.toString())); + } + 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())); + } + } + + /** + * Which JavaScript event triggers notification. + */ + private String getJSEvent() + { + if (formComponent instanceof DropDownChoice || formComponent instanceof AbstractTextComponent) + { + return "onchange"; + } + else + { + return "onclick"; + } + } + + /** + * How to get the current value via JavaScript. + */ + private String getJSValue() + { + if (formComponent instanceof DropDownChoice) + { + return "this.options[this.selectedIndex].value"; + } + else if (formComponent instanceof CheckBox) + { + return "this.checked"; + } + else + { + return "event.target.value"; + } + } + + /** + * Process the form component. + */ + private void process() + { + formComponent.validate(); + if (formComponent.isValid()) + { + if (getUpdateModel()) + { + formComponent.valid(); + formComponent.updateModel(); + } + + onUpdate(); + } + else + { + formComponent.invalid(); + } + + onUpdate(); + } + + /** + * 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 the component is updated. + */ + protected void onUpdate() + { + } + + @Override + public final void onRequest() + { + Form<?> form = formComponent.findParent(Form.class); + if (form == null) + { + process(); + } + else + { + form.getRootForm().onFormSubmitted(new IFormSubmitter() + { + @Override + public void onSubmit() + { + process(); + } + + @Override + public void onError() + { + } + + @Override + public void onAfterSubmit() + { + } + + @Override + public Form<?> getForm() + { + return formComponent.getForm(); + } + + @Override + public boolean getDefaultFormProcessing() + { + return false; + } + }); + } + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/9bc7ee47/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 deleted file mode 100644 index 8298c06..0000000 --- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SelectionChangeBehavior.java +++ /dev/null @@ -1,218 +0,0 @@ -/* - * 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.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 -{ - - private FormComponent<?> formComponent; - - @Override - public boolean getStatelessHint(Component component) - { - return false; - } - - @Override - public final void bind(final Component hostComponent) - { - 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()); - - Form<?> form = component.findParent(Form.class); - if (form != null) - { - tag.put(event, condition + form.getJsForListenerUrl(url.toString())); - } - 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())); - } - } - - /** - * Which JavaScript event triggers notification. - */ - private String getJSEvent() - { - if (formComponent instanceof DropDownChoice) - { - return "onchange"; - } - else - { - return "onclick"; - } - } - - /** - * How to get the current value via JavaScript. - */ - private String getJSValue() - { - if (formComponent instanceof DropDownChoice) - { - return "this.options[this.selectedIndex].value"; - } - else if (formComponent instanceof CheckBox) - { - return "this.checked"; - } - else - { - return "event.target.value"; - } - } - - /** - * 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() - { - } - - @Override - public final void onRequest() - { - Form<?> form = formComponent.findParent(Form.class); - if (form == null) - { - process(); - } - else - { - form.getRootForm().onFormSubmitted(new IFormSubmitter() - { - @Override - public void onSubmit() - { - process(); - } - - @Override - public void onError() - { - } - - @Override - public void onAfterSubmit() - { - } - - @Override - public Form<?> getForm() - { - return formComponent.getForm(); - } - - @Override - public boolean getDefaultFormProcessing() - { - return false; - } - }); - } - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/9bc7ee47/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java b/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java index 31448d9..93b735c 100644 --- a/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java +++ b/wicket-core/src/main/java/org/apache/wicket/util/tester/FormTester.java @@ -38,7 +38,7 @@ import org.apache.wicket.markup.html.form.IFormSubmittingComponent; import org.apache.wicket.markup.html.form.ListMultipleChoice; import org.apache.wicket.markup.html.form.Radio; import org.apache.wicket.markup.html.form.RadioGroup; -import org.apache.wicket.markup.html.form.SelectionChangeBehavior; +import org.apache.wicket.markup.html.form.FormComponentUpdatingBehavior; import org.apache.wicket.markup.html.form.upload.FileUploadField; import org.apache.wicket.markup.html.form.upload.MultiFileUploadField; import org.apache.wicket.protocol.http.mock.MockHttpServletRequest; @@ -506,7 +506,7 @@ public class FormTester ChoiceSelector choiceSelector = choiceSelectorFactory.create(component); choiceSelector.doSelect(index); - List<SelectionChangeBehavior> behaviors = component.getBehaviors(SelectionChangeBehavior.class); + List<FormComponentUpdatingBehavior> behaviors = component.getBehaviors(FormComponentUpdatingBehavior.class); if (behaviors.isEmpty() == false) { tester.invokeListener(component, behaviors.get(0)); } http://git-wip-us.apache.org/repos/asf/wicket/blob/9bc7ee47/wicket-core/src/test/java/org/apache/wicket/MockPageWithForm.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/MockPageWithForm.java b/wicket-core/src/test/java/org/apache/wicket/MockPageWithForm.java index 4b8e706..76c2e58 100644 --- a/wicket-core/src/test/java/org/apache/wicket/MockPageWithForm.java +++ b/wicket-core/src/test/java/org/apache/wicket/MockPageWithForm.java @@ -22,7 +22,7 @@ import java.util.List; import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.markup.html.form.DropDownChoice; import org.apache.wicket.markup.html.form.Form; -import org.apache.wicket.markup.html.form.SelectionChangeBehavior; +import org.apache.wicket.markup.html.form.FormComponentUpdatingBehavior; import org.apache.wicket.model.Model; @@ -62,9 +62,9 @@ public class MockPageWithForm extends WebPage list.add("Select me"); MyForm form = new MyForm("form"); DropDownChoice<String> dropDown = new DropDownChoice<String>("dropdown", new Model<String>(), list); - dropDown.add(new SelectionChangeBehavior() { + dropDown.add(new FormComponentUpdatingBehavior() { @Override - protected void onSelectionChanged() + protected void onUpdate() { selected = true; } http://git-wip-us.apache.org/repos/asf/wicket/blob/9bc7ee47/wicket-core/src/test/java/org/apache/wicket/util/tester/FormTesterTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/util/tester/FormTesterTest.java b/wicket-core/src/test/java/org/apache/wicket/util/tester/FormTesterTest.java index 945e46e..fe152ef 100644 --- a/wicket-core/src/test/java/org/apache/wicket/util/tester/FormTesterTest.java +++ b/wicket-core/src/test/java/org/apache/wicket/util/tester/FormTesterTest.java @@ -28,7 +28,7 @@ import org.apache.wicket.markup.IMarkupResourceStreamProvider; import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.markup.html.form.DropDownChoice; import org.apache.wicket.markup.html.form.Form; -import org.apache.wicket.markup.html.form.SelectionChangeBehavior; +import org.apache.wicket.markup.html.form.FormComponentUpdatingBehavior; import org.apache.wicket.markup.html.form.upload.FileUpload; import org.apache.wicket.model.Model; import org.apache.wicket.request.mapper.parameter.PageParameters; @@ -265,9 +265,9 @@ public class FormTesterTest extends WicketTestCase Form<Object> form = new Form<>("form"); add(form); List<String> choices = Arrays.asList("opt 1", "opt 2"); - form.add(new DropDownChoice<String>("selector", Model.of(""), choices).add(new SelectionChangeBehavior() { + form.add(new DropDownChoice<String>("selector", Model.of(""), choices).add(new FormComponentUpdatingBehavior() { @Override - protected void onSelectionChanged() + protected void onUpdate() { selection = (String)getFormComponent().getDefaultModelObject(); } http://git-wip-us.apache.org/repos/asf/wicket/blob/9bc7ee47/wicket-datetime/src/test/java/org/apache/wicket/extensions/yui/calendar/DatesPage1.java ---------------------------------------------------------------------- diff --git a/wicket-datetime/src/test/java/org/apache/wicket/extensions/yui/calendar/DatesPage1.java b/wicket-datetime/src/test/java/org/apache/wicket/extensions/yui/calendar/DatesPage1.java index 43f4067..a632e97 100644 --- a/wicket-datetime/src/test/java/org/apache/wicket/extensions/yui/calendar/DatesPage1.java +++ b/wicket-datetime/src/test/java/org/apache/wicket/extensions/yui/calendar/DatesPage1.java @@ -31,7 +31,7 @@ import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.markup.html.form.ChoiceRenderer; import org.apache.wicket.markup.html.form.DropDownChoice; import org.apache.wicket.markup.html.form.Form; -import org.apache.wicket.markup.html.form.SelectionChangeBehavior; +import org.apache.wicket.markup.html.form.FormComponentUpdatingBehavior; import org.apache.wicket.markup.html.link.Link; import org.apache.wicket.markup.html.panel.FeedbackPanel; import org.apache.wicket.model.IModel; @@ -111,7 +111,7 @@ public class DatesPage1 extends WebPage setChoiceRenderer(new LocaleChoiceRenderer()); setDefaultModel(new PropertyModel<>(DatesPage1.this, "selectedLocale")); - add(new SelectionChangeBehavior()); + add(new FormComponentUpdatingBehavior()); } @Override http://git-wip-us.apache.org/repos/asf/wicket/blob/9bc7ee47/wicket-examples/src/main/java/org/apache/wicket/examples/dates/DatesPage.java ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/dates/DatesPage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/dates/DatesPage.java index eaaaed2..dadad43 100644 --- a/wicket-examples/src/main/java/org/apache/wicket/examples/dates/DatesPage.java +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/dates/DatesPage.java @@ -34,7 +34,7 @@ import org.apache.wicket.extensions.yui.calendar.TimeField; import org.apache.wicket.markup.html.form.ChoiceRenderer; import org.apache.wicket.markup.html.form.DropDownChoice; import org.apache.wicket.markup.html.form.Form; -import org.apache.wicket.markup.html.form.SelectionChangeBehavior; +import org.apache.wicket.markup.html.form.FormComponentUpdatingBehavior; import org.apache.wicket.markup.html.link.Link; import org.apache.wicket.markup.html.panel.FeedbackPanel; import org.apache.wicket.model.IModel; @@ -102,7 +102,7 @@ public class DatesPage extends WicketExamplePage setChoiceRenderer(new LocaleChoiceRenderer()); setDefaultModel(new PropertyModel<>(DatesPage.this, "selectedLocale")); - add(new SelectionChangeBehavior()); + add(new FormComponentUpdatingBehavior()); } } http://git-wip-us.apache.org/repos/asf/wicket/blob/9bc7ee47/wicket-examples/src/main/java/org/apache/wicket/examples/forminput/FormInput.java ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/forminput/FormInput.java b/wicket-examples/src/main/java/org/apache/wicket/examples/forminput/FormInput.java index ecc3414..0ff4681 100644 --- a/wicket-examples/src/main/java/org/apache/wicket/examples/forminput/FormInput.java +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/forminput/FormInput.java @@ -35,7 +35,7 @@ import org.apache.wicket.markup.html.form.ListMultipleChoice; import org.apache.wicket.markup.html.form.Radio; import org.apache.wicket.markup.html.form.RadioChoice; import org.apache.wicket.markup.html.form.RadioGroup; -import org.apache.wicket.markup.html.form.SelectionChangeBehavior; +import org.apache.wicket.markup.html.form.FormComponentUpdatingBehavior; import org.apache.wicket.markup.html.form.SimpleFormComponentLabel; import org.apache.wicket.markup.html.form.TextField; import org.apache.wicket.markup.html.link.Link; @@ -250,9 +250,9 @@ public class FormInput extends WicketExamplePage // updating the current locale to property 'locale' of FormInput setModel(new PropertyModel<>(FormInput.this, "locale")); - add(new SelectionChangeBehavior() { + add(new FormComponentUpdatingBehavior() { @Override - protected void onSelectionChanged() + protected void onUpdate() { // note that we don't have to do anything here, as our property // model already calls FormInput.setLocale when the model is http://git-wip-us.apache.org/repos/asf/wicket/blob/9bc7ee47/wicket-examples/src/main/java/org/apache/wicket/examples/tree/AdvancedTreePage.java ---------------------------------------------------------------------- diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/tree/AdvancedTreePage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/tree/AdvancedTreePage.java index 4522885..d5c4abc 100644 --- a/wicket-examples/src/main/java/org/apache/wicket/examples/tree/AdvancedTreePage.java +++ b/wicket-examples/src/main/java/org/apache/wicket/examples/tree/AdvancedTreePage.java @@ -43,7 +43,7 @@ import org.apache.wicket.markup.html.form.Button; import org.apache.wicket.markup.html.form.ChoiceRenderer; import org.apache.wicket.markup.html.form.DropDownChoice; import org.apache.wicket.markup.html.form.Form; -import org.apache.wicket.markup.html.form.SelectionChangeBehavior; +import org.apache.wicket.markup.html.form.FormComponentUpdatingBehavior; import org.apache.wicket.markup.html.link.Link; import org.apache.wicket.model.IModel; import org.apache.wicket.model.PropertyModel; @@ -98,10 +98,10 @@ public abstract class AdvancedTreePage extends AbstractTreePage form.add(tree); form.add(new DropDownChoice<Content>("content", new PropertyModel<>(this, "content"), - initContents(), new ChoiceRenderer<>("class.simpleName")).add(new SelectionChangeBehavior())); + initContents(), new ChoiceRenderer<>("class.simpleName")).add(new FormComponentUpdatingBehavior())); form.add(new DropDownChoice<Behavior>("theme", new PropertyModel<>(this, "theme"), - initThemes(), new ChoiceRenderer<>("class.simpleName")).add(new SelectionChangeBehavior())); + initThemes(), new ChoiceRenderer<>("class.simpleName")).add(new FormComponentUpdatingBehavior())); form.add(new Link<Void>("expandAll") {