001: /*
002: ItsNat Java Web Application Framework
003: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
004: Author: Jose Maria Arranz Santamaria
005:
006: This program is free software: you can redistribute it and/or modify
007: it under the terms of the GNU Affero General Public License as published by
008: the Free Software Foundation, either version 3 of the License, or
009: (at your option) any later version. See the GNU Affero General Public
010: License for more details. See the copy of the GNU Affero General Public License
011: included in this program. If not, see <http://www.gnu.org/licenses/>.
012: */
013:
014: package org.itsnat.impl.core.listener;
015:
016: import java.util.HashMap;
017: import java.util.Map;
018: import org.itsnat.core.ItsNatException;
019: import org.itsnat.impl.core.ItsNatDocumentImpl;
020: import org.itsnat.impl.core.js.listener.DOMBasedEventListenerJSRenderImpl;
021: import org.w3c.dom.Node;
022: import org.w3c.dom.events.EventListener;
023: import org.w3c.dom.events.EventTarget;
024:
025: /**
026: *
027: * @author jmarranz
028: */
029: public abstract class DOMBasedEventListenerRegistryImpl {
030: protected ItsNatDocumentImpl itsNatDoc;
031: protected int capturingCount;
032: protected Map remoteListenersById = new HashMap(); // No es weak porque necesitamos sujetar el listener wrapper pues es un objeto de uso interno para este fin
033:
034: /**
035: * Creates a new instance of DOMBasedEventListenerRegistryImpl
036: */
037: public DOMBasedEventListenerRegistryImpl(
038: ItsNatDocumentImpl itsNatDoc) {
039: this .itsNatDoc = itsNatDoc;
040: }
041:
042: public boolean isEmpty() {
043: return remoteListenersById.isEmpty();
044: }
045:
046: public void checkValidEventTarget(EventTarget target) {
047: isValidEventTarget(target, true);
048: }
049:
050: public boolean isValidEventTarget(EventTarget target,
051: boolean throwErr) {
052: // MSIE no admite asociar eventos DOM a text nodes y FireFox lo permite pero no los procesa por ej. los clicks
053: // En los comentarios sí se permite aunque es absurdo (no pueden ser pulsados etc) delegamos en el programador
054: if (target == null)
055: return true; // Derivar si no se permite
056: Node node = (Node) target; // nuestro AbstractView implementa Node
057: int type = node.getNodeType();
058: if (type == Node.TEXT_NODE)
059: if (throwErr)
060: throw new ItsNatException("Text node is not allowed");
061: else
062: return false;
063: return true;
064: }
065:
066: public int getCapturingCount() {
067: return capturingCount;
068: }
069:
070: public void addDOMBasedEventListener(
071: DOMBasedEventListenerWrapperImpl listenerWrapper) {
072: String id = listenerWrapper.getId();
073: remoteListenersById.put(id, listenerWrapper);
074:
075: getJSRender().addEventListenerCode(listenerWrapper);
076:
077: if (listenerWrapper.getUseCapture())
078: capturingCount++;
079: }
080:
081: public DOMBasedEventListenerWrapperImpl removeDOMBasedEventListenerByIdOnly(
082: String id, boolean updateClient) {
083: if (id == null)
084: return null;
085:
086: DOMBasedEventListenerWrapperImpl listenerWrapper = (DOMBasedEventListenerWrapperImpl) remoteListenersById
087: .remove(id);
088: if (listenerWrapper == null)
089: return null; // Ya se eliminó o nunca se añadió (raro)
090:
091: if (updateClient)
092: getJSRender().removeEventListenerCode(listenerWrapper);
093:
094: if (listenerWrapper.getUseCapture())
095: capturingCount--;
096:
097: return listenerWrapper;
098: }
099:
100: public abstract EventListener[] getDOMBasedEventListeners(
101: EventTarget target, String type, boolean useCapture);
102:
103: public abstract DOMBasedEventListenerJSRenderImpl getJSRender();
104:
105: }
|