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.Iterator; 24 import java.util.LinkedHashMap; 25 import java.util.Map; 26 import java.util.Set; 27 28 /*** A Map of EventHandler(s). This can be used to store many 29 * EventHandler(s), yet have the ability to pull one out by 30 * name. 31 * 32 * @author Matthew L. Wilson 33 * @version $Revision: 1.4 $ $Date: 2004/06/29 20:32:57 $ 34 */ 35 public class EventHandlerMap implements EventHandler 36 { 37 /*** Holds all the publisher. */ 38 private Map eventHandlers = new LinkedHashMap(); 39 40 /*** Removes the EventHandler from the Map. 41 * 42 * @param name The name of the EventHandler. 43 * @param eventHandler The EventHandler. 44 */ 45 public void removeEventHandler(String name, EventHandler eventHandler) 46 { 47 EventHandler eventHandlerInMap = (EventHandler) eventHandlers.get(name); 48 //If there are no EventHandler(s) for this name. 49 if (eventHandlerInMap == null) 50 { 51 throw new RuntimeException("Handler not found!"); 52 } 53 54 if (eventHandlerInMap instanceof EventHandlerList) 55 { 56 ((EventHandlerList) eventHandlerInMap).removeEventHandler(eventHandler); 57 } 58 else 59 { 60 eventHandlers.remove(name); 61 } 62 } 63 64 /*** Adds a EventHandler to the internal List of EventHandlers. 65 * 66 * @param eventHandler The EventHandler to add. 67 */ 68 public void addEventHandler(String name, EventHandler eventHandler) 69 { 70 EventHandler eventHandlerInMap = (EventHandler) eventHandlers.get(name); 71 //If there are no EventHandler(s) for this name. 72 if (eventHandlerInMap == null) 73 { 74 eventHandlers.put(name, eventHandler); 75 } 76 else 77 { 78 //If there are many EventHandler(s) for this name, in a EventHandlerList. 79 if (eventHandlerInMap instanceof EventHandlerList) 80 { 81 ((EventHandlerList) eventHandlerInMap).addEventHandler(eventHandler); 82 } 83 //There is a plaing old EventHandler, so take the existing on and the new 84 //ne and place it in a new EventHandlerList. 85 else 86 { 87 EventHandlerList allListenersForThisName = new EventHandlerList(); 88 allListenersForThisName.addEventHandler(allListenersForThisName); 89 allListenersForThisName.addEventHandler(eventHandler); 90 eventHandlers.put(name, allListenersForThisName); 91 } 92 } 93 94 } 95 96 /*** 97 * 98 * @param name 99 * @return 100 */ 101 public EventHandler getEventHandler(String name) 102 { 103 return (EventHandler) eventHandlers.get(name); 104 } 105 106 /*** @see net.mlw.gfw.event.EventHandler#onEvent(net.mlw.gfw.event.Event) 107 */ 108 public void onEvent(String name, Event event) 109 { 110 EventHandler eventHandler = (EventHandler) eventHandlers.get(name); 111 if (eventHandler != null) 112 { 113 eventHandler.onEvent(event); 114 } 115 } 116 117 /*** @see net.mlw.gfw.event.EventHandler#onEvent(net.mlw.gfw.event.Event) 118 */ 119 public void onEvent(Event event) 120 { 121 for (Iterator iter = eventHandlers.values().iterator(); iter.hasNext();) 122 { 123 ((EventHandler) iter.next()).onEvent(event); 124 } 125 } 126 127 /*** Gets all the names of the EventHandler(s). 128 * 129 * @return All the names of the EventHandler(s). 130 */ 131 public Set getKeys() 132 { 133 return eventHandlers.keySet(); 134 } 135 136 }

This page was automatically generated by Maven