001: /* Timer.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Mon Sep 26 12:45:22 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zul;
020:
021: import org.zkoss.xml.HTMLs;
022:
023: import org.zkoss.zk.ui.WrongValueException;
024: import org.zkoss.zk.ui.HtmlBasedComponent;
025:
026: /**
027: * Fires one or more {@link org.zkoss.zk.ui.event.Event} after
028: * a specified delay.
029: *
030: * <p>{@link Timer} is a special component that is invisible.
031: *
032: * <p>Notice that the timer won't fire any event until it is attached
033: * to a page.
034: *
035: * @author tomyeh
036: */
037: public class Timer extends HtmlBasedComponent {
038: private int _delay;
039: private boolean _repeats, _running = true;
040:
041: public Timer() {
042: super .setVisible(false);
043: }
044:
045: public Timer(int delay) {
046: this ();
047: _delay = delay;
048: }
049:
050: /** Returns the delay, the number of milliseconds between
051: * successive action events.
052: * <p>Default: 0 (immediately).
053: */
054: public int getDelay() {
055: return _delay;
056: }
057:
058: /** Sets the delay, the number of milliseconds between
059: * successive action events.
060: */
061: public void setDelay(int delay) throws WrongValueException {
062: if (delay < 0)
063: throw new WrongValueException(
064: "Negative delay is not allowed: " + delay);
065: if (delay != _delay) {
066: _delay = delay;
067: smartUpdate("z.delay", Integer.toString(_delay));
068: if (_running)
069: smartUpdate("z.init", true); //init
070: }
071: }
072:
073: /** Returns whether the timer shall send Event repeatly.
074: * <p>Default: false.
075: */
076: public boolean isRepeats() {
077: return _repeats;
078: }
079:
080: /** Sets whether the timer shall send Event repeatly.
081: */
082: public void setRepeats(boolean repeats) {
083: if (_repeats != repeats) {
084: _repeats = repeats;
085: smartUpdate("z.repeats", Boolean.toString(_repeats));
086: if (_running)
087: smartUpdate("z.init", true); //init
088: }
089: }
090:
091: /** Returns whether this timer is running.
092: * <p>Default: true.
093: * @see #stop
094: * @see #start
095: */
096: public boolean isRunning() {
097: return _running;
098: }
099:
100: /** Start or stops the timer.
101: */
102: public void setRunning(boolean running) {
103: if (running)
104: start();
105: else
106: stop();
107: }
108:
109: /** Stops the timer.
110: */
111: public void stop() {
112: if (_running) {
113: _running = false;
114: smartUpdate("z.running", false);
115: }
116: }
117:
118: /** Starts the timer.
119: */
120: public void start() {
121: if (!_running) {
122: _running = true;
123: smartUpdate("z.running", true);
124: }
125: }
126:
127: //-- super --//
128: public String getOuterAttrs() {
129: final StringBuffer sb = new StringBuffer(64).append(super
130: .getOuterAttrs());
131: HTMLs.appendAttribute(sb, "z.delay", _delay);
132: HTMLs.appendAttribute(sb, "z.repeats", _repeats);
133: if (!_running)
134: sb.append(" z.running=\"false\"");
135: return sb.toString();
136: }
137:
138: //-- Component --//
139: /** Not allowd. */
140: public boolean setVisible(boolean visible) {
141: throw new UnsupportedOperationException(
142: "timer is always invisible");
143: }
144:
145: /** Not childable. */
146: public boolean isChildable() {
147: return false;
148: }
149:
150: //-- ComponentCtrl --//
151: protected Object newExtraCtrl() {
152: return new ExtraCtrl();
153: }
154:
155: /** A utility class to implement {@link #getExtraCtrl}.
156: * It is used only by component developers.
157: */
158: protected class ExtraCtrl extends HtmlBasedComponent.ExtraCtrl
159: implements org.zkoss.zk.ui.ext.client.Timer {
160: //Timer//
161: public void onTimer() {
162: if (!_repeats)
163: stop(); //Bug 1829397
164: }
165: }
166: }
|