KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > event > EventDispatcher


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/event/EventDispatcher.java,v 1.5 2004/07/28 09:36:24 ib Exp $
3  * $Revision: 1.5 $
4  * $Date: 2004/07/28 09:36:24 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.event;
25
26 import org.apache.slide.util.conf.Configurable;
27 import org.apache.slide.util.conf.Configuration;
28 import org.apache.slide.util.conf.ConfigurationException;
29
30 import java.lang.reflect.Method JavaDoc;
31 import java.lang.reflect.Field JavaDoc;
32 import java.util.*;
33
34 /**
35  * Event dispatcher class
36  *
37  * @version $Revision: 1.5 $
38  */

39 public class EventDispatcher implements Configurable {
40     private static EventDispatcher eventDispatcher = new EventDispatcher();
41
42     private List eventListeners = new ArrayList();
43
44     private EventDispatcher() {
45     }
46
47     public static EventDispatcher getInstance() {
48         return eventDispatcher;
49     }
50
51     public void addEventListener(EventListener listener) {
52             eventListeners.add(listener);
53     }
54
55     public void fireVetoableEvent(VetoableEventMethod eventMethod, EventObject event) throws VetoException {
56         for (Iterator i = eventListeners.iterator(); i.hasNext(); ) {
57             EventListener listener = (EventListener)i.next();
58             if ( listener instanceof GlobalListener ) {
59                 ((GlobalListener)listener).vetoableEventFired(eventMethod, event);
60             } else {
61                 eventMethod.fireVetaoableEvent(listener, event);
62             }
63         }
64     }
65
66     public void fireEvent(EventMethod eventMethod, EventObject event) {
67         for (Iterator i = eventListeners.iterator(); i.hasNext(); ) {
68             EventListener listener = (EventListener)i.next();
69             if ( listener instanceof GlobalListener ) {
70                 ((GlobalListener)listener).eventFired(eventMethod, event);
71             } else {
72                 eventMethod.fireEvent(listener, event);
73             }
74         }
75     }
76
77     public void configure(Configuration config) throws ConfigurationException {
78         Enumeration listenerConfigs = config.getConfigurations("listener");
79         while (listenerConfigs.hasMoreElements()) {
80             Configuration listenerConfig = (Configuration)listenerConfigs.nextElement();
81             String JavaDoc classname = listenerConfig.getAttribute("classname");
82             try {
83                 Class JavaDoc listenerClass = Class.forName(classname);
84                 EventListener eventListener = null;
85                 try {
86                     Method JavaDoc getInstanceMethod = listenerClass.getMethod("getInstance", new Class JavaDoc[0]);
87                     eventListener = (EventListener)getInstanceMethod.invoke(null, null);
88                 } catch ( NoSuchMethodException JavaDoc e) {
89                     eventListener = (EventListener)listenerClass.newInstance();
90                 }
91                 if ( eventListener instanceof Configurable ) {
92                    Configuration conf = null;
93                    try {
94                       conf = listenerConfig.getConfiguration("configuration");
95                    } catch (ConfigurationException e) {
96                       // ignore, listener has no configuration element, OK too
97
}
98                    if (conf != null) {
99                       ((Configurable)eventListener).configure(conf);
100                    }
101                 }
102                 addEventListener(eventListener);
103             } catch (ClassCastException JavaDoc e) {
104                 throw new ConfigurationException("Event listener '"+classname+"' is not of type EventListener", config);
105             } catch (Exception JavaDoc e) {
106                 throw new ConfigurationException("Event listener '"+classname+"' could not be loaded", config);
107             }
108         }
109         Enumeration enableConfigs = config.getConfigurations("event");
110         while (enableConfigs.hasMoreElements()) {
111             Configuration enableConfig = (Configuration)enableConfigs.nextElement();
112             String JavaDoc classname = enableConfig.getAttribute("classname");
113             String JavaDoc method = enableConfig.getAttribute("method", null);
114             boolean enable = enableConfig.getAttributeAsBoolean("enable", true);
115             try {
116                 Class JavaDoc eventClass = Class.forName(classname);
117                 Field JavaDoc methodsField = eventClass.getField("methods");
118                 AbstractEventMethod[] methods = (AbstractEventMethod [])methodsField.get(null);
119                 for ( int i = 0; i < methods.length; i++ ) {
120                     if ( method == null || methods[i].getName().equals(method) ) {
121                         methods[i].setEnabled(enable);
122                     }
123                 }
124             } catch (NoSuchFieldException JavaDoc e) {
125                 throw new ConfigurationException("Event '"+classname+"' does not provide the required static member 'methods'", config);
126             } catch (Exception JavaDoc e) {
127                 throw new ConfigurationException("Event '"+classname+"' could not be loaded", config);
128             }
129         }
130     }
131 }
Popular Tags