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.ItsNatDocument;
019: import org.itsnat.core.event.DOMEvent;
020: import org.itsnat.feashow.FeatureTreeNode;
021: import org.w3c.dom.Attr;
022: import org.w3c.dom.Document;
023: import org.w3c.dom.Element;
024: import org.w3c.dom.Node;
025: import org.w3c.dom.events.Event;
026: import org.w3c.dom.events.EventListener;
027: import org.w3c.dom.events.EventTarget;
028: import org.w3c.dom.events.MutationEvent;
029: import org.w3c.dom.CharacterData;
030:
031: public class MutationEventListenerTreeNode extends FeatureTreeNode
032: implements EventListener {
033: protected Element panelElem;
034:
035: public MutationEventListenerTreeNode() {
036: }
037:
038: public void startExamplePanel() {
039: ItsNatDocument itsNatDoc = getItsNatDocument();
040: Document doc = itsNatDoc.getDocument();
041:
042: this .panelElem = doc.getElementById("mutationPanelId");
043:
044: itsNatDoc.addMutationEventListener((EventTarget) panelElem,
045: this , false);
046: }
047:
048: public void endExamplePanel() {
049: ItsNatDocument itsNatDoc = getItsNatDocument();
050: itsNatDoc.removeMutationEventListener((EventTarget) panelElem,
051: this , false);
052:
053: this .panelElem = null;
054: }
055:
056: public void handleEvent(Event evt) {
057: DOMEvent itsNatEvent = (DOMEvent) evt;
058: // ItsNatDocument itsNatDoc = itsNatEvent.getItsNatDocument();
059:
060: MutationEvent mutEvent = (MutationEvent) evt;
061:
062: String msg = "";
063: String type = mutEvent.getType();
064: if (type.equals("DOMNodeInserted")) {
065: Element parent = (Element) mutEvent.getRelatedNode();
066: Node newNode = (Node) mutEvent.getTarget();
067:
068: msg = newNode + " sibling: " + newNode.getNextSibling()
069: + ", parent: " + parent;
070: } else if (type.equals("DOMNodeRemoved")) {
071: Element parent = (Element) mutEvent.getRelatedNode();
072: Node removedNode = (Node) mutEvent.getTarget();
073:
074: msg = removedNode + " parent: " + parent;
075: } else if (type.equals("DOMAttrModified")) {
076: Attr attr = (Attr) mutEvent.getRelatedNode();
077: Element elem = (Element) mutEvent.getTarget();
078: String attrName = mutEvent.getAttrName();
079: String attrValue = elem.getAttribute(attrName);
080: int changeType = mutEvent.getAttrChange();
081: switch (changeType) {
082: case MutationEvent.ADDITION:
083: msg = "added, name: " + attrName + " , value: "
084: + mutEvent.getNewValue();
085: break;
086: case MutationEvent.MODIFICATION:
087: msg = "updated, name: " + attrName + " , new value: "
088: + mutEvent.getNewValue();
089: break;
090: case MutationEvent.REMOVAL:
091: msg = "removed, name: " + attrName;
092: break;
093: }
094: } else if (type.equals("DOMCharacterDataModified")) {
095: CharacterData charNode = (CharacterData) mutEvent
096: .getTarget();
097: msg = "new value: " + mutEvent.getNewValue();
098: }
099:
100: log(mutEvent.getType() + " " + msg + "\n");
101: }
102:
103: }
|