Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes.Method;
import org.apache.wicket.core.request.handler.IPartialPageRequestHandler;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.FormComponentPanel;
import org.apache.wicket.markup.html.form.validation.IFormValidator;
import org.apache.wicket.util.lang.Args;
import org.danekja.java.util.function.serializable.SerializableConsumer;
Expand Down Expand Up @@ -121,6 +121,11 @@ protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
super.updateAjaxAttributes(attributes);

attributes.setMethod(Method.POST);

if (getComponent() instanceof FormComponentPanel<?> formComponentPanel) {
formComponentPanel.wantChildrenToProcessInputInAjaxUpdate();
attributes.setSerializeRecursively(true);
}
}

@Override
Expand All @@ -135,6 +140,12 @@ protected final void onEvent(final AjaxRequestTarget target)

try
{
if (formComponent instanceof FormComponentPanel<?> formComponentPanel) {
if (formComponentPanel.wantChildrenToProcessInputInAjaxUpdate()) {
((FormComponentPanel<?>)formComponent).processInputOfChildren();
}
}

formComponent.inputChanged();
formComponent.validate();
if (formComponent.isValid())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,38 @@ public void clearInput()
}
});
}

/**
* By returning <code>true</code> (and implementing {@link #processInputOfChildren()}) it will be possible to add a
* {@link org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior AjaxFormComponentUpdatingBehavior} to this
* panel, and be able to access the updated model object of this panel in that behavior.
*
* @return <code>true</code> if this panel wants children to process the input on an Ajax update, <code>false</code>
* otherwise.
*/
public boolean wantChildrenToProcessInputInAjaxUpdate() {
return false;
}

/**
* Called by {@link org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior AjaxFormComponentUpdatingBehavior}
* if {@link #wantChildrenToProcessInputInAjaxUpdate()} returns <code>true</code>. Each nested form component must
* be asked to process its input. You should use {@link #processInputOfChild(FormComponent)} as this method takes
* nested <code>FormComponentPanel</code>s that also want their children to process the input into account.
*/
public void processInputOfChildren() {
}

/**
* Tell the given child component to process its input. If the child component is a <code>FormComponentPanel</code>
* that wants its children to process their input, it will be told to do so.
*
* @param child the component to tell to process its children.
*/
protected final void processInputOfChild(FormComponent<?> child) {
if (child instanceof FormComponentPanel<?> formComponentPanel && formComponentPanel.wantChildrenToProcessInputInAjaxUpdate()) {
formComponentPanel.processInputOfChildren();
}
child.processInput();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.wicket.examples.forminput;

import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.form.FormComponentPanel;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
Expand All @@ -29,7 +30,12 @@
* with the lhs and rhs. You would use this component's model (value) primarily to write the result
* to some object, without ever directly setting it in code yourself.
* </p>
*
* <p>
* <strong>Ajaxifying a Multiply</strong>:
* If you want to update this component with an {@link AjaxFormComponentUpdatingBehavior}, you have to override
* {@link #wantChildrenToProcessInputInAjaxUpdate()} and return <code>true</code>.
* </p>
*
* @author eelcohillenius
*/
public class Multiply extends FormComponentPanel<Integer>
Expand Down Expand Up @@ -110,6 +116,13 @@ private void init()
right.setRequired(true);
}

@Override
public void processInputOfChildren()
{
processInputOfChild(left);
processInputOfChild(right);
}

/**
* @see org.apache.wicket.markup.html.form.FormComponent#convertInput()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,11 @@
* Works on a {@link java.time.temporal.Temporal} object, aggregating a {@link LocalDateTextField} and a {@link TimeField}.
* <p>
* <strong>Ajaxifying an AbstractDateTimeField</strong>:
* If you want to update this component with an {@link AjaxFormComponentUpdatingBehavior}, you have to attach it
* to the contained components by overriding {@link #newDateField(String, IModel)}:
*
* <pre>{@code
* DateTimeField dateTimeField = new DateTimeField(...) {
* protected DateTextField newDateTextField(String id, PropertyModel<Date> dateFieldModel)
* {
* DateTextField dateField = super.newDateTextField(id, dateFieldModel);
* dateField.add(new AjaxFormComponentUpdatingBehavior("change") {
* protected void onUpdate(AjaxRequestTarget target) {
* processInput(); // let DateTimeField process input too
* If you want to update this component with an {@link AjaxFormComponentUpdatingBehavior}, you have to override
* {@link #wantChildrenToProcessInputInAjaxUpdate()} and return <code>true</code>, and override
* {@link #newTimeField(String, IModel)} and return a subclass of <code>TimeField</code> that also returns
* <code>true</code> from <code>wantChildrenToProcessInputInAjaxUpdate()</code>.
*
* ...
* }
* });
* return recorder;
* }
* }
* }</pre>
*
* @author eelcohillenius
*/
abstract class AbstractDateTimeField<T extends Temporal> extends FormComponentPanel<T>
Expand Down Expand Up @@ -140,6 +125,13 @@ public String getInput()
return String.format("%s, %s", localDateField.getInput(), timeField.getInput());
}

@Override
public void processInputOfChildren()
{
processInputOfChild(localDateField);
processInputOfChild(timeField);
}

/**
* Sets the converted input, which is an instance of {@link Date}, possibly null. It combines
* the inputs of the nested date, hours, minutes and am/pm fields and constructs a date from it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Arrays;
import java.util.Locale;

import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.core.util.string.CssUtils;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.basic.Label;
Expand All @@ -44,7 +45,11 @@
* AM/PM field. The format (12h/24h) of the hours field depends on the time format of this
* {@link TimeField}'s {@link Locale}, as does the visibility of the AM/PM field (see
* {@link TimeField#use12HourFormat}).
*
* <p>
* <strong>Ajaxifying a TimeField</strong>:
* If you want to update this component with an {@link AjaxFormComponentUpdatingBehavior}, you have to override
* {@link #wantChildrenToProcessInputInAjaxUpdate()} and return <code>true</code>.
*
* @author eelcohillenius
*/
public class TimeField extends FormComponentPanel<LocalTime>
Expand Down Expand Up @@ -246,6 +251,14 @@ public String getInput()
return String.format("%s:%s", hoursField.getInput(), minutesField.getInput());
}

@Override
public void processInputOfChildren()
{
processInputOfChild(hoursField);
processInputOfChild(minutesField);
processInputOfChild(amOrPmChoice);
}

@Override
public void convertInput()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@
* }
* }
* }</pre>
*
*
* <p>
* Alternatively you can override {@link #wantChildrenToProcessInputInAjaxUpdate()} and return <code>true</code>, but
* this will submit more data than needed: it will include the form data of the choices and selection components too.
*
* <p>
* You can add a {@link DefaultTheme} to style this component in a left to right fashion.
*
* @author Igor Vaynberg ( ivaynberg )
Expand Down Expand Up @@ -565,6 +570,12 @@ public int getRows()
return rows;
}

@Override
public void processInputOfChildren()
{
processInputOfChild(recorderComponent);
}

@Override
public void convertInput()
{
Expand Down