Source code: org/metacosm/util/MCEventDispatcher.java
1 /*
2 Metacosm, an object-oriented network game framework
3 Copyright (C) 1999-2001 Metacosm Development Team
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 package org.metacosm.util;
21
22 import java.util.Iterator;
23 import java.util.Set;
24 import java.util.Map;
25
26 /**
27 * EventDispatcher should be able to route message to registered listeners
28 * according to the message type. Ideally, types should be ordered in a tree,
29 * to allow listeners to register simply to a whole family of message
30 * (not implemented yet)
31 *
32 */
33 public class MCEventDispatcher {
34
35 public MCEventDispatcher() {
36 listenersToAll = (Set) CollectionFactory.createStandardSet("org.metacosm.util.StringKey");
37 listenersByMsgType = (Map) CollectionFactory.createStandardMap("org.metacosm.util.StringKey");
38 }
39
40 public void addListener (MCEventListener el, String msgType) {
41 if (msgType.equals("all")) {
42 listenersToAll.add(el);
43 } else {
44 StringKey msgTypeKey = new StringKey(msgType);
45
46 Set listenersSet = (Set) listenersByMsgType.get(msgTypeKey);
47
48 if (listenersSet == null) {
49 listenersSet = (Set) CollectionFactory.createStandardSet("org.metacosm.util.StringKey");
50 listenersByMsgType.put(msgTypeKey,listenersSet);
51 }
52 listenersSet.add(el);
53 }
54 }
55
56 public void removeListener (MCEventListener el,String msgType) {
57 if (msgType.equals("all")) {
58 listenersToAll.remove(el);
59 } else {
60 StringKey msgTypeKey = new StringKey(msgType);
61
62 Set listenersSet = (Set) listenersByMsgType.get(msgTypeKey);
63
64 if (listenersSet != null) {
65 listenersSet.remove(el);
66 }
67 }
68 }
69
70 public void notifyListeners(MCEvent adminMessage) {
71 // Notify listener of all msg types
72 for (Iterator it = listenersToAll.iterator(); it.hasNext(); ) {
73 // (it.next()).getClass().toString();
74 ((MCEventListener)it.next()).MCEventReceived(adminMessage);
75 }
76
77 // Notify listener of this msg type
78 StringKey msgTypeKey = new StringKey(adminMessage.getType());
79
80 Set listenersSet = (Set) listenersByMsgType.get(msgTypeKey);
81
82 if (listenersSet != null) {
83 for (Iterator it = listenersSet.iterator(); it.hasNext(); ) {
84 ((MCEventListener)it.next()).MCEventReceived(adminMessage);
85 }
86 }
87 }
88
89 private Set listenersToAll;
90 private Map listenersByMsgType;
91 }
92