View Javadoc
1 /***
2 * Copyright (c) 2003 held jointly by the individual authors.
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; with out even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 *
18 * > http://www.gnu.org/copyleft/lesser.html
19 * > http://www.opensource.org/licenses/lgpl-license.php
20 */
21 package net.mlw.gfw.event;
22
23 import java.util.HashMap;
24 import java.util.LinkedHashMap;
25 import java.util.Map;
26
27 /*** This EventHandler contains a Map of EventHandler(s). It allows
28 * EventHandler(s) to be added for a given type of Event. See
29 * addEventHandler(Class, EventHandler)
30 *
31 * @author Matthew L. Wilson
32 * @version $Revision: 1.5 $ $Date: 2004/06/29 20:32:57 $
33 */
34 public class EventHandlerTypeDelegator implements EventHandler
35 {
36 public static EventProcessingMode MODE_DEFAULT = new EventProcessingMode("Default");
37
38 /*** The EventHandler that is called if no EventHandler is found in the Map. **/
39 private EventHandler defaultBehavour;
40
41 /*** Holds all the publisher of the active mode. */
42 private Map eventHandlers = new LinkedHashMap();
43
44 private Map modes = new HashMap(1);
45
46 /*** Default constructor.
47 */
48 public EventHandlerTypeDelegator()
49 {
50 modes.put(MODE_DEFAULT, eventHandlers);
51 this.defaultBehavour = new EventHandler()
52 {
53 public void onEvent(Event event)
54 {
55 throw new EventNotProcessedException(event);
56 }
57 };
58 }
59
60 /***
61 * @param defaultBehavour
62 */
63 public EventHandlerTypeDelegator(EventHandler defaultBehavour)
64 {
65 modes.put(MODE_DEFAULT, eventHandlers);
66 if (defaultBehavour == null)
67 {
68 throw new IllegalArgumentException("defaultBehavour is a required parameter.");
69 }
70 this.defaultBehavour = defaultBehavour;
71
72 }
73
74 /*** Adds a EventHandler to the internal List of EventHandlers.
75 *
76 * @param eventHandler The EventHandler to add.
77 */
78 public void addEventHandler(Class eventClass, EventHandler eventHandler)
79 {
80 EventHandler eventHandlerInMap = (EventHandler) eventHandlers.get(eventClass.getName());
81 //If there are no EventHandler(s) for this name.
82 if (eventHandlerInMap == null)
83 {
84 eventHandlers.put(eventClass.getName(), eventHandler);
85 }
86 else
87 {
88 //If there are many EventHandler(s) for this name, in a EventHandlerList.
89 if (eventHandlerInMap instanceof EventHandlerList)
90 {
91 ((EventHandlerList) eventHandlerInMap).addEventHandler(eventHandler);
92 }
93 //There is a plain old EventHandler, so take the existing on and the new
94 //new and place it in a new EventHandlerList.
95 else
96 {
97 EventHandlerList allListenersForThisName = new EventHandlerList();
98 allListenersForThisName.addEventHandler(eventHandlerInMap);
99 allListenersForThisName.addEventHandler(eventHandler);
100 eventHandlers.put(eventClass.getName(), allListenersForThisName);
101 }
102 }
103
104 }
105
106 /*** @see net.mlw.gfw.event.EventHandler#onEvent(net.mlw.gfw.event.Event)
107 */
108 public void onEvent(Event event)
109 {
110 EventHandler eventHandler = (EventHandler) eventHandlers.get(event.getClass().getName());
111 if (eventHandler != null)
112 {
113 eventHandler.onEvent(event);
114 }
115 else
116 {
117 defaultBehavour.onEvent(event);
118 }
119 }
120
121 /***
122 * @param defaultBehavour The defaultBehavour to set.
123 */
124 public void setDefaultBehavour(EventHandler defaultBehavour)
125 {
126 this.defaultBehavour = defaultBehavour;
127 }
128
129 public void setEventProcessingMode(EventProcessingMode mode)
130 {
131 Map eventHandlers = (Map) modes.get(mode);
132 if (eventHandlers == null)
133 {
134 modes.put(mode, eventHandlers = new LinkedHashMap());
135 }
136 this.eventHandlers = eventHandlers;
137 }
138
139 public static class EventProcessingMode
140 {
141 private String name;
142
143 /*** @param name
144 */
145 public EventProcessingMode(String name)
146 {
147 this.name = name;
148 }
149
150
151 /*** @see java.lang.Object#toString()
152 */
153 public String toString()
154 {
155 return name + ": " + super.toString();
156 }
157
158 }
159 /***
160 * @return Returns the defaultBehavour.
161 */
162 public EventHandler getDefaultBehavour()
163 {
164 return defaultBehavour;
165 }
166
167 }
This page was automatically generated by Maven