| 1 |
############################################################################## |
|---|
| 2 |
# |
|---|
| 3 |
# Copyright (c) 2005-2006 Nuxeo and Contributors. |
|---|
| 4 |
# All Rights Reserved. |
|---|
| 5 |
# |
|---|
| 6 |
# This software is subject to the provisions of the Zope Public License, |
|---|
| 7 |
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|---|
| 8 |
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|---|
| 9 |
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 10 |
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|---|
| 11 |
# FOR A PARTICULAR PURPOSE. |
|---|
| 12 |
# |
|---|
| 13 |
############################################################################## |
|---|
| 14 |
""" |
|---|
| 15 |
|
|---|
| 16 |
$Id$ |
|---|
| 17 |
""" |
|---|
| 18 |
__docformat__ = "reStructuredText" |
|---|
| 19 |
|
|---|
| 20 |
from zope.app.copypastemove import dispatchToSublocations |
|---|
| 21 |
from zope.interface import implements |
|---|
| 22 |
|
|---|
| 23 |
from cpsskins.controllers.interfaces import IController |
|---|
| 24 |
from cpsskins.utils import getThemeManager |
|---|
| 25 |
|
|---|
| 26 |
class Controller(object): |
|---|
| 27 |
"""Base controller with a set of handlers for the most common events. |
|---|
| 28 |
|
|---|
| 29 |
Element controllers are element adapters that provide IController. |
|---|
| 30 |
|
|---|
| 31 |
When a monitored event occurs on an element, the event is dispatched |
|---|
| 32 |
to the registered controller. |
|---|
| 33 |
|
|---|
| 34 |
Controllers should subclass this base class since the list of events may |
|---|
| 35 |
be extended in the future. |
|---|
| 36 |
""" |
|---|
| 37 |
implements(IController) |
|---|
| 38 |
|
|---|
| 39 |
def __init__(self, element): |
|---|
| 40 |
self.element = element |
|---|
| 41 |
|
|---|
| 42 |
def created(self): |
|---|
| 43 |
"""What to do when an element has been created.""" |
|---|
| 44 |
|
|---|
| 45 |
def added(self): |
|---|
| 46 |
"""What to do when an element has been added to a container.""" |
|---|
| 47 |
|
|---|
| 48 |
def removed(self): |
|---|
| 49 |
"""What to do when an element has been removed from a container.""" |
|---|
| 50 |
|
|---|
| 51 |
def modified(self): |
|---|
| 52 |
"""What to do when an element has been modified.""" |
|---|
| 53 |
|
|---|
| 54 |
# Event handlers: events get dispatched to the controllers. |
|---|
| 55 |
|
|---|
| 56 |
def elementCreated(element, event): |
|---|
| 57 |
"""This suscriber is called when an element has been created. |
|---|
| 58 |
""" |
|---|
| 59 |
controller = IController(element, None) |
|---|
| 60 |
if controller is not None: |
|---|
| 61 |
controller.created() |
|---|
| 62 |
|
|---|
| 63 |
def elementAdded(element, event): |
|---|
| 64 |
"""This suscriber is called when an element has been added to a container. |
|---|
| 65 |
""" |
|---|
| 66 |
# register the element |
|---|
| 67 |
getThemeManager().registerElement(element) |
|---|
| 68 |
|
|---|
| 69 |
# get the object's controller |
|---|
| 70 |
controller = IController(element, None) |
|---|
| 71 |
if controller is not None: |
|---|
| 72 |
controller.added() |
|---|
| 73 |
|
|---|
| 74 |
# propagate the event to sublocations |
|---|
| 75 |
dispatchToSublocations(element, event) |
|---|
| 76 |
|
|---|
| 77 |
def elementRemoved(element, event): |
|---|
| 78 |
"""This suscriber is called when an element has been removed from a |
|---|
| 79 |
container. |
|---|
| 80 |
""" |
|---|
| 81 |
tmutil = getThemeManager() |
|---|
| 82 |
# the element may be removed from a folder above the theme manager |
|---|
| 83 |
if tmutil is None: |
|---|
| 84 |
return |
|---|
| 85 |
|
|---|
| 86 |
# get the object's controller |
|---|
| 87 |
controller = IController(element, None) |
|---|
| 88 |
if controller is not None: |
|---|
| 89 |
controller.removed() |
|---|
| 90 |
|
|---|
| 91 |
# unregister the element |
|---|
| 92 |
tmutil.unregisterElement(element) |
|---|
| 93 |
|
|---|
| 94 |
def elementModified(element, event): |
|---|
| 95 |
"""This suscriber is called when an element has been modified. |
|---|
| 96 |
""" |
|---|
| 97 |
# get the object's controller |
|---|
| 98 |
controller = IController(element, None) |
|---|
| 99 |
if controller is not None: |
|---|
| 100 |
controller.modified() |
|---|
| 101 |
|
|---|