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.ajax;
018
019import java.time.Duration;
020
021import org.apache.wicket.util.lang.Args;
022import org.danekja.java.util.function.serializable.SerializableConsumer;
023
024/**
025 * Automatically re-renders the component it is attached to via AJAX at a regular interval.
026 * 
027 * @since 1.2
028 * 
029 * @author Igor Vaynberg (ivaynberg)
030 * @see #onTimer(AjaxRequestTarget)
031 * @see #onPostProcessTarget(AjaxRequestTarget)
032 */
033public class AjaxSelfUpdatingTimerBehavior extends AbstractAjaxTimerBehavior
034{
035        private static final long serialVersionUID = 1L;
036
037        /**
038         * Construct.
039         * 
040         * @param updateInterval
041         *            {@link Duration} between AJAX callbacks
042         */
043        public AjaxSelfUpdatingTimerBehavior(final Duration updateInterval)
044        {
045                super(updateInterval);
046        }
047
048        @Override
049        protected final void onTimer(final AjaxRequestTarget target)
050        {
051                target.add(getComponent());
052                onPostProcessTarget(target);
053        }
054
055        /**
056         * Give the subclass a chance to add something to the target, like a javascript effect call.
057         * Called after the hosting component has been added to the target.
058         * 
059         * @param target
060         *            The AJAX target
061         */
062        protected void onPostProcessTarget(final AjaxRequestTarget target)
063        {
064        }
065
066        /**
067         * Creates an {@link AbstractAjaxTimerBehavior} based on lambda expressions
068         * 
069         * @param interval
070         *            the interval for the self update
071         * @param onTimer
072         *            the {@code SerializableConsumer} which accepts the {@link AjaxRequestTarget}
073         * @return the {@link AbstractAjaxTimerBehavior}
074         */
075        public static AjaxSelfUpdatingTimerBehavior onSelfUpdate(Duration interval, SerializableConsumer<AjaxRequestTarget> onTimer)
076        {
077                Args.notNull(onTimer, "onTimer");
078
079                return new AjaxSelfUpdatingTimerBehavior(interval)
080                {
081                        private static final long serialVersionUID = 1L;
082
083                        @Override
084                        protected void onPostProcessTarget(AjaxRequestTarget target)
085                        {
086                                onTimer.accept(target);
087                        }
088                };
089        }
090}