Package muntjac :: Package event :: Module event_router
[hide private]
[frames] | no frames]

Source Code for Module muntjac.event.event_router

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 15   
 16  """Class implementing the inheritable event listening model.""" 
 17   
 18  from muntjac.event.method_event_source import IMethodEventSource 
 19  from muntjac.event.listener_method import ListenerMethod 
 20  from muntjac.util import OrderedSet 
 21   
 22   
23 -class EventRouter(IMethodEventSource):
24 """C{EventRouter} class implementing the inheritable event 25 listening model. For more information on the event model see the 26 L{muntjac.event package documentation<muntjac.event>}. 27 28 @author: Vaadin Ltd. 29 @author: Richard Lincoln 30 @version: 1.1.2 31 """ 32
33 - def __init__(self):
34 #: List of registered listeners. 35 self._listenerList = None
36 37
38 - def addListener(self, eventType, obj, method, arguments=None, 39 eventArgIdx=None):
40 # Registers a new listener with the specified activation method 41 # to listen events generated by this component. 42 43 if self._listenerList is None: 44 self._listenerList = OrderedSet() 45 46 lm = ListenerMethod(eventType, obj, method, arguments, eventArgIdx) 47 48 self._listenerList.add(lm)
49 50
51 - def removeListener(self, eventType, target, method=None):
52 # Removes the event listener methods matching 53 # the given given paramaters. 54 if method is None: 55 if self._listenerList is not None: 56 for lm in self._listenerList: 57 if lm.matches(eventType, target): 58 self._listenerList.remove(lm) 59 return 60 else: 61 if isinstance(method, basestring): 62 methodName = method 63 64 method = getattr(target, methodName) 65 66 if method is None: 67 raise ValueError() 68 69 # Remove the listeners 70 if self._listenerList is not None: 71 for lm in self._listenerList: 72 if lm.matches(eventType, target, method): 73 self._listenerList.remove(lm) 74 return 75 else: 76 if self._listenerList is not None: 77 for lm in self._listenerList: 78 if lm.matches(eventType, target, method): 79 self._listenerList.remove(lm) 80 return
81 82
83 - def removeAllListeners(self):
84 """Removes all listeners from event router.""" 85 self._listenerList = None
86 87
88 - def fireEvent(self, event):
89 """Sends an event to all registered listeners. The listeners will 90 decide if the activation method should be called or not. 91 92 @param event: 93 the Event to be sent to all listeners. 94 """ 95 # It is not necessary to send any events if there are no listeners 96 if self._listenerList is not None: 97 # Make a copy of the listener list to allow listeners to be added 98 # inside listener methods. Fixes #3605. 99 100 # Send the event to all listeners. The listeners themselves 101 # will filter out unwanted events. 102 listeners = list(self._listenerList) 103 for listener in listeners: 104 listener.receiveEvent(event)
105 106
107 - def hasListeners(self, eventType):
108 """Checks if the given Event type is listened by a listener registered 109 to this router. 110 111 @param eventType: 112 the event type to be checked 113 @return: true if a listener is registered for the given event type 114 """ 115 if self._listenerList is not None: 116 for lm in self._listenerList: 117 if lm.isType(eventType): 118 return True 119 return False
120 121
122 - def getListeners(self, eventType):
123 """Returns all listeners that match or extend the given event type. 124 125 @param eventType: 126 The type of event to return listeners for. 127 @return: A collection with all registered listeners. Empty if no 128 listeners are found. 129 """ 130 listeners = list() 131 if self._listenerList is not None: 132 for lm in self._listenerList: 133 if lm.isOrExtendsType(eventType): 134 listeners.append(lm.getTarget()) 135 return listeners
136