All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wicketstuff.lambda.components.ComponentFactory Maven / Gradle / Ivy

The newest version!
/**
 *  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.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);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy