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.attributes;
018
019import org.apache.wicket.markup.html.WebComponent;
020import org.apache.wicket.util.io.IClusterable;
021import org.apache.wicket.util.lang.Args;
022import java.time.Duration;
023
024/**
025 * Class to keep track of throttling settings.
026 * 
027 * @author ivaynberg
028 */
029public class ThrottlingSettings implements IClusterable
030{
031        private static final long serialVersionUID = 1L;
032
033        private Duration delay;
034        private final String id;
035        private boolean postponeTimerOnUpdate;
036
037        /**
038         * Construct without id (will default to the component's markup ID) and postponeTimerOnUpdate
039         * set to false.
040         * 
041         * @param delay
042         *            throttle delay
043         */
044        public ThrottlingSettings(final Duration delay)
045        {
046                this(null, delay, false);
047        }
048
049        /**
050         * Construct without id (will default to the component's markup ID).
051         * 
052         * @param delay
053         *            throttle delay
054         * @param postponeTimerOnUpdate
055         *            postpone timer
056         */
057        public ThrottlingSettings(final Duration delay, boolean postponeTimerOnUpdate)
058        {
059                this(null, delay, postponeTimerOnUpdate);
060        }
061
062        /**
063         * Construct.
064         * 
065         * @param id
066         *            throttle id
067         * @param delay
068         *            throttle delay
069         */
070        public ThrottlingSettings(final String id, final Duration delay)
071        {
072                this(id, delay, false);
073        }
074
075        /**
076         * Construct.
077         * 
078         * @param id
079         *            throttle id
080         * @param delay
081         *            the amount of time the action should be postponed
082         */
083        public ThrottlingSettings(final String id, final Duration delay,
084                final boolean postponeTimerOnUpdate)
085        {
086                this.id = id;
087                this.delay = Args.notNull(delay, "delay");
088                this.postponeTimerOnUpdate = postponeTimerOnUpdate;
089        }
090
091        /**
092         * @return the amount of time the action should be postponed
093         */
094        public Duration getDelay()
095        {
096                return delay;
097        }
098
099        public void setDelay(Duration delay)
100        {
101                this.delay = Args.notNull(delay, "delay");
102        }
103
104        /**
105         * This id is used by the client-side throttling code to keep track of the various event
106         * throttles. Normally you can just use any unique ID here, such as the component's markupId (
107         * {@link WebComponent#getMarkupId()}). To unite several different events with one throttle,
108         * give them the same ID. If this is null, it will (on the client only) default to the
109         * component's markupId.
110         * 
111         * @return throttle id
112         */
113        public String getId()
114        {
115                return id;
116        }
117
118        /**
119         * If it is set to true, then the timer is reset each time the throttle function gets called.
120         * Use this behaviour if you want something to happen at X milliseconds after the
121         * <strong>last</strong> call to throttle. If the parameter is not set, or set to false, then
122         * the timer is not reset.
123         */
124        public boolean getPostponeTimerOnUpdate()
125        {
126                return postponeTimerOnUpdate;
127        }
128
129        public void setPostponeTimerOnUpdate(boolean postponeTimerOnUpdate)
130        {
131                this.postponeTimerOnUpdate = postponeTimerOnUpdate;
132        }
133
134}