01: /*
02: * $Id: org.eclipse.jdt.ui.prefs 5004 2006-03-17 20:47:08 -0800 (Fri, 17 Mar
03: * 2006) eelco12 $ $Revision: 5004 $ $Date: 2006-03-17 20:47:08 -0800 (Fri, 17
04: * Mar 2006) $
05: *
06: * ==============================================================================
07: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
08: * use this file except in compliance with the License. You may obtain a copy of
09: * the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16: * License for the specific language governing permissions and limitations under
17: * the License.
18: */
19: package wicket.ajax.calldecorator;
20:
21: import wicket.ajax.AbstractDefaultAjaxBehavior;
22: import wicket.ajax.IAjaxCallDecorator;
23: import wicket.util.string.Strings;
24: import wicket.util.time.Duration;
25:
26: /**
27: * Adds throttling to the ajax call. Throttled behaviors only execute once
28: * within the given delay even though they can be triggered multiple times.
29: * <p>
30: * For example, this is useful when attaching an event behavior to the
31: * onkeypress event. It is not desirable to have an ajax call made every time
32: * the user types so we can throttle that call to a desirable delay, such as
33: * once per second. This gives us a near real time ability to provide feedback
34: * without overloading the server.
35: *
36: * @since 1.2
37: *
38: * @author Igor Vaynberg (ivaynberg)
39: *
40: */
41: public final class AjaxCallThrottlingDecorator extends
42: AjaxPostprocessingCallDecorator {
43: private static final long serialVersionUID = 1L;
44:
45: private final Duration duration;
46: private final String id;
47:
48: /**
49: * Construct.
50: *
51: * @param id
52: * throttle id
53: * @param delay
54: * throttle delay
55: */
56: public AjaxCallThrottlingDecorator(String id, Duration delay) {
57: this (null, id, delay);
58: }
59:
60: /**
61: * Construct.
62: *
63: * @param decorator
64: * wrapped decorator
65: * @param id
66: * throttle id
67: * @param delay
68: * throttle delay
69: */
70: public AjaxCallThrottlingDecorator(IAjaxCallDecorator decorator,
71: String id, Duration delay) {
72: super (decorator);
73: if (Strings.isEmpty(id)) {
74: throw new IllegalArgumentException(
75: "id cannot be an empty string");
76: }
77: this .id = id;
78: this .duration = delay;
79: }
80:
81: /**
82: * @see wicket.ajax.calldecorator.AjaxPostprocessingCallDecorator#postDecorateScript(CharSequence)
83: */
84: public final CharSequence postDecorateScript(CharSequence script) {
85: return AbstractDefaultAjaxBehavior.throttleScript(script, id,
86: duration);
87: }
88:
89: }
|