001: /* *************************************************************************
002:
003: Millstone(TM)
004: Open Sourced User Interface Library for
005: Internet Development with Java
006:
007: Millstone is a registered trademark of IT Mill Ltd
008: Copyright (C) 2000-2005 IT Mill Ltd
009:
010: *************************************************************************
011:
012: This library is free software; you can redistribute it and/or
013: modify it under the terms of the GNU Lesser General Public
014: license version 2.1 as published by the Free Software Foundation.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: *************************************************************************
026:
027: For more information, contact:
028:
029: IT Mill Ltd phone: +358 2 4802 7180
030: Ruukinkatu 2-4 fax: +358 2 4802 7181
031: 20540, Turku email: info@itmill.com
032: Finland company www: www.itmill.com
033:
034: Primary source for MillStone information and releases: www.millstone.org
035:
036: ********************************************************************** */
037:
038: package org.millstone.base.event;
039:
040: import java.util.EventObject;
041: import java.util.LinkedList;
042: import java.util.Iterator;
043: import java.lang.reflect.Method;
044:
045: /** Event router class implementing the MillStone inheritable event
046: * listening model. For more information on the event model see the
047: * {@link org.millstone.base.event package documentation}.
048: *
049: * @author IT Mill Ltd.
050: * @version 3.1.1
051: * @since 3.0
052: */
053: public class EventRouter implements MethodEventSource {
054:
055: /** List of registered listeners. */
056: private LinkedList listenerList = null;
057:
058: /* Registers a new listener with the specified activation method to
059: * listen events generated by this component.
060: * Don't add a JavaDoc comment here, we use the default documentation
061: * from implemented interface.
062: */
063: public void addListener(Class eventType, Object object,
064: Method method) {
065:
066: if (listenerList == null)
067: listenerList = new LinkedList();
068:
069: listenerList.add(new ListenerMethod(eventType, object, method));
070: }
071:
072: /* Registers a new listener with the specified named activation method
073: * to listen events generated by this component.
074: * Don't add a JavaDoc comment here, we use the default documentation
075: * from implemented interface.
076: */
077: public void addListener(Class eventType, Object object,
078: String methodName) {
079:
080: if (listenerList == null)
081: listenerList = new LinkedList();
082:
083: listenerList.add(new ListenerMethod(eventType, object,
084: methodName));
085: }
086:
087: /* Removes all registered listeners matching the given parameters.
088: * Don't add a JavaDoc comment here, we use the default documentation
089: * from implemented interface.
090: */
091: public void removeListener(Class eventType, Object target) {
092:
093: if (listenerList != null) {
094: Iterator i = listenerList.iterator();
095: while (i.hasNext()) {
096: try {
097: ListenerMethod lm = (ListenerMethod) i.next();
098: if (lm.matches(eventType, target))
099: i.remove();
100: } catch (java.lang.ClassCastException e) {
101: // Class cast exceptions are ignored
102: }
103: }
104: }
105: }
106:
107: /* Removes the event listener methods matching the given given
108: * paramaters.
109: * Don't add a JavaDoc comment here, we use the default documentation
110: * from implemented interface.
111: */
112: public void removeListener(Class eventType, Object target,
113: Method method) {
114:
115: if (listenerList != null) {
116: Iterator i = listenerList.iterator();
117: while (i.hasNext()) {
118: try {
119: ListenerMethod lm = (ListenerMethod) i.next();
120: if (lm.matches(eventType, target, method))
121: i.remove();
122: } catch (java.lang.ClassCastException e) {
123: // Class cast exceptions are ignored
124: }
125: }
126: }
127: }
128:
129: /* Removes the event listener method matching the given given
130: * paramaters.
131: * Don't add a JavaDoc comment here, we use the default documentation
132: * from implemented interface.
133: */
134: public void removeListener(Class eventType, Object target,
135: String methodName) {
136:
137: // Find the correct method
138: Method[] methods = target.getClass().getMethods();
139: Method method = null;
140: for (int i = 0; i < methods.length; i++)
141: if (methods[i].getName().equals(methodName))
142: method = methods[i];
143: if (method == null)
144: throw new IllegalArgumentException();
145:
146: // Remove the listeners
147: if (listenerList != null) {
148: Iterator i = listenerList.iterator();
149: while (i.hasNext()) {
150: try {
151: ListenerMethod lm = (ListenerMethod) i.next();
152: if (lm.matches(eventType, target, method))
153: i.remove();
154: } catch (java.lang.ClassCastException e) {
155: // Class cast exceptions are ignored
156: }
157: }
158: }
159: }
160:
161: /** Remove all listeners from event router */
162: public void removeAllListeners() {
163: listenerList = null;
164: }
165:
166: /** Send an event to all registered listeners. The listeners will decide
167: * if the activation method should be called or not.
168: *
169: * @param event Event to be sent to all listeners
170: */
171: public void fireEvent(EventObject event) {
172:
173: // It is not necessary to send any events if there are no listeners
174: if (listenerList != null) {
175:
176: // Send the event to all listeners. The listeners themselves
177: // will filter out unwanted events.
178: Iterator i = new LinkedList(listenerList).iterator();
179: while (i.hasNext())
180: ((ListenerMethod) i.next()).receiveEvent(event);
181: }
182: }
183: }
|