KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > event > GroupEventDispatcher


1 /**
2  * $RCSfile: GroupEventDispatcher.java,v $
3  * $Revision: 1.3 $
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.messenger.event;
13
14 import org.jivesoftware.util.Log;
15 import org.jivesoftware.messenger.group.Group;
16
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.concurrent.CopyOnWriteArrayList JavaDoc;
20
21 /**
22  * Dispatches group events. Each event has a {@link EventType type}
23  * and optional parameters, as follows:<p>
24  *
25  * <table border="1">
26  * <tr><th>Event Type</th><th>Extra Params</th></tr>
27  * <tr><td>{@link EventType#group_created group_created}</td><td><i>None</i></td></tr>
28  * <tr><td>{@link EventType#group_deleting group_deleting}</td><td><i>None</i></td></tr>
29  * <tr><td>{@link EventType#member_added member_added}</td><td>A param named <tt>member</tt> with a String username as a
30  * payload</td></tr>
31  * <tr><td>{@link EventType#member_removed member_removed}</td><td>A param named <tt>member</tt> with a String username as a
32  * payload</td></tr>
33  * <tr><td>{@link EventType#admin_added admin_added}</td><td>A param named <tt>admin</tt> with a String username
34  * as a payload</td></tr>
35  * <tr><td>{@link EventType#admin_removed admin_removed}</td><td>A param named <tt>admin</tt> with a String username
36  * as a payload</td></tr>
37  * <tr valign="top"><td>{@link EventType#group_modified group_modified}</td><td>
38  * <table><tr><td><b>Reason</b></td><td><b>Key</b></td><td><b>Value</b></td></tr>
39  * <tr><td colspan="3">Name modified</td></tr>
40  * <tr><td>&nbsp;</td><td>type</td><td>nameModified</td></tr>
41  * <tr><td>&nbsp;</td><td>originalValue</td><td><i>(Name before it was modified)</i>
42  * </td></tr>
43  * <tr><td colspan="3"><hr></td></tr>
44  * <tr><td colspan="3">Description modified</td></tr>
45  * <tr><td>&nbsp;</td><td>type</td><td>descriptionModified</td></tr>
46  * <tr><td>&nbsp;</td><td>originalValue</td><td><i>(Description before it was
47  * modified)</i></td></tr>
48  * <tr><td colspan="3"><hr></td></tr>
49  * <tr><td colspan="3">Property modified</td></tr>
50  * <tr><td>&nbsp;</td><td>type</td><td>propertyModified</td></tr>
51  * <tr><td>&nbsp;</td><td>propertyKey</td><td><i>(Name of the property)</i></td>
52  * </tr>
53  * <tr><td>&nbsp;</td><td>originalValue</td><td><i>(Property value before it was
54  * modified)</i></td></tr>
55  * <tr><td colspan="3"><hr></td></tr>
56  * <tr><td colspan="3">Property added</td></tr>
57  * <tr><td>&nbsp;</td><td>type</td><td>propertyAdded</td></tr>
58  * <tr><td>&nbsp;</td><td>propertyKey</td><td><i>(Name of the new property)</i></td>
59  * </tr>
60  * <tr><td colspan="3"><hr></td></tr>
61  * <tr><td colspan="3">Property deleted</td></tr>
62  * <tr><td>&nbsp;</td><td>type</td><td>propertyDeleted</td></tr>
63  * <tr><td>&nbsp;</td><td>propertyKey</td><td><i>(Name of the property deleted)</i></td></tr></table></td></tr>
64  * </table>
65  *
66  * @author Matt Tucker
67  */

68 public class GroupEventDispatcher {
69
70     private static List JavaDoc<GroupEventListener> listeners =
71             new CopyOnWriteArrayList JavaDoc<GroupEventListener>();
72
73     private GroupEventDispatcher() {
74         // Not instantiable.
75
}
76
77     /**
78      * Registers a listener to receive events.
79      *
80      * @param listener the listener.
81      */

82     public static void addListener(GroupEventListener listener) {
83         if (listener == null) {
84             throw new NullPointerException JavaDoc();
85         }
86         listeners.add(listener);
87     }
88
89     /**
90      * Unregisters a listener to receive events.
91      *
92      * @param listener the listener.
93      */

94     public static void removeListener(GroupEventListener listener) {
95         listeners.remove(listener);
96     }
97
98     /**
99      * Dispatches an event to all listeners.
100      *
101      * @param group the group.
102      * @param eventType the event type.
103      * @param params event parameters.
104      */

105     public static void dispatchEvent(Group group, EventType eventType, Map JavaDoc params) {
106         for (GroupEventListener listener : listeners) {
107             try {
108                 switch (eventType) {
109                     case group_created: {
110                         listener.groupCreated(group, params);
111                         break;
112                     }
113                     case group_deleting: {
114                         listener.groupDeleting(group, params);
115                         break;
116                     }
117                     case member_added: {
118                         listener.memberAdded(group, params);
119                         break;
120                     }
121                     case member_removed: {
122                         listener.memberRemoved(group, params);
123                         break;
124                     }
125                     case admin_added: {
126                         listener.adminAdded(group, params);
127                         break;
128                     }
129                     case admin_removed: {
130                         listener.adminRemoved(group, params);
131                         break;
132                     }
133                     case group_modified: {
134                         listener.groupModified(group, params);
135                         break;
136                     }
137                     default:
138                         break;
139                 }
140             }
141             catch (Exception JavaDoc e) {
142                 Log.error(e);
143             }
144         }
145     }
146
147     /**
148      * Represents valid event types.
149      */

150     public enum EventType {
151
152         /**
153          * A new group was created.
154          */

155         group_created,
156
157         /**
158          * A group is about to be deleted. Note that this event is fired before
159          * a group is actually deleted. This allows for referential cleanup
160          * before the group is gone.
161          */

162         group_deleting,
163
164         /**
165          * The name, description, or extended property of a group was changed.
166          */

167         group_modified,
168
169         /**
170          * A member was added to a group.
171          */

172         member_added,
173
174         /**
175          * A member was removed from a group.
176          */

177         member_removed,
178
179         /**
180          * An administrator was added to a group.
181          */

182         admin_added,
183
184         /**
185          * An administrator was removed from a group.
186          */

187         admin_removed;
188     }
189 }
Popular Tags