Skip to content

Commit

Permalink
Enabled Ajaxification of subclasses of form component panels, and upd…
Browse files Browse the repository at this point in the history
…ated Javadoc about how to Ajaxify.
  • Loading branch information
jstuyts committed Jan 30, 2025
1 parent 16bca62 commit 6606b69
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 22 deletions.
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 dateField;
* }
* }
* }</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

0 comments on commit 6606b69

Please sign in to comment.