001: /* EventHandlerMap.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Wed Mar 28 16:28:40 2007, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zk.ui.metainfo;
020:
021: import java.util.Set;
022: import java.util.Collections;
023: import java.util.List;
024: import java.util.LinkedList;
025: import java.util.Map;
026: import java.util.HashMap;
027: import java.util.Iterator;
028:
029: import org.zkoss.lang.Objects;
030: import org.zkoss.zk.ui.Component;
031:
032: /**
033: * A map of instances of {@link EventHandler}.
034: *
035: * <p>Note: it is not thread safe. Thus, it is better to {@link #clone}
036: * and then modifying the cloned instance if you want to change it
037: * concurrently.
038: *
039: * @author tomyeh
040: */
041: public class EventHandlerMap implements Cloneable, java.io.Serializable {
042: /** The event handler map, (String evtnm, EventHandler evthd).
043: */
044: private Map _evthds;
045:
046: /** Returns whether no event handler at all.
047: */
048: public boolean isEmpty() {
049: return _evthds == null || _evthds.isEmpty();
050: }
051:
052: /** Returns the first effective event handler of the specified event name,
053: * or null if not available.
054: *
055: * <p>It checks whether an event handler is effective by calling
056: * {@link EventHandler#isEffective(Component)}.
057: *
058: * @param comp the component used to evaluate whether an event handler
059: * is effective.
060: * @see EventHandler#isEffective(Component)
061: * @since 3.0.0
062: */
063: public EventHandler get(Component comp, String evtnm) {
064: if (_evthds != null) {
065: final List ehl = (List) _evthds.get(evtnm);
066: if (ehl != null) {
067: for (Iterator it = ehl.iterator(); it.hasNext();) {
068: final EventHandler eh = (EventHandler) it.next();
069: if (eh.isEffective(comp))
070: return eh;
071: }
072: }
073: }
074: return null;
075: }
076:
077: /** Returns a readonly collection of event names (String), or
078: * an empty collection if no event name is registered.
079: * @since 3.0.2
080: */
081: public Set getEventNames() {
082: return _evthds != null ? _evthds.keySet()
083: : Collections.EMPTY_SET;
084: }
085:
086: /** Returns a readonly list of all event handlers associated
087: * with the specified event name, or null if no handler is associated
088: * with.
089: *
090: * <p>Unlike {@link #get(Component,String)}, it returns all
091: * event handlers no matter whether they are effective.
092: *
093: * @since 3.0.0
094: */
095: public List getAll(String evtnm) {
096: if (_evthds != null)
097: return (List) _evthds.get(evtnm);
098: return null;
099: }
100:
101: /**
102: * @deprecated As of release 3.0.0, replaced by getAll(evtnm)[0].
103: * See also {@link #get(Component, String)} or {@link #getAll}
104: */
105: public EventHandler get(String evtnm) {
106: if (_evthds != null) {
107: final List ehl = (List) _evthds.get(evtnm);
108: if (ehl != null)
109: return (EventHandler) ehl.get(0); //must not empty
110: }
111: return null;
112: }
113:
114: /** Adds the event handler for the specified event name.
115: * <p>Note: the new handler won't overwrite the previous one,
116: * unless {@link EventHandler#getCondition} is the same.
117: * Rather, the new handler is appended to the list. You can retreive
118: * list by invoking {@link #getAll}.
119: *
120: * @see #getAll
121: * @since 3.0.0
122: */
123: public void add(String evtnm, EventHandler evthd) {
124: if (evtnm == null || evthd == null)
125: throw new IllegalArgumentException("null");
126:
127: if (_evthds == null)
128: _evthds = new HashMap(4);
129:
130: List ehl = (List) _evthds.get(evtnm);
131: if (ehl == null) {
132: _evthds.put(evtnm, ehl = new LinkedList());
133: } else {
134: for (Iterator it = ehl.iterator(); it.hasNext();) {
135: final EventHandler eh = (EventHandler) it.next();
136: if (Objects.equals(eh.getCondition(), evthd
137: .getCondition()))
138: it.remove(); //replicate
139: }
140: }
141:
142: ehl.add(evthd);
143: }
144:
145: /** Adds all event handlers of the specified map to this map.
146: */
147: public void addAll(EventHandlerMap src) {
148: if (src != null && !src.isEmpty()) {
149: for (Iterator it = src._evthds.entrySet().iterator(); it
150: .hasNext();) {
151: final Map.Entry me = (Map.Entry) it.next();
152: final String evtnm = (String) me.getKey();
153: final List srcl = (List) me.getValue();
154: for (Iterator e = srcl.iterator(); e.hasNext();)
155: add(evtnm, (EventHandler) e.next());
156: }
157: }
158: }
159:
160: //Cloneable//
161: /** Clones this event handler map.
162: */
163: public Object clone() {
164: final EventHandlerMap clone = new EventHandlerMap();
165: clone.addAll(this );
166: return clone;
167: }
168:
169: //Object//
170: public String toString() {
171: return "[evthd:" + _evthds + ']';
172: }
173: }
|