root/gaphor/tags/gaphor-0.12.0/gaphor/adapters/grouping.py

Revision 1559, 2.5 kB (checked in by arj..@yirdis.nl, 2 years ago)

fixed bug in storage module. Make constraints connect properly when loading a model.

Line 
1 from zope import interface, component
2
3 from gaphor.diagram import items
4 from gaphor.diagram.interfaces import IGroup
5
6
7 ###class AdapterMetaclass(type):
8 ###    """
9 ###    Adapter metaclass.
10 ###    """
11 ###    def __new__(self, name, bases, data):
12 ###        """
13 ###        Perform class interface adaptation.
14 ###        """
15 ###
16 ###        import sys
17 ###        frame = sys._getframe(1)
18 ###        locals = frame.f_locals
19 ###
20 ###        cls = type.__new__(self, name, bases, data)
21 ###        if '__adapts__' in data:
22 ###            a, b = data['__adapts__']
23 ###            cls.adapts(a, b)
24 ###            component.provideAdapter(cls)
25 ###
26 ###        return cls
27 ###
28 ###
29 ###    def adapts(self, a, b):
30 ###        """
31 ###        Stolen from zope as component.adapts prevents adapting on metaclass
32 ###        level.
33 ###        """
34 ###        import sys
35 ###        frame = sys._getframe(1)
36 ###        locals = frame.f_locals
37 ###        locals['__component_adapts__'] = component._adapts_descr(a, b)
38
39
40 class AbstractGroup(object):
41     """
42     Base class for grouping of UML objects.
43
44     Attributes:
45      - parent: parent item
46      - item:   item to be part of parent item
47     """
48
49 ###    __metaclass__ = AdapterMetaclass
50
51     interface.implements(IGroup)
52
53     def __init__(self, parent, item):
54         self.parent = parent
55         self.item = item
56
57     def can_contain(self):
58         raise NotImplemented, 'This is abstract class'
59
60
61     def group(self):
62         raise NotImplemented, 'This is abstract class'
63
64
65
66 from gaphor.diagram import DiagramItemMeta
67 class InteractionLifelineGroup(AbstractGroup):
68     """
69     Add lifeline to interaction.
70     """
71
72 ###    __adapts__ = items.InteractionItem, items.LifelineItem
73     def pre_can_contain(self):
74         return isinstance(self.parent, items.InteractionItem) \
75                 and issubclass(self.item, items.LifelineItem)
76
77     def can_contain(self):
78         return isinstance(self.parent, items.InteractionItem) \
79                 and isinstance(self.item, items.LifelineItem)
80
81     def group(self):
82         self.parent.subject.lifeline = self.item.subject
83         self.parent.canvas.reparent(self.item, self.parent)
84         #self.parent.add_item(self.item)
85
86
87     def ungroup(self):
88         del self.parent.subject.lifeline[self.item.subject]
89
90
91 component.provideAdapter(factory=InteractionLifelineGroup,
92         adapts=(items.InteractionItem, DiagramItemMeta))
93 component.provideAdapter(factory=InteractionLifelineGroup,
94         adapts=(items.InteractionItem, items.LifelineItem))
95
96
97 class ComponentClassGroup(AbstractGroup):
98     """
99     Add class to component.
100     """
Note: See TracBrowser for help on using the browser.