01: /*
02: * This file is not part of the ItsNat framework.
03: *
04: * Original source code use and closed source derivatives are authorized
05: * to third parties with no restriction or fee.
06: * The original source code is owned by the author.
07: *
08: * This program is distributed AS IS in the hope that it will be useful,
09: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * Author: Jose Maria Arranz Santamaria
13: * (C) Innowhere Software Services S.L., Spanish company, year 2007
14: */
15:
16: package org.itsnat.feashow.features.core.listeners;
17:
18: import java.util.Date;
19: import org.itsnat.core.ItsNatDocument;
20: import org.itsnat.core.ItsNatTimer;
21: import org.itsnat.core.event.TimerEvent;
22: import org.itsnat.core.event.TimerHandle;
23: import org.itsnat.feashow.FeatureTreeNode;
24: import org.w3c.dom.Document;
25: import org.w3c.dom.Element;
26: import org.w3c.dom.events.Event;
27: import org.w3c.dom.events.EventListener;
28: import org.w3c.dom.events.EventTarget;
29:
30: public class TimersTreeNode extends FeatureTreeNode implements
31: EventListener {
32: protected Element linkToStart;
33: protected ItsNatTimer timer;
34:
35: public TimersTreeNode() {
36: }
37:
38: public void startExamplePanel() {
39: ItsNatDocument itsNatDoc = getItsNatDocument();
40: Document doc = itsNatDoc.getDocument();
41:
42: this .linkToStart = doc.getElementById("linkToStartId");
43:
44: itsNatDoc.addEventListener((EventTarget) linkToStart, "click",
45: this , false);
46:
47: this .timer = itsNatDoc.createItsNatTimer();
48: }
49:
50: public void endExamplePanel() {
51: ItsNatDocument itsNatDoc = getItsNatDocument();
52: itsNatDoc.removeEventListener((EventTarget) linkToStart,
53: "click", this , false);
54:
55: timer.cancel();
56: this .timer = null;
57:
58: this .linkToStart = null;
59: }
60:
61: public void handleEvent(Event evt) {
62: EventListener listener = new EventListener() {
63: public void handleEvent(Event evt) {
64: TimerEvent timerEvt = (TimerEvent) evt;
65: TimerHandle handle = timerEvt.getTimerHandle();
66: long firstTime = handle.getFirstTime();
67: if ((new Date().getTime() - firstTime) > 10000) // to avoid never ending ticks
68: {
69: handle.cancel();
70: log("Scheduled task canceled, id: "
71: + handle.hashCode());
72: } else
73: log("Tick, id: " + handle.hashCode()
74: + " next execution: "
75: + new Date(handle.scheduledExecutionTime()));
76: }
77: };
78: TimerHandle handle = timer.schedule(null, listener, 1000, 2000);
79:
80: log("Scheduled task started, id: " + handle.hashCode()
81: + " first time: " + new Date(handle.getFirstTime())
82: + " period: " + handle.getPeriod());
83: }
84:
85: }
|