001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.wings;
022:
023: import java.awt.event.ActionEvent;
024: import java.util.Vector;
025:
026: /**
027: * This class posts <code>ActionEvent</code>s at specified
028: * intervals.
029: * <br><br>
030: * <strong>ActionEvents posted by WingTimer can be dispatched on thread AWT EventDispatchThread
031: * and on thread WingTimer.thread.</strong><br>
032: * Create timer with postOnEventThread=false for time critical timer events.<br>
033: * <br>
034: * <br>
035: * <b>This is one of the core WingS classes required by all the components</b><br>
036: * <br>
037: * <b>This class is thread safe.</b>
038: **/
039: public class WingTimer implements Runnable {
040: protected static final Vector timers = new Vector();
041: protected static Thread thread;
042:
043: public void run() {
044: WingTimer t;
045: try {
046: while (true) {
047: synchronized (timers) {
048: if (timers.size() == 0) {
049: timers.wait();
050: continue;
051: } else {
052: t = (WingTimer) timers.firstElement();
053: long delay = t.time
054: - System.currentTimeMillis();
055: if (delay > 0) {
056: timers.wait(delay);
057: continue;
058: }
059: removeTimer(t);
060: }
061: }
062: ActionEvent e = new ActionEvent(t,
063: ActionEvent.ACTION_PERFORMED, "timer");
064: WingComponent l1 = t.target;
065: if (l1 != null) {
066: if (t.poet)
067: l1.postOnEventThread(e);
068: else
069: l1.wingProcessActionEvent(e);
070: }
071: // ActionListener l2= t.listener;
072: // if(l2!=null) l2.actionPerformed(e);
073:
074: if (t.repeat)
075: addTimer(t);
076: }
077: } catch (InterruptedException e) {
078: }
079:
080: synchronized (timers) {
081: thread = null;
082: }
083: }
084:
085: protected static void addTimer(WingTimer timer) {
086: synchronized (timers) {
087: removeTimer(timer);
088:
089: if (thread == null) {
090: thread = new Thread(
091: new WingTimer(0, false, null, false));
092: thread.setDaemon(true);
093: thread.start();
094: }
095:
096: timer.time = System.currentTimeMillis() + timer.delay;
097: int i;
098: for (i = 0; i < timers.size(); i++) {
099: if (timer.time < ((WingTimer) timers.elementAt(i)).time) {
100: break;
101: }
102: }
103: timers.insertElementAt(timer, i);
104: if (i == 0)
105: timers.notify();
106: }
107: }
108:
109: protected static void removeTimer(WingTimer timer) {
110: synchronized (timers) {
111: timers.removeElement(timer);
112: timer.time = 0;
113: }
114: }
115:
116: private WingComponent target;
117: // private ActionListener listener;
118: private int delay;
119: private boolean repeat;
120: private long time;
121: private Object data;
122: private boolean poet;
123:
124: /**
125: * Creates a <code>WingTimer</code> with the specified delay time,
126: * repeat mode, and target component.
127: * @param delay milliseconds for the delay
128: * @param repeat specify <code>false</code> to make the timer
129: * stop after sending its first action event
130: * @param target target component
131: */
132: public WingTimer(int delay, boolean repeat, WingComponent target) {
133: this (delay, repeat, target, true);
134: }
135:
136: /**
137: * Creates a <code>WingTimer</code> with the specified delay time,
138: * repeat mode, and target component.
139: * @param delay milliseconds for the delay
140: * @param repeat specify <code>false</code> to make the timer
141: * stop after sending its first action event
142: * @param target target component
143: * @param postOnEventThread specifies whether the events are posted on event thread
144: */
145: public WingTimer(int delay, boolean repeat, WingComponent target,
146: boolean postOnEventThread) {
147: this .delay = delay;
148: this .repeat = repeat;
149: this .target = target;
150: this .poet = postOnEventThread;
151: }
152:
153: /**
154: * Starts the timer
155: */
156: public void start() {
157: addTimer(this );
158: }
159:
160: /**
161: * Stops the timer
162: */
163: public void stop() {
164: if (time != 0)
165: removeTimer(this );
166: }
167:
168: /**
169: * Sets target component
170: * @param target target component
171: */
172: public void setTarget(WingComponent target) {
173: this .target = target;
174: }
175:
176: // public void setListener(ActionListener listener)
177: // {
178: // this.listener= listener;
179: // }
180: public WingComponent getTarget() {
181: return target;
182: }
183:
184: public void setData(Object data) {
185: this .data = data;
186: }
187:
188: public Object getData() {
189: return data;
190: }
191:
192: public void setDelay(int delay) {
193: this.delay = delay;
194: }
195: }
|