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 org.itsnat.core.ItsNatDocument;
19: import org.itsnat.core.domutil.ItsNatDOMUtil;
20: import org.itsnat.feashow.FeatureTreeNode;
21: import org.w3c.dom.Document;
22: import org.w3c.dom.Element;
23: import org.w3c.dom.events.Event;
24: import org.w3c.dom.events.EventListener;
25: import org.w3c.dom.events.EventTarget;
26:
27: public class DOMEventListenerTreeNode extends FeatureTreeNode implements
28: EventListener {
29: protected Element clickable1;
30: protected Element clickable2;
31:
32: public DOMEventListenerTreeNode() {
33: }
34:
35: public void startExamplePanel() {
36: ItsNatDocument itsNatDoc = getItsNatDocument();
37: Document doc = itsNatDoc.getDocument();
38:
39: this .clickable1 = doc.getElementById("clickableId1");
40: this .clickable2 = doc.getElementById("clickableId2");
41:
42: enableFirst();
43: }
44:
45: public void endExamplePanel() {
46: ItsNatDocument itsNatDoc = getItsNatDocument();
47: itsNatDoc.removeEventListener((EventTarget) clickable1,
48: "click", this , false);
49: itsNatDoc.removeEventListener((EventTarget) clickable2,
50: "click", this , false);
51:
52: this .clickable1 = null;
53: this .clickable2 = null;
54: }
55:
56: public void enableFirst() {
57: enable(clickable1);
58: disable(clickable2);
59: }
60:
61: public void enableSecond() {
62: disable(clickable1);
63: enable(clickable2);
64: }
65:
66: public void enable(Element clickable) {
67: ItsNatDocument itsNatDoc = getItsNatDocument();
68: itsNatDoc.addEventListener((EventTarget) clickable, "click",
69: this , false);
70: ItsNatDOMUtil.setTextContent(clickable, "You can click me!!");
71: clickable.setAttribute("style", "color:red;");
72: }
73:
74: public void disable(Element clickable) {
75: ItsNatDocument itsNatDoc = getItsNatDocument();
76: itsNatDoc.removeEventListener((EventTarget) clickable, "click",
77: this , false);
78: ItsNatDOMUtil.setTextContent(clickable, "You cannot click me");
79: clickable.removeAttribute("style");
80: }
81:
82: public void handleEvent(Event evt) {
83: ItsNatDocument itsNatDoc = getItsNatDocument();
84: itsNatDoc.addCodeToSend("alert('Clicked!!');");
85:
86: EventTarget currTarget = evt.getCurrentTarget();
87: if (currTarget == clickable1) {
88: enableSecond();
89: } else {
90: enableFirst();
91: }
92:
93: log(evt.getCurrentTarget() + " " + evt.getType());
94: }
95:
96: }
|