001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Darrell Meyer <darrell@mygwt.net> - derived implementation
011: *******************************************************************************/package net.mygwt.ui.client.util;
012:
013: import java.util.ArrayList;
014: import java.util.EventListener;
015: import java.util.HashMap;
016: import java.util.List;
017:
018: import net.mygwt.ui.client.event.BaseEvent;
019: import net.mygwt.ui.client.event.Listener;
020: import net.mygwt.ui.client.event.TypedListener;
021:
022: /**
023: * Maps listeners by event type.
024: */
025: public class EventTable {
026:
027: private HashMap map;
028:
029: /**
030: * Returns <code>true</code> if the event table has any listeners for the
031: * given event type.
032: *
033: * @param eventType the event type
034: * @return the listener state
035: */
036: public boolean hasListener(int eventType) {
037: if (map == null) {
038: return false;
039: }
040: return map.containsKey(new Integer(eventType));
041: }
042:
043: /**
044: * Hooks the listener by the given event type and element.
045: *
046: * @param eventType the event type
047: * @param listener the listener to be added
048: */
049: public void hook(int eventType, Listener listener) {
050: if (map == null) {
051: map = new HashMap();
052: }
053: Integer type = new Integer(eventType);
054: List listeners = (List) map.get(type);
055: if (listeners == null) {
056: listeners = new ArrayList();
057: map.put(type, listeners);
058: }
059: if (!listeners.contains(listener)) {
060: listeners.add(listener);
061: }
062: }
063:
064: /**
065: * Removes all mapped listeners.
066: */
067: public void removeAllListeners() {
068: map.clear();
069: }
070:
071: /**
072: * Sends the event to any registered listeners.
073: *
074: * @param be the base event
075: * @return <code>true</code> if event was canceled by any listeners
076: */
077: public boolean sendEvent(BaseEvent be) {
078: if (map == null)
079: return true;
080: List list = (List) map.get(new Integer(be.type));
081: if (list == null)
082: return true;
083: for (int i = 0; i < list.size(); i++) {
084: Listener l = (Listener) list.get(i);
085: l.handleEvent(be);
086: }
087: return be.doit;
088: }
089:
090: public void unhook(int eventType, EventListener listener) {
091: if (map == null)
092: return;
093: List list = (List) map.get(new Integer(eventType));
094: if (list == null)
095: return;
096: for (int i = list.size() - 1; i >= 0; i--) {
097: TypedListener l = (TypedListener) list.get(i);
098: if (l.getEventListener() == listener) {
099: list.remove(l);
100: }
101: }
102: }
103:
104: /**
105: * Unhooks the given listener for the given event type.
106: *
107: * @param eventType the event type
108: * @param listener the listener to be removed
109: */
110: public void unhook(int eventType, Listener listener) {
111: if (map == null)
112: return;
113: Integer type = new Integer(eventType);
114: List list = (List) map.get(type);
115: if (list == null)
116: return;
117: list.remove(listener);
118: }
119: }
|