2
# This file is part of GNU Enterprise.
4
# GNU Enterprise is free software; you can redistribute it
5
# and/or modify it under the terms of the GNU General Public
6
# License as published by the Free Software Foundation; either
7
# version 2, or (at your option) any later version.
9
# GNU Enterprise is distributed in the hope that it will be
10
# useful, but WITHOUT ANY WARRANTY; without even the implied
11
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
# PURPOSE. See the GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public
15
# License along with program; see the file COPYING. If not,
16
# write to the Free Software Foundation, Inc., 59 Temple Place
17
# - Suite 330, Boston, MA 02111-1307, USA.
19
# Copyright 2000-2005 Free Software Foundation
29
from EventAware import EventAware
30
from Event import Event
32
class EventController(EventAware):
34
An EventController is responsible for dispatching events to
38
# Note: Don't call EventAware.__init__
39
# ... that would be fugly.
41
# Store a dictionary of registered events
42
self.__incomingEvents = {}
45
def registerEventListeners(self, events):
47
Registers an event listener for a specific event
49
@param events: A dictionary of {'eventName': listenerFuction} pairs
52
for event in events.keys():
54
self.__incomingEvents[event].append(events[event])
56
self.__incomingEvents[event] = [events[event]]
58
def startCachingEvents(self):
60
Causes the event controller to start storing events
61
rather than dispatching them. It will continue to
62
store events until L{stopCachingEvents} is called.
66
def stopCachingEvents(self):
68
Notifies the event controller that is should
69
start processing events again. Any previously
70
cached events are dispatched.
74
for event, arg, parms in cache:
75
self.dispatchEvent(event, *arg, **parms)
77
def dispatchEvent(self, event, *args, **parms):
79
# If we are caching our events, cache it:
81
self.__cache.append((event, args, parms))
83
except AttributeError:
86
# Hackery so dispatchEvent can be passed
87
# either an Event() object, or a text string
88
# identifying the type of event. If the
89
# latter, an event is created on the fly.
93
event = Event(event, *args, **parms)
97
if self.__incomingEvents.has_key(event.__event__):
98
handlers = self.__incomingEvents[event.__event__]
100
if self.__incomingEvents.has_key('__before__'):
101
handlers = self.__incomingEvents['__before__'] + handlers
103
if self.__incomingEvents.has_key('__after__'):
104
handlers = handlers + self.__incomingEvents['__after__']
106
for handler in handlers:
108
if event.__error__ or event.__dropped__:
111
# Fire any "dispatchAfter" events
112
for args, parms in event.__after__:
113
self.dispatchEvent(*args, **parms)
115
return event.__result__