1 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 ; 31 import java.lang.reflect.Field ; 32 import java.util.*; 33 34 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 classname = listenerConfig.getAttribute("classname"); 82 try { 83 Class listenerClass = Class.forName(classname); 84 EventListener eventListener = null; 85 try { 86 Method getInstanceMethod = listenerClass.getMethod("getInstance", new Class [0]); 87 eventListener = (EventListener)getInstanceMethod.invoke(null, null); 88 } catch ( NoSuchMethodException 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 } 98 if (conf != null) { 99 ((Configurable)eventListener).configure(conf); 100 } 101 } 102 addEventListener(eventListener); 103 } catch (ClassCastException e) { 104 throw new ConfigurationException("Event listener '"+classname+"' is not of type EventListener", config); 105 } catch (Exception 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 classname = enableConfig.getAttribute("classname"); 113 String method = enableConfig.getAttribute("method", null); 114 boolean enable = enableConfig.getAttributeAsBoolean("enable", true); 115 try { 116 Class eventClass = Class.forName(classname); 117 Field 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 e) { 125 throw new ConfigurationException("Event '"+classname+"' does not provide the required static member 'methods'", config); 126 } catch (Exception e) { 127 throw new ConfigurationException("Event '"+classname+"' could not be loaded", config); 128 } 129 } 130 } 131 } | Popular Tags |