1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
package org.eclipse.papyrus.uml.diagram.sequence.edit.policies;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.papyrus.uml.diagram.common.editpolicies.AbstractMaskManagedEditPolicy;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.LifelineEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.LifelineNameEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.util.LifelineLabelHelper;
import org.eclipse.uml2.uml.ConnectableElement;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.ValueSpecification;
public class LifelineLabelEditPolicy extends AbstractMaskManagedEditPolicy {
@Override
public void addAdditionalListeners() {
super.addAdditionalListeners();
Lifeline lifeline = getUMLElement();
// check host semantic element is not null
if (lifeline == null) {
return;
}
// adds a listener to the element itself, and to linked elements, like Type
getDiagramEventBroker().addNotificationListener(lifeline, this);
ConnectableElement ce = lifeline.getRepresents();
if (ce != null) {
getDiagramEventBroker().addNotificationListener(ce, this);
if (ce.getType() != null) {
getDiagramEventBroker().addNotificationListener(ce.getType(), this);
}
}
ValueSpecification selector = lifeline.getSelector();
if (selector != null) {
getDiagramEventBroker().addNotificationListener(selector, this);
}
}
@Override
protected void removeAdditionalListeners() {
super.removeAdditionalListeners();
Lifeline lifeline = getUMLElement();
// check host semantic element is not null
if (lifeline == null) {
return;
}
getDiagramEventBroker().removeNotificationListener(lifeline, this);
ConnectableElement ce = lifeline.getRepresents();
if (ce != null) {
getDiagramEventBroker().removeNotificationListener(ce, this);
if (ce.getType() != null) {
getDiagramEventBroker().removeNotificationListener(ce.getType(), this);
}
}
ValueSpecification selector = lifeline.getSelector();
if (selector != null) {
getDiagramEventBroker().removeNotificationListener(selector, this);
}
}
@Override
public void notifyChanged(Notification notification) {
super.notifyChanged(notification);
Object object = notification.getNotifier();
if (object == null || getUMLElement() == null) {
return;
}
if (notification.getFeature() != null) {
if (notification.getFeature().equals(UMLPackage.eINSTANCE.getNamedElement_Name())) {
refreshDisplay();
} else if (notification.getFeature().equals(UMLPackage.Literals.LIFELINE__REPRESENTS)) {
// change represent element
if (notification.getNewValue() instanceof ConnectableElement) {
ConnectableElement ce = (ConnectableElement) notification.getNewValue();
getDiagramEventBroker().addNotificationListener(ce, this);
if (ce.getType() != null) {
getDiagramEventBroker().addNotificationListener(ce.getType(), this);
}
}
if (notification.getOldValue() instanceof ConnectableElement) {
ConnectableElement ce = (ConnectableElement) notification.getOldValue();
getDiagramEventBroker().removeNotificationListener(ce, this);
if (ce.getType() != null) {
getDiagramEventBroker().removeNotificationListener(ce.getType(), this);
}
}
refreshDisplay();
} else if (isMaskManagedAnnotation(object) || isRemovedMaskManagedLabelAnnotation(object, notification)) {
refreshDisplay();
} else if (object.equals(getUMLElement().getRepresents())) {
// change represent type
if (notification.getNewValue() instanceof Type && notification.getNewValue() instanceof EObject) {
getDiagramEventBroker().addNotificationListener((EObject) notification.getNewValue(), this);
}
if (notification.getOldValue() instanceof Type && notification.getOldValue() instanceof EObject) {
getDiagramEventBroker().removeNotificationListener((EObject) notification.getOldValue(), this);
}
refreshDisplay();
} else if (object.equals(getUMLElement().getSelector())
|| notification.getFeature().equals(UMLPackage.Literals.LIFELINE__SELECTOR)) {
// change selector
if (notification.getNewValue() instanceof ValueSpecification) {
getDiagramEventBroker().addNotificationListener((ValueSpecification) notification.getNewValue(), this);
}
if (notification.getOldValue() instanceof ValueSpecification) {
getDiagramEventBroker().removeNotificationListener((ValueSpecification) notification.getOldValue(), this);
}
refreshDisplay();
}
}
}
@Override
public void refreshDisplay() {
// calls the helper for this edit Part
LifelineEditPart lp = (LifelineEditPart) getHost();
List children = lp.getChildren();
for (Object p : children) {
if (p instanceof LifelineNameEditPart) {
LifelineLabelHelper.getInstance().refreshEditPartDisplay((GraphicalEditPart) p);
}
}
}
@Override
public Collection<String> getDefaultDisplayValue() {
return LifelineLabelHelper.DEFAULT_LABEL_DISPLAY;
}
/**
* {@inheritDoc}
*/
@Override
public Map<String, String> getMasks() {
return LifelineLabelHelper.getInstance().getMasks();
}
/**
* {@inheritDoc}
*/
@Override
public Lifeline getUMLElement() {
return (Lifeline) hostSemanticElement;
}
}
|