diff --git a/pom.xml b/pom.xml index 78dfffa2df..fefd273d31 100644 --- a/pom.xml +++ b/pom.xml @@ -358,7 +358,8 @@ dashboard-parent jamon-parent wicketstuff-rest-lambda - + wicketstuff-lambda-components + @@ -1350,4 +1351,4 @@ - + \ No newline at end of file diff --git a/wicketstuff-lambda-components/pom.xml b/wicketstuff-lambda-components/pom.xml new file mode 100644 index 0000000000..be88d5726e --- /dev/null +++ b/wicketstuff-lambda-components/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + org.wicketstuff + wicketstuff-core + 8.0.0-SNAPSHOT + + wicketstuff-lambda-components + wicketstuff-lambda-components + http://maven.apache.org + + + + org.apache.wicket + wicket-core + + + diff --git a/wicketstuff-lambda-components/src/main/java/org/wicketstuff/lambda/components/ComponentFactory.java b/wicketstuff-lambda-components/src/main/java/org/wicketstuff/lambda/components/ComponentFactory.java new file mode 100644 index 0000000000..40483c17d5 --- /dev/null +++ b/wicketstuff-lambda-components/src/main/java/org/wicketstuff/lambda/components/ComponentFactory.java @@ -0,0 +1,229 @@ +package org.wicketstuff.lambda.components; + +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.ajax.markup.html.AjaxLink; +import org.apache.wicket.ajax.markup.html.form.AjaxButton; +import org.apache.wicket.ajax.markup.html.form.AjaxCheckBox; +import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink; +import org.apache.wicket.markup.html.link.Link; +import org.danekja.java.util.function.serializable.SerializableBiConsumer; +import org.danekja.java.util.function.serializable.SerializableConsumer; + +public class ComponentFactory +{ + + /** + * Creates an {@link AjaxLink} based on lambda expressions + * + * @param id + * the id of the ajax link + * @param onClick + * the consumer of the clicked link and an {@link AjaxRequestTarget} + * @return the {@link AjaxLink} + * + */ + public static AjaxLink ajaxLink(String id, + SerializableBiConsumer, AjaxRequestTarget> onClick) + { + return new AjaxLink(id) + { + + /** + * + */ + private static final long serialVersionUID = -7879252384382557716L; + + @Override + public void onClick(AjaxRequestTarget target) + { + onClick.accept(this, target); + } + }; + } + + /** + * Creates an {@link AjaxButton} based on lambda expressions + * + * @param id + * the id of the ajax button + * @param onSubmit + * the consumer of the submitted button and an {@link AjaxRequestTarget} + * @return the {@link AjaxButton} + * + */ + public static AjaxButton ajaxButton(String id, + SerializableBiConsumer onSubmit) + { + return new AjaxButtonLambda(id, onSubmit, (button, target) -> {}); + } + + /** + * Creates an {@link AjaxButton} based on lambda expressions + * + * @param id + * the id of the ajax button + * @param onSubmit + * the consumer of the submitted button and an {@link AjaxRequestTarget} + * @param onError + * the consumer of the button in error and an {@link AjaxRequestTarget} + * @return the {@link AjaxButton} + * + */ + public static AjaxButton ajaxButton(String id, + SerializableBiConsumer onSubmit, + SerializableBiConsumer onError) + { + return new AjaxButtonLambda(id, onSubmit, onError); + } + + /** + * Creates an {@link AjaxCheckBox} based on lambda expressions + * + * @param id + * the id of ajax check box + * @param onUpdate + * the consumer of the updated checkbox and an {@link AjaxRequestTarget} + * @return the {@link AjaxCheckBox} + * + */ + public static AjaxCheckBox ajaxCheckBox(String id, + SerializableBiConsumer onUpdate) + { + return new AjaxCheckBox(id) + { + + /** + * + */ + private static final long serialVersionUID = 615006993199791039L; + + @Override + protected void onUpdate(AjaxRequestTarget target) + { + onUpdate.accept(this, target); + } + }; + } + + /** + * Creates an {@link AjaxSubmitLink} based on lambda expressions + * + * @param id + * the id of ajax submit link + * @param onSubmit + * the consumer of the submitted button and an {@link AjaxRequestTarget} + * @return the {@link AjaxSubmitLink} + * + */ + public static AjaxSubmitLink ajaxSubmitLink(String id, + SerializableBiConsumer onSubmit) + { + return new AjaxSubmitLinkLambda(id, onSubmit, (link, target) -> {}); + } + + /** + * Creates an {@link AjaxSubmitLink} based on lambda expressions + * + * @param id + * the id of ajax submit link + * @param onSubmit + * the consumer of the submitted link and an {@link AjaxRequestTarget} + * @param onError + * the consumer of the link in error and an {@link AjaxRequestTarget} + * @return the {@link AjaxSubmitLink} + * + */ + public static AjaxSubmitLink ajaxSubmitLink(String id, + SerializableBiConsumer onSubmit, + SerializableBiConsumer onError) + { + return new AjaxSubmitLinkLambda(id, onSubmit, onError); + } + + /** + * Creates a {@link Link} based on lambda expressions + * + * @param id + * the id of the link + * @param onClick + * the consumer of the clicked link + * @return the {@link Link} + * + */ + public static Link link(String id, SerializableConsumer> onClick) + { + return new Link(id) + { + /** + * + */ + private static final long serialVersionUID = 4426455567301117747L; + + @Override + public void onClick() + { + onClick.accept(this); + } + }; + } + + private static final class AjaxButtonLambda extends AjaxButton + { + /** + * + */ + private static final long serialVersionUID = 5357407089802522143L; + private final SerializableBiConsumer onSubmit; + private final SerializableBiConsumer onError; + + private AjaxButtonLambda(String id, SerializableBiConsumer onSubmit, + SerializableBiConsumer onError) + { + super(id); + this.onSubmit = onSubmit; + this.onError = onError; + } + + @Override + protected void onSubmit(AjaxRequestTarget target) + { + onSubmit.accept(this, target); + } + + @Override + protected void onError(AjaxRequestTarget target) + { + onError.accept(this, target); + } + } + + private static final class AjaxSubmitLinkLambda extends AjaxSubmitLink + { + /** + * + */ + private static final long serialVersionUID = -4648835813629191811L; + private final SerializableBiConsumer onSubmit; + private final SerializableBiConsumer onError; + + private AjaxSubmitLinkLambda(String id, SerializableBiConsumer onSubmit, + SerializableBiConsumer onError) + { + super(id); + this.onSubmit = onSubmit; + this.onError = onError; + } + + @Override + protected void onSubmit(AjaxRequestTarget target) + { + onSubmit.accept(this, target); + } + + @Override + protected void onError(AjaxRequestTarget target) + { + onError.accept(this, target); + } + } +}