001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.extensions.ajax.markup.html;
018
019import org.apache.wicket.ajax.AjaxRequestTarget;
020import org.apache.wicket.ajax.IAjaxIndicatorAware;
021import org.apache.wicket.ajax.markup.html.AjaxLink;
022import org.apache.wicket.model.IModel;
023import org.apache.wicket.util.lang.Args;
024import org.danekja.java.util.function.serializable.SerializableBiConsumer;
025
026/**
027 * A variant of the {@link AjaxLink} that displays a busy indicator while the ajax request is in
028 * progress.
029 * 
030 * @since 1.2
031 * 
032 * @author Igor Vaynberg (ivaynberg)
033 * @param <T>
034 * 
035 */
036public abstract class IndicatingAjaxLink<T> extends AjaxLink<T> implements IAjaxIndicatorAware
037{
038        private static final long serialVersionUID = 1L;
039        private final AjaxIndicatorAppender indicatorAppender = new AjaxIndicatorAppender();
040
041        /**
042         * Constructor
043         * 
044         * @param id
045         */
046        public IndicatingAjaxLink(final String id)
047        {
048                this(id, null);
049        }
050
051        /**
052         * Constructor
053         * 
054         * @param id
055         * @param model
056         */
057        public IndicatingAjaxLink(final String id, final IModel<T> model)
058        {
059                super(id, model);
060                add(indicatorAppender);
061        }
062
063        @Override
064        public String getAjaxIndicatorMarkupId()
065        {
066                return indicatorAppender.getMarkupId();
067        }
068
069
070        public static <T> IndicatingAjaxLink<T> onClick(String id,
071                SerializableBiConsumer<AjaxLink<T>, AjaxRequestTarget> onClick)
072        {
073                Args.notNull(onClick, "onClick");
074
075                return new IndicatingAjaxLink<T>(id)
076                {
077                        @Override
078                        public void onClick(AjaxRequestTarget target)
079                        {
080                                onClick.accept(this, target);
081                        }
082                };
083        }
084}