001: /*
002: * This file is not part of the ItsNat framework.
003: *
004: * Original source code use and closed source derivatives are authorized
005: * to third parties with no restriction or fee.
006: * The original source code is owned by the author.
007: *
008: * This program is distributed AS IS in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
011: *
012: * Author: Jose Maria Arranz Santamaria
013: * (C) Innowhere Software Services S.L., Spanish company, year 2007
014: */
015:
016: package org.itsnat.feashow.features.core.listeners;
017:
018: import org.itsnat.core.CometNotifier;
019: import org.itsnat.core.ItsNatDocument;
020: import org.itsnat.feashow.FeatureTreeNode;
021: import org.w3c.dom.Document;
022: import org.w3c.dom.Element;
023: import org.w3c.dom.events.Event;
024: import org.w3c.dom.events.EventListener;
025: import org.w3c.dom.events.EventTarget;
026:
027: public class CometNotifierTreeNode extends FeatureTreeNode implements
028: EventListener {
029: protected Element linkToStart;
030: protected CometNotifier notifier;
031: protected Thread backgroundThr;
032: protected boolean started = false;
033:
034: public CometNotifierTreeNode() {
035: }
036:
037: public void startExamplePanel() {
038: ItsNatDocument itsNatDoc = getItsNatDocument();
039: Document doc = itsNatDoc.getDocument();
040:
041: this .linkToStart = doc.getElementById("linkToStartId");
042:
043: itsNatDoc.addEventListener((EventTarget) linkToStart, "click",
044: this , false);
045: }
046:
047: public void endExamplePanel() {
048: ItsNatDocument itsNatDoc = getItsNatDocument();
049: itsNatDoc.removeEventListener((EventTarget) linkToStart,
050: "click", this , false);
051:
052: this .linkToStart = null;
053:
054: stopAll();
055: }
056:
057: public void stopAll() {
058: if (!started)
059: return;
060:
061: this .started = false;
062:
063: notifier.stop();
064: // Optional: try{ backgroundThr.join(); }catch(InterruptedException ex) { }
065:
066: this .notifier = null;
067:
068: this .backgroundThr = null;
069: }
070:
071: public void handleEvent(Event evt) {
072: // EventTarget currTarget = evt.getCurrentTarget();
073:
074: if (started) {
075: stopAll();
076: log("COMET notifier stopped");
077: }
078:
079: this .started = true;
080:
081: final ItsNatDocument itsNatDoc = getItsNatDocument();
082:
083: final CometNotifier notifier = itsNatDoc
084: .getClientDocumentOwner().createCometNotifier();
085: this .notifier = notifier;
086:
087: log("COMET notifier started");
088:
089: this .backgroundThr = new Thread() {
090: public void run() {
091: log("Background server task started");
092:
093: for (int i = 0; i < 10; i++) {
094: try {
095: Thread.sleep(2000);
096: } catch (InterruptedException ex) {
097: }
098:
099: synchronized (itsNatDoc) {
100: if (!started)
101: return;
102: log("Tick"); // calls ItsNatDocument.addCodeToSend(Object)
103: }
104: notifier.notifyClient();
105: }
106:
107: synchronized (itsNatDoc) {
108: if (!started)
109: return;
110: log("Time limit");
111: }
112: notifier.notifyClient();
113: stopAll();
114: }
115: };
116:
117: backgroundThr.start();
118: }
119: }
|