From 6606b6917cdb832a3211bd38432c8e728ce8cf55 Mon Sep 17 00:00:00 2001
From: Johan Stuyts
Date: Thu, 30 Jan 2025 11:00:58 +0100
Subject: [PATCH] Enabled Ajaxification of subclasses of form component panels,
and updated Javadoc about how to Ajaxify.
---
.../wicket/examples/forminput/Multiply.java | 15 +++++++++-
.../form/datetime/AbstractDateTimeField.java | 30 +++++++------------
.../markup/html/form/datetime/TimeField.java | 15 +++++++++-
.../markup/html/form/palette/Palette.java | 13 +++++++-
4 files changed, 51 insertions(+), 22 deletions(-)
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/forminput/Multiply.java b/wicket-examples/src/main/java/org/apache/wicket/examples/forminput/Multiply.java
index 337cec16ceb..f3e7d32fb69 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/forminput/Multiply.java
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/forminput/Multiply.java
@@ -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;
@@ -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.
*
- *
+ *
+ * Ajaxifying a Multiply:
+ * If you want to update this component with an {@link AjaxFormComponentUpdatingBehavior}, you have to override
+ * {@link #wantChildrenToProcessInputInAjaxUpdate()} and return true
.
+ *
+ *
* @author eelcohillenius
*/
public class Multiply extends FormComponentPanel
@@ -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()
*/
diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/AbstractDateTimeField.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/AbstractDateTimeField.java
index 4cdd0629276..d43e8cca6e3 100644
--- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/AbstractDateTimeField.java
+++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/AbstractDateTimeField.java
@@ -35,26 +35,11 @@
* Works on a {@link java.time.temporal.Temporal} object, aggregating a {@link LocalDateTextField} and a {@link TimeField}.
*
* Ajaxifying an AbstractDateTimeField:
- * 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)}:
- *
- *
{@code
- * DateTimeField dateTimeField = new DateTimeField(...) {
- * protected DateTextField newDateTextField(String id, PropertyModel 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 true
, and override
+ * {@link #newTimeField(String, IModel)} and return a subclass of TimeField
that also returns
+ * true
from wantChildrenToProcessInputInAjaxUpdate()
.
*
- * ...
- * }
- * });
- * return dateField;
- * }
- * }
- * }
- *
* @author eelcohillenius
*/
abstract class AbstractDateTimeField extends FormComponentPanel
@@ -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.
diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/TimeField.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/TimeField.java
index 382c19276aa..9abbd5f6630 100644
--- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/TimeField.java
+++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/datetime/TimeField.java
@@ -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;
@@ -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}).
- *
+ *
+ * Ajaxifying a TimeField:
+ * If you want to update this component with an {@link AjaxFormComponentUpdatingBehavior}, you have to override
+ * {@link #wantChildrenToProcessInputInAjaxUpdate()} and return true
.
+ *
* @author eelcohillenius
*/
public class TimeField extends FormComponentPanel
@@ -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()
{
diff --git a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/palette/Palette.java b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/palette/Palette.java
index cfe47be160a..e4b46e630f6 100644
--- a/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/palette/Palette.java
+++ b/wicket-extensions/src/main/java/org/apache/wicket/extensions/markup/html/form/palette/Palette.java
@@ -71,7 +71,12 @@
* }
* }
* }
- *
+ *
+ *
+ * Alternatively you can override {@link #wantChildrenToProcessInputInAjaxUpdate()} and return true
, but
+ * this will submit more data than needed: it will include the form data of the choices and selection components too.
+ *
+ *
* You can add a {@link DefaultTheme} to style this component in a left to right fashion.
*
* @author Igor Vaynberg ( ivaynberg )
@@ -565,6 +570,12 @@ public int getRows()
return rows;
}
+ @Override
+ public void processInputOfChildren()
+ {
+ processInputOfChild(recorderComponent);
+ }
+
@Override
public void convertInput()
{