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: package org.itsnat.manual;
16:
17: import org.itsnat.core.ItsNatDocument;
18: import org.itsnat.core.ItsNatServletRequest;
19: import org.itsnat.core.event.ItsNatEvent;
20: import org.itsnat.core.html.ItsNatHTMLDocument;
21: import org.w3c.dom.Element;
22: import org.w3c.dom.Text;
23: import org.w3c.dom.events.Event;
24: import org.w3c.dom.events.EventListener;
25: import org.w3c.dom.events.EventTarget;
26: import org.w3c.dom.html.HTMLDocument;
27:
28: public class CoreExampleProcessor implements EventListener {
29: protected ItsNatHTMLDocument itsNatDoc;
30: protected Element clickElem1;
31: protected Element clickElem2;
32:
33: public CoreExampleProcessor(ItsNatHTMLDocument itsNatDoc) {
34: this .itsNatDoc = itsNatDoc;
35: load();
36: }
37:
38: public void load() {
39: HTMLDocument doc = itsNatDoc.getHTMLDocument();
40: this .clickElem1 = doc.getElementById("clickableId1");
41: this .clickElem2 = doc.getElementById("clickableId2");
42:
43: clickElem1.setAttribute("style", "color:red;");
44: Text text1 = (Text) clickElem1.getFirstChild();
45: text1.setData("Click Me!");
46:
47: Text text2 = (Text) clickElem2.getFirstChild();
48: text2.setData("Cannot be clicked");
49:
50: Element noteElem = doc.createElement("p");
51: noteElem.appendChild(doc
52: .createTextNode("Ready to receive clicks..."));
53: doc.getBody().appendChild(noteElem);
54:
55: itsNatDoc.addEventListener((EventTarget) clickElem1, "click",
56: this , false);
57: }
58:
59: public void handleEvent(Event evt) {
60: EventTarget currTarget = evt.getCurrentTarget();
61: if (currTarget == clickElem1) {
62: removeClickable(clickElem1);
63: setAsClickable(clickElem2);
64: } else {
65: setAsClickable(clickElem1);
66: removeClickable(clickElem2);
67: }
68:
69: ItsNatEvent itsNatEvt = (ItsNatEvent) evt;
70: ItsNatServletRequest itsNatReq = itsNatEvt
71: .getItsNatServletRequest();
72: ItsNatDocument itsNatDoc = itsNatReq.getItsNatDocument();
73: HTMLDocument doc = (HTMLDocument) itsNatDoc.getDocument();
74: Element noteElem = doc.createElement("p");
75: noteElem.appendChild(doc.createTextNode("Clicked "
76: + ((Element) currTarget).getAttribute("id")));
77: doc.getBody().appendChild(noteElem);
78: }
79:
80: public void setAsClickable(Element elem) {
81: elem.setAttribute("style", "color:red;");
82: Text text = (Text) elem.getFirstChild();
83: text.setData("Click Me!");
84: itsNatDoc.addEventListener((EventTarget) elem, "click", this ,
85: false);
86: }
87:
88: public void removeClickable(Element elem) {
89: elem.removeAttribute("style");
90: Text text = (Text) elem.getFirstChild();
91: text.setData("Cannot be clicked");
92: itsNatDoc.removeEventListener((EventTarget) elem, "click",
93: this , false);
94: }
95: }
|