1 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
2 |
# Authors: |
3 |
# Jan-Oliver Wagner <[email protected]> |
4 |
# |
5 |
# This program is free software under the GPL (>=v2) |
6 |
# Read the file COPYING coming with Thuban for details. |
7 |
|
8 |
__version__ = "$Revision$" |
9 |
|
10 |
from Thuban import _ |
11 |
|
12 |
from messages import EXTENSION_CHANGED, EXTENSION_OBJECTS_CHANGED |
13 |
|
14 |
from base import TitledObject, Modifiable |
15 |
|
16 |
|
17 |
class Extension(TitledObject, Modifiable): |
18 |
|
19 |
"""Represent a extension. A extension is a package of additional |
20 |
functionality to Thuban and contains a number of Objects |
21 |
that can be freely defined in an extension. |
22 |
Each object must a a TitleObject with additional set "object.name". |
23 |
Furthermore, each object must implement the methods Subscribe, |
24 |
Unsubscribe and Destroy (i.e. derive from Modifiable). |
25 |
|
26 |
Extension objects send the following message types: |
27 |
|
28 |
EXTENSION_CHANGED -- Something in the extension has changed. |
29 |
|
30 |
EXTENSION_OBJECTS_CHANGED -- Something in the objects has changed. |
31 |
|
32 |
""" |
33 |
|
34 |
forwarded_channels = (EXTENSION_CHANGED, EXTENSION_OBJECTS_CHANGED) |
35 |
|
36 |
def __init__(self, title): |
37 |
"""Initialize the extension.""" |
38 |
TitledObject.__init__(self, title) |
39 |
self.objects = [] |
40 |
|
41 |
def Destroy(self): |
42 |
for object in self.objects: |
43 |
object.Destroy() |
44 |
Modifiable.Destroy(self) |
45 |
|
46 |
def AddObject(self, object): |
47 |
self.objects.append(object) |
48 |
for channel in self.forwarded_channels: |
49 |
object.Subscribe(channel, self.forward, channel) |
50 |
self.changed(EXTENSION_OBJECTS_CHANGED, self) |
51 |
|
52 |
def RemoveObject(self, object): |
53 |
for channel in self.forwarded_channels: |
54 |
object.Unsubscribe(channel, self.forward, channel) |
55 |
self.layers.remove(layer) |
56 |
self.changed(EXTENSION_OBJECTS_CHANGED, self) |
57 |
object.Destroy() |
58 |
|
59 |
def Objects(self): |
60 |
return self.objects |
61 |
|
62 |
def HasObjects(self): |
63 |
return len(self.objects) > 0 |
64 |
|
65 |
def FindObject(self, title): |
66 |
"""Find an object by title. If found, return it, else return None.""" |
67 |
for object in self.objects: |
68 |
if object.title == title: |
69 |
return object |
70 |
return None |
71 |
|
72 |
def forward(self, *args): |
73 |
"""Reissue events""" |
74 |
if len(args) > 1: |
75 |
args = (args[-1],) + args[:-1] |
76 |
apply(self.issue, args) |
77 |
|
78 |
def WasModified(self): |
79 |
"""Return true if something of the extension was modified""" |
80 |
if self.modified: |
81 |
return 1 |
82 |
else: |
83 |
return 0 |
84 |
|
85 |
def UnsetModified(self): |
86 |
"""Unset the modified flag of the extension""" |
87 |
Modifiable.UnsetModified(self) |
88 |
|
89 |
def TreeInfo(self): |
90 |
return (_("Extension: %s") % self.title, |
91 |
["%s: %s" % (object.title, object.name) |
92 |
for object in self.objects]) |