| 1 |
''' |
|---|
| 2 |
Generalization -- |
|---|
| 3 |
''' |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
import gobject |
|---|
| 7 |
import diacanvas |
|---|
| 8 |
import gaphor |
|---|
| 9 |
import gaphor.UML as UML |
|---|
| 10 |
from gaphor.diagram import initialize_item |
|---|
| 11 |
import relationship |
|---|
| 12 |
|
|---|
| 13 |
class GeneralizationItem(relationship.RelationshipItem): |
|---|
| 14 |
|
|---|
| 15 |
def __init__(self, id=None): |
|---|
| 16 |
relationship.RelationshipItem.__init__(self, id) |
|---|
| 17 |
self.set(has_head=1, head_fill_color=0, |
|---|
| 18 |
head_a=15.0, head_b=15.0, head_c=10.0, head_d=10.0) |
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
def find_relationship(self, head_subject, tail_subject): |
|---|
| 23 |
"""See RelationshipItem.find_relationship(). |
|---|
| 24 |
""" |
|---|
| 25 |
if self.subject and \ |
|---|
| 26 |
self.subject.general is head_subject and \ |
|---|
| 27 |
self.subject.specific is tail_subject: |
|---|
| 28 |
return self.subject |
|---|
| 29 |
|
|---|
| 30 |
for gen in tail_subject.generalization: |
|---|
| 31 |
if gen.general is head_subject: |
|---|
| 32 |
|
|---|
| 33 |
for item in spec.subject.presentation: |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
if item.canvas is self.canvas and item is not self: |
|---|
| 37 |
break |
|---|
| 38 |
else: |
|---|
| 39 |
return spec |
|---|
| 40 |
return None |
|---|
| 41 |
|
|---|
| 42 |
def allow_connect_handle(self, handle, connecting_to): |
|---|
| 43 |
"""See RelationshipItem.allow_connect_handle(). |
|---|
| 44 |
""" |
|---|
| 45 |
try: |
|---|
| 46 |
if not isinstance(connecting_to.subject, UML.Classifier): |
|---|
| 47 |
return False |
|---|
| 48 |
|
|---|
| 49 |
c1 = self.handles[0].connected_to |
|---|
| 50 |
c2 = self.handles[-1].connected_to |
|---|
| 51 |
if not c1 and not c2: |
|---|
| 52 |
return True |
|---|
| 53 |
if self.handles[0] is handle: |
|---|
| 54 |
return (self.handles[-1].connected_to.subject is not connecting_to.subject) |
|---|
| 55 |
elif self.handles[-1] is handle: |
|---|
| 56 |
return (self.handles[0].connected_to.subject is not connecting_to.subject) |
|---|
| 57 |
assert 1, 'Should never be reached...' |
|---|
| 58 |
except AttributeError, e: |
|---|
| 59 |
log.debug('Generalization.allow_connect_handle: %s' % e) |
|---|
| 60 |
return False |
|---|
| 61 |
|
|---|
| 62 |
def confirm_connect_handle (self, handle): |
|---|
| 63 |
"""See RelationshipItem.confirm_connect_handle(). |
|---|
| 64 |
""" |
|---|
| 65 |
|
|---|
| 66 |
c1 = self.handles[0].connected_to |
|---|
| 67 |
c2 = self.handles[-1].connected_to |
|---|
| 68 |
if c1 and c2: |
|---|
| 69 |
s1 = c1.subject |
|---|
| 70 |
s2 = c2.subject |
|---|
| 71 |
relation = self.find_relationship(s1, s2) |
|---|
| 72 |
if not relation: |
|---|
| 73 |
relation = gaphor.resource(UML.ElementFactory).create(UML.Generalization) |
|---|
| 74 |
relation.general = s1 |
|---|
| 75 |
relation.specific = s2 |
|---|
| 76 |
self.subject = relation |
|---|
| 77 |
|
|---|
| 78 |
def confirm_disconnect_handle (self, handle, was_connected_to): |
|---|
| 79 |
"""See RelationshipItem.confirm_disconnect_handle(). |
|---|
| 80 |
""" |
|---|
| 81 |
|
|---|
| 82 |
if self.subject: |
|---|
| 83 |
del self.subject |
|---|
| 84 |
|
|---|
| 85 |
initialize_item(GeneralizationItem, UML.Generalization) |
|---|