Package library :: Package pyjamas :: Module EventController
[hide private]
[frames] | no frames]

Source Code for Module library.pyjamas.EventController

  1  # Module for creating event handlers 
  2   
  3  ###################### 
  4  ## EventGenerator - mixin notification support class 
  5  # 
  6  # An EventGenerator mixin keeps multiple listener lists for custom events, 
  7  # and can fire notifications for the events 
  8  # 
  9  # To use this class, simply add the mixin class to the derived class definition: 
 10  ## class myClass(somebaseclass, EventGenerator): 
 11  # The derived class can define any number of custom events with a single call: 
 12  ## self.addListenedEvent("Foo") 
 13  # which will add the usual listener management methods to the class: 
 14  ## def addFooListener(self, listener)      # add a listener 
 15  ## def removeFooListener(self, listener)   # remove a listener 
 16  # as well as a method to send a custom event notification to the listeners: 
 17  ## def dispatchFooEvent(self, parms*)   # call listeners 
 18  # Listeners use this class in the usual way: 
 19  ## fooObj.addFooListener(self)      # will call self.onFoo() if I have one 
 20  # OR 
 21  ## fooObj.addFooListener(self.myFooHandler)      # will call self.myFooHandler() 
 22  # 
 23  # R. Newpol - 2009 
 24  ######################################### 
 25   
26 -class EventGenerator():
27 - def _get_add_listener_func_name(self, ev):
28 return "add"+ev+"Listener"
29 - def _get_remove_listener_func_name(self, ev):
30 return "remove"+ev+"Listener"
31 - def _get_dispatch_func_name(self, ev):
32 return "dispatch"+ev+"Event"
33 - def addListenedEvent(self, ev):
34 # create the EventListener with event-specific lists and funcs 35 global EventListener # is also a javascript global 36 el = EventListener(ev) 37 # add the "addXXXListener() method to ourself 38 attr_add = self._get_add_listener_func_name(ev) 39 setattr(self,attr_add,el.add_listener) 40 # add the "removeXXXListener()" method to ourself 41 attr_rem = self._get_remove_listener_func_name(ev) 42 setattr(self,attr_rem,el.rem_listener) 43 # add the "dispatchXXXEvent() method to ourself 44 attr_dsp = self._get_dispatch_func_name(ev) 45 setattr(self,attr_dsp,el.dispatch)
46 47 # EventListener objects provide event-specific listener list, 48 # as well as event-specific methods to add/remove listeners to the list, 49 # and dispatch event notifications 50 # This class is not instantiated directly; it is used by the EventGenerator class
51 -class EventListener():
52 - def __init__(self, ev):
53 self.ev = ev 54 self.listeners = []
55 - def add_listener(self, listener):
56 self.listeners.append(listener)
57 - def rem_listener(self, listener):
58 self.listeners.remove(listener)
59 # listener objects should have a "onXX" event method
60 - def dispatch(self, *args):
61 ev = "on"+self.ev 62 for listener in self.listeners: 63 if hasattr(listener,ev): 64 getattr(listener,ev)(*args) 65 else: 66 listener(*args)
67 68 69 # original Handler event class (unrelated to EventGenerator)
70 -class Handler(object):
71
72 - def __init__(self, parent, event_type, *args, **kwargs):
73 if parent is None: 74 parent = self 75 self.parent = parent 76 self.event_type = event_type 77 self.listeners = {} 78 self.callback_fnname = "on%s" % event_type 79 self.extra_args = args 80 self.extra_kwargs = kwargs 81 82 # monkey-patch the parent with the callbacks 83 add_listener_fnname = "add%sListener" % event_type 84 del_listener_fnname = "remove%sListener" % event_type 85 listener = "_%sListeners" % event_type 86 on_event_name = "on%sEvent" % event_type 87 setattr(parent, listener, self) 88 setattr(parent, add_listener_fnname, self.addListener) 89 setattr(parent, del_listener_fnname, self.removeListener) 90 setattr(parent, on_event_name, self.onEvent)
91
92 - def addListener(self, listener, *args, **kwargs):
93 args = args or () 94 kwargs = kwargs or {} 95 args = self.extra_args + args 96 kwargs.update(self.extra_kwargs) 97 self.listeners[listener] = (args, kwargs)
98
99 - def removeListener(self, listener):
100 self.listeners.pop(listener)
101
102 - def onEvent(self, sender, *eventargs):
103 for listener, args in self.listeners.items(): 104 fn = getattr(listener, self.callback_fnname, listener) 105 (args, kwargs) = args 106 args = (sender,) + args + eventargs 107 fn(*args, **kwargs)
108