~ubuntu-branches/ubuntu/hardy/gnue-common/hardy

« back to all changes in this revision

Viewing changes to src/events/EventController.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2005-03-09 11:06:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050309110631-8gvvn39q7tjz1kj6
Tags: upstream-0.5.14
ImportĀ upstreamĀ versionĀ 0.5.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This file is part of GNU Enterprise.
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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.
 
18
#
 
19
# Copyright 2000-2005 Free Software Foundation
 
20
#
 
21
# FILE:
 
22
# EventController.py
 
23
#
 
24
# DESCRIPTION:
 
25
#
 
26
# NOTES:
 
27
#
 
28
 
 
29
from EventAware import EventAware
 
30
from Event import Event
 
31
 
 
32
class EventController(EventAware):
 
33
  """
 
34
  An EventController is responsible for dispatching events to 
 
35
  registered listeners.
 
36
  """
 
37
  def __init__(self):
 
38
    # Note: Don't call EventAware.__init__
 
39
    #   ... that would be fugly.
 
40
 
 
41
    # Store a dictionary of registered events
 
42
    self.__incomingEvents = {}
 
43
 
 
44
  
 
45
  def registerEventListeners(self, events):
 
46
    """
 
47
    Registers an event listener for a specific event
 
48
    
 
49
    @param events: A dictionary of {'eventName': listenerFuction} pairs
 
50
    @type events: dict
 
51
    """
 
52
    for event in events.keys():
 
53
      try:
 
54
        self.__incomingEvents[event].append(events[event])
 
55
      except KeyError:
 
56
        self.__incomingEvents[event] = [events[event]]
 
57
 
 
58
  def startCachingEvents(self):
 
59
    """
 
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.
 
63
    """
 
64
    self.__cache = []
 
65
 
 
66
  def stopCachingEvents(self):
 
67
    """
 
68
    Notifies the event controller that is should 
 
69
    start processing events again.  Any previously 
 
70
    cached events are dispatched.
 
71
    """
 
72
    cache = self.__cache
 
73
    del self.__cache
 
74
    for event, arg, parms in cache:
 
75
      self.dispatchEvent(event, *arg, **parms)
 
76
 
 
77
  def dispatchEvent(self, event, *args, **parms):
 
78
 
 
79
    # If we are caching our events, cache it:
 
80
    try:
 
81
      self.__cache.append((event, args, parms))
 
82
      return
 
83
    except AttributeError:
 
84
      pass
 
85
 
 
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.
 
90
    try:
 
91
      event.__event__
 
92
    except:
 
93
      event = Event(event, *args, **parms)
 
94
 
 
95
    handlers = []
 
96
 
 
97
    if self.__incomingEvents.has_key(event.__event__):
 
98
      handlers = self.__incomingEvents[event.__event__]
 
99
 
 
100
    if self.__incomingEvents.has_key('__before__'):
 
101
      handlers = self.__incomingEvents['__before__'] + handlers
 
102
 
 
103
    if self.__incomingEvents.has_key('__after__'):
 
104
      handlers = handlers + self.__incomingEvents['__after__']
 
105
 
 
106
    for handler in handlers:
 
107
      handler(event)
 
108
      if event.__error__ or event.__dropped__:
 
109
        break
 
110
 
 
111
    # Fire any "dispatchAfter" events
 
112
    for args, parms in event.__after__:
 
113
      self.dispatchEvent(*args, **parms)
 
114
 
 
115
    return event.__result__