KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > PropertyEventDispatcher


1 /**
2  * $RCSfile: PropertyEventDispatcher.java,v $
3  * $Revision: 1.2 $
4  * $Date: 2005/07/26 17:10:33 $
5  *
6  * Copyright (C) 2004-2005 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.util;
13
14 import org.jivesoftware.util.Log;
15
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.concurrent.CopyOnWriteArrayList JavaDoc;
19
20 /**
21  * Dispatches property events. Each event has a {@link EventType type}
22  * and optional parameters, as follows:<p>
23  *
24  * <table border="1">
25  * <tr><th>Event Type</th><th>Extra Params</th></tr>
26  * <tr><td>{@link EventType#property_set property_set}</td><td>A param named <tt>value</tt> that
27  * has the value of the property set.</td></tr>
28  * <tr><td>{@link EventType#property_deleted property_deleted}</td><td><i>None</i></td></tr>
29  * <tr><td>{@link EventType#xml_property_set xml_property_set}</td><td>A param named <tt>value</tt> that
30  * has the value of the property set.</td></tr>
31  * <tr><td>{@link EventType#xml_property_deleted xml_property_deleted}</td><td><i>None</i></td></tr>
32  * </table>
33  *
34  * @author Matt Tucker
35  */

36 public class PropertyEventDispatcher {
37
38     private static List JavaDoc<PropertyEventListener> listeners =
39             new CopyOnWriteArrayList JavaDoc<PropertyEventListener>();
40
41     private PropertyEventDispatcher() {
42         // Not instantiable.
43
}
44
45     /**
46      * Registers a listener to receive events.
47      *
48      * @param listener the listener.
49      */

50     public static void addListener(PropertyEventListener listener) {
51         if (listener == null) {
52             throw new NullPointerException JavaDoc();
53         }
54         listeners.add(listener);
55     }
56
57     /**
58      * Unregisters a listener to receive events.
59      *
60      * @param listener the listener.
61      */

62     public static void removeListener(PropertyEventListener listener) {
63         listeners.remove(listener);
64     }
65
66     /**
67      * Dispatches an event to all listeners.
68      *
69      * @param property the property.
70      * @param eventType the event type.
71      * @param params event parameters.
72      */

73     public static void dispatchEvent(String JavaDoc property, EventType eventType, Map JavaDoc params) {
74         for (PropertyEventListener listener : listeners) {
75             try {
76                 switch (eventType) {
77                     case property_set: {
78                         listener.propertySet(property, params);
79                         break;
80                     }
81                     case property_deleted: {
82                         listener.propertyDeleted(property, params);
83                         break;
84                     }
85                     case xml_property_set: {
86                         listener.xmlPropertySet(property, params);
87                         break;
88                     }
89                     case xml_property_deleted: {
90                         listener.xmlPropertyDeleted(property, params);
91                         break;
92                     }
93                     default:
94                         break;
95                 }
96             }
97             catch (Exception JavaDoc e) {
98                 Log.error(e);
99             }
100         }
101     }
102
103     /**
104      * Represents valid event types.
105      */

106     public enum EventType {
107
108         /**
109          * A property was set.
110          */

111         property_set,
112
113         /**
114          * A property was deleted.
115          */

116         property_deleted,
117
118         /**
119          * An XML property was set.
120          */

121         xml_property_set,
122
123         /**
124          * An XML property was deleted.
125          */

126         xml_property_deleted;
127     }
128 }
Popular Tags