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.form.AjaxButton;
022import org.apache.wicket.markup.html.form.Form;
023import org.apache.wicket.model.IModel;
024import org.apache.wicket.util.lang.Args;
025import org.danekja.java.util.function.serializable.SerializableBiConsumer;
026
027/**
028 * A variant of the {@link AjaxButton} that displays a busy indicator while the ajax request is in
029 * progress.
030 * 
031 * @author evan
032 * 
033 */
034public abstract class IndicatingAjaxButton extends AjaxButton implements IAjaxIndicatorAware
035{
036        private static final long serialVersionUID = 1L;
037
038        private final AjaxIndicatorAppender indicatorAppender = new AjaxIndicatorAppender();
039
040        /**
041         * Constructor
042         * 
043         * @param id
044         */
045        public IndicatingAjaxButton(final String id)
046        {
047                this(id, null, null);
048        }
049
050        /**
051         * Constructor
052         * 
053         * @param id
054         * @param model
055         *            model used to set <code>value</code> markup attribute
056         */
057        public IndicatingAjaxButton(final String id, final IModel<String> model)
058        {
059                this(id, model, null);
060        }
061
062        /**
063         * 
064         * Constructor
065         * 
066         * @param id
067         * @param form
068         */
069        public IndicatingAjaxButton(final String id, final Form<?> form)
070        {
071                this(id, null, form);
072        }
073
074        /**
075         * Constructor
076         * 
077         * @param id
078         * @param model
079         * @param form
080         */
081        public IndicatingAjaxButton(final String id, final IModel<String> model, final Form<?> form)
082        {
083                super(id, model, form);
084                add(indicatorAppender);
085        }
086
087        /**
088         * @see IAjaxIndicatorAware#getAjaxIndicatorMarkupId()
089         * @return the markup id of the ajax indicator
090         * 
091         */
092        @Override
093        public String getAjaxIndicatorMarkupId()
094        {
095                return indicatorAppender.getMarkupId();
096        }
097
098
099        public static IndicatingAjaxButton onSubmit(String id,
100                SerializableBiConsumer<AjaxButton, AjaxRequestTarget> onSubmit)
101        {
102                Args.notNull(onSubmit, "onSubmit");
103
104                return new IndicatingAjaxButton(id)
105                {
106                        @Override
107                        public void onSubmit(AjaxRequestTarget target)
108                        {
109                                onSubmit.accept(this, target);
110                        }
111                };
112        }
113}