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