1 18 package org.apache.batik.dom.events; 19 20 import org.apache.batik.dom.util.HashTable; 21 import org.w3c.dom.events.Event ; 22 import org.w3c.dom.events.EventException ; 23 import org.w3c.dom.events.EventListener ; 24 25 34 public class EventSupport { 35 36 39 protected HashTable capturingListeners; 40 41 44 protected HashTable bubblingListeners; 45 46 73 public void addEventListener(String type, EventListener listener, 74 boolean useCapture) { 75 HashTable listeners; 76 if (useCapture) { 77 if (capturingListeners == null) { 78 capturingListeners = new HashTable(); 79 } 80 listeners = capturingListeners; 81 } else { 82 if (bubblingListeners == null) { 83 bubblingListeners = new HashTable(); 84 } 85 listeners = bubblingListeners; 86 } 87 EventListenerList list = (EventListenerList) listeners.get(type); 88 if (list == null) { 89 list = new EventListenerList(); 90 listeners.put(type, list); 91 } 92 if (!list.contains(listener)) { 93 list.add(listener); 94 } 95 } 96 97 124 public void removeEventListener(String type, EventListener listener, 125 boolean useCapture) { 126 HashTable listeners; 127 if (useCapture) { 128 listeners = capturingListeners; 129 } else { 130 listeners = bubblingListeners; 131 } 132 if (listeners == null) { 133 return; 134 } 135 EventListenerList list = (EventListenerList)listeners.get(type); 136 if (list != null) { 137 list.remove(listener); 138 if (list.size() == 0) { 139 listeners.remove(type); 140 } 141 } 142 } 143 144 170 public static boolean dispatchEvent(NodeEventTarget target, Event e) 171 throws EventException { 172 AbstractEvent evt = (AbstractEvent) e; 173 if (evt == null) { 174 return false; 175 } 176 String type = evt.getType(); 177 if (type == null) { 178 throw createUnspecifiedEventTypeErr("Event type can't be null"); 179 } 180 evt.setTarget(target); 182 evt.stopPropagation(false); 183 evt.preventDefault(false); 184 NodeEventTarget [] ancestors = getAncestors(target); 186 evt.setEventPhase(Event.CAPTURING_PHASE); 188 for (int i=0; i < ancestors.length && !evt.getStopPropagation(); 189 ++i) { 190 NodeEventTarget node = ancestors[i]; 191 evt.setCurrentTarget(node); 192 fireEventListeners(node, evt, true); 193 } 194 if (!evt.getStopPropagation()) { 196 evt.setEventPhase(Event.AT_TARGET); 197 evt.setCurrentTarget(target); 198 fireEventListeners(target, evt, false); 199 } 200 if (evt.getBubbles()) { 202 evt.setEventPhase(Event.BUBBLING_PHASE); 203 for (int i=ancestors.length-1; 204 i >=0 && !evt.getStopPropagation(); --i) { 205 NodeEventTarget node = ancestors[i]; 206 evt.setCurrentTarget(node); 207 fireEventListeners(node, evt, false); 208 } 209 } 210 return !evt.getPreventDefault(); 211 } 212 213 private static void fireEventListeners(NodeEventTarget node, 214 Event evt, boolean useCapture) { 215 String type = evt.getType(); 216 EventSupport support = node.getEventSupport(); 217 if (support == null) { 219 return; 220 } 221 EventListenerList list=support.getEventListeners(type, useCapture); 222 if (list == null) { 224 return; 225 } 226 EventListener [] listeners = list.getEventListeners(); 228 if (listeners == null) { 230 return; 231 } 232 for (int i=0; i < listeners.length; ++i) { 234 try { 235 listeners[i].handleEvent(evt); 236 } catch (ThreadDeath td) { 237 throw td; 238 } catch (Throwable th) { 239 th.printStackTrace(); 240 } 241 } 242 } 243 244 private static NodeEventTarget [] getAncestors(NodeEventTarget node) { 246 node = node.getParentNodeEventTarget(); int nancestors = 0; 248 for (NodeEventTarget n = node; 249 n != null; 250 n = n.getParentNodeEventTarget(), nancestors++) {} 251 NodeEventTarget [] ancestors = new NodeEventTarget[nancestors]; 252 for (int i=nancestors-1; 253 i >= 0; 254 --i, node = node.getParentNodeEventTarget()) { 255 ancestors[i] = node; 256 } 257 return ancestors; 258 } 259 260 266 public EventListenerList getEventListeners(String type, 267 boolean useCapture) { 268 HashTable listeners=(useCapture)?capturingListeners:bubblingListeners; 269 if (listeners == null) { 270 return null; 271 } else { 272 return (EventListenerList) listeners.get(type); 273 } 274 } 275 276 282 private static EventException createEventException(short code, String s) { 283 return new EventException (code, s); 284 } 285 286 private static EventException createUnspecifiedEventTypeErr(String s) { 287 return createEventException(EventException.UNSPECIFIED_EVENT_TYPE_ERR, 288 s); 289 } 290 } 291 | Popular Tags |