| 1 |
""" |
|---|
| 2 |
ClassItem diagram item |
|---|
| 3 |
""" |
|---|
| 4 |
|
|---|
| 5 |
from gaphas.state import observed, reversible_property |
|---|
| 6 |
|
|---|
| 7 |
from gaphor import UML |
|---|
| 8 |
from gaphor.i18n import _ |
|---|
| 9 |
|
|---|
| 10 |
from gaphor.diagram.classifier import ClassifierItem |
|---|
| 11 |
from feature import AttributeItem, OperationItem |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
class ClassItem(ClassifierItem): |
|---|
| 15 |
""" |
|---|
| 16 |
This item visualizes a Class instance. |
|---|
| 17 |
|
|---|
| 18 |
A ClassItem contains two compartments (Compartment): one for |
|---|
| 19 |
attributes and one for operations. To add and remove such features |
|---|
| 20 |
the ClassItem implements the CanvasGroupable interface. |
|---|
| 21 |
Items can be added by callling class.add() and class.remove(). |
|---|
| 22 |
This is used to handle CanvasItems, not UML objects! |
|---|
| 23 |
""" |
|---|
| 24 |
|
|---|
| 25 |
__uml__ = UML.Class, UML.Stereotype |
|---|
| 26 |
__stereotype__ = { |
|---|
| 27 |
'stereotype': UML.Stereotype, |
|---|
| 28 |
'metaclass': lambda self: (not isinstance(self.subject, UML.Stereotype)) and hasattr(self.subject, 'extension') and self.subject.extension, |
|---|
| 29 |
} |
|---|
| 30 |
|
|---|
| 31 |
def __init__(self, id=None): |
|---|
| 32 |
ClassifierItem.__init__(self, id) |
|---|
| 33 |
self.drawing_style = self.DRAW_COMPARTMENT |
|---|
| 34 |
self._attributes = self.create_compartment('attributes') |
|---|
| 35 |
self._operations = self.create_compartment('operations') |
|---|
| 36 |
|
|---|
| 37 |
self.add_watch(UML.Class.ownedAttribute, self.on_class_owned_attribute) |
|---|
| 38 |
self.add_watch(UML.Class.ownedOperation, self.on_class_owned_operation) |
|---|
| 39 |
self.add_watch(UML.Property.association, self.on_class_owned_attribute) |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
def save(self, save_func): |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
self.save_property(save_func, 'show-attributes') |
|---|
| 47 |
self.save_property(save_func, 'show-operations') |
|---|
| 48 |
ClassifierItem.save(self, save_func) |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
def postload(self): |
|---|
| 52 |
ClassifierItem.postload(self) |
|---|
| 53 |
self.sync_attributes() |
|---|
| 54 |
self.sync_operations() |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
@observed |
|---|
| 58 |
def _set_show_operations(self, value): |
|---|
| 59 |
self._operations.visible = value |
|---|
| 60 |
|
|---|
| 61 |
show_operations = reversible_property(fget=lambda s: s._operations.visible, |
|---|
| 62 |
fset=_set_show_operations) |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
@observed |
|---|
| 66 |
def _set_show_attributes(self, value): |
|---|
| 67 |
self._attributes.visible = value |
|---|
| 68 |
|
|---|
| 69 |
show_attributes = reversible_property(fget=lambda s: s._attributes.visible, |
|---|
| 70 |
fset=_set_show_attributes) |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
def _create_attribute(self, attribute): |
|---|
| 74 |
""" |
|---|
| 75 |
Create a new attribute item. |
|---|
| 76 |
""" |
|---|
| 77 |
new = AttributeItem() |
|---|
| 78 |
new.subject = attribute |
|---|
| 79 |
self._attributes.append(new) |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
def _create_operation(self, operation): |
|---|
| 83 |
""" |
|---|
| 84 |
Create a new operation item. |
|---|
| 85 |
""" |
|---|
| 86 |
new = OperationItem() |
|---|
| 87 |
new.subject = operation |
|---|
| 88 |
self._operations.append(new) |
|---|
| 89 |
|
|---|
| 90 |
def unregister_handlers(self): |
|---|
| 91 |
super(ClassItem, self).unregister_handlers() |
|---|
| 92 |
for a in self._attributes: |
|---|
| 93 |
a.unregister_handlers() |
|---|
| 94 |
for o in self._operations: |
|---|
| 95 |
o.unregister_handlers() |
|---|
| 96 |
|
|---|
| 97 |
def sync_attributes(self): |
|---|
| 98 |
""" |
|---|
| 99 |
Sync the contents of the attributes compartment with the data |
|---|
| 100 |
in self.subject. |
|---|
| 101 |
""" |
|---|
| 102 |
owned_attributes = [a for a in self.subject.ownedAttribute if not a.association] |
|---|
| 103 |
|
|---|
| 104 |
self.sync_uml_elements(owned_attributes, self._attributes, |
|---|
| 105 |
self._create_attribute) |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
def sync_operations(self): |
|---|
| 109 |
""" |
|---|
| 110 |
Sync the contents of the operations compartment with the data |
|---|
| 111 |
in self.subject. |
|---|
| 112 |
""" |
|---|
| 113 |
self.sync_uml_elements(self.subject.ownedOperation, self._operations, |
|---|
| 114 |
self._create_operation) |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
def on_class_owned_attribute(self, event): |
|---|
| 118 |
if self.subject: |
|---|
| 119 |
self.sync_attributes() |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
def on_class_owned_operation(self, event): |
|---|
| 123 |
if self.subject: |
|---|
| 124 |
self.sync_operations() |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|