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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
/*****************************************************************************
* Copyright (c) 2008 CEA LIST.
*
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
* Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.fr - Bug 393532
*
*****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.editparts;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.gmfdiag.common.editpart.ConnectionEditPart;
import org.eclipse.papyrus.uml.appearance.helper.AppliedStereotypeHelper;
import org.eclipse.papyrus.uml.appearance.helper.UMLVisualInformationPapyrusConstant;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.editpolicies.ApplyStereotypeEditPolicy;
import org.eclipse.papyrus.uml.diagram.common.editpolicies.ShowHideLabelEditPolicy;
import org.eclipse.papyrus.uml.diagram.common.figure.edge.UMLEdgeFigure;
import org.eclipse.papyrus.uml.diagram.common.service.ApplyStereotypeRequest;
import org.eclipse.papyrus.uml.diagram.common.stereotype.StereotypeDisplayHelper;
import org.eclipse.papyrus.uml.tools.listeners.StereotypeElementListener.StereotypeExtensionNotification;
import org.eclipse.swt.graphics.Image;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Stereotype;
/**
* Abstract edit part for all connection nodes that control UML elements
* represented by edges. It then defines basic behavior for Stereotype
* management. It has facilities to retrieve UML element controlled by this edit
* part
*/
public abstract class UMLConnectionNodeEditPart extends ConnectionEditPart implements IUMLEditPart, IEditpartListenerAccess {
/**
* Creates a new UMLConnectionNodeEditPart
*
* @param view
* owned view by this edit part
*/
public UMLConnectionNodeEditPart(View view) {
super(view);
}
/**
* {@inheritDoc}
*/
@Override
protected void addSemanticListeners() {
super.addSemanticListeners();
// retrieve element
final Element element = getUMLElement();
if (element == null) {
return;
}
// add listener to react to the application and remove of a stereotype
addListenerFilter(STEREOTYPABLE_ELEMENT, this, resolveSemanticElement());
// add a lister to each already applied stereotyped
for (EObject stereotypeApplication : element.getStereotypeApplications()) {
addListenerFilter(STEREOTYPED_ELEMENT, this, stereotypeApplication);
}
}
@Override
public final Iterator getEventListenerIterator(Class clazz) {
return getEventListeners(clazz);
}
/**
* {@inheritDoc}
*/
@Override
protected void handleNotificationEvent(Notification event) {
super.handleNotificationEvent(event);
// NOTA: should check here which element has to be refreshed
// check if this concerns a stereotype application or unapplication
final int eventType = event.getEventType();
if (eventType == StereotypeExtensionNotification.STEREOTYPE_APPLIED_TO_ELEMENT) {
// a stereotype was applied to the notifier
// then a new listener should be added to the stereotype application
addListenerFilter(STEREOTYPED_ELEMENT, this, (EObject) event.getNewValue());
}
// // refresh the figure if stereotypes have changed
// if (resolveSemanticElement() != null) {
// refreshAppliedStereotypes();
// }
}
/**
* {@inheritDoc}
*/
@Override
protected void createDefaultEditPolicies() {
super.createDefaultEditPolicies();
// adds the stereotype application edit policy
installEditPolicy(ApplyStereotypeRequest.APPLY_STEREOTYPE_REQUEST, new ApplyStereotypeEditPolicy());
// adds a custom EditPolicy to manage the displaying of each label on
// the connector
// this editpolicy erase ConnectionLabelsEditPolicy
installEditPolicy(ShowHideLabelEditPolicy.SHOW_HIDE_LABEL_ROLE, new ShowHideLabelEditPolicy());
}
/**
* {@inheritDoc}
*/
@Override
public EditPart getTargetEditPart(Request request) {
if (ApplyStereotypeRequest.APPLY_STEREOTYPE_REQUEST.equals(request.getType())) {
return this;
}
return super.getTargetEditPart(request);
}
/**
* <p>
* Returns the primary shape being the View of this edit part.
* </p>
* <b>Warning</b> It should never return <code>null</code>
*
* @return the primary shape associated to this edit part.
*/
@Override
public abstract UMLEdgeFigure getPrimaryShape();
/**
* Refresh the display of stereotypes for this uml node edit part.
*/
public void refreshAppliedStereotypes() {
// computes the stereotypes to be displayed
final String stereotypesToDisplay = stereotypesToDisplay();
// computes the icon to be displayed
final Image imageToDisplay = stereotypeIconToDisplay();
// if the string is not empty, then, the figure has to display it. Else,
// it displays nothing
if (stereotypesToDisplay != "" || imageToDisplay != null) {
getPrimaryShape().setStereotypeDisplay(stereotypesToDisplay, imageToDisplay);
} else {
// getPrimaryShape().setStereotypeDisplay(null, null);
}
}
/**
* {@inheritDoc}
*/
@Override
protected void removeSemanticListeners() {
super.removeSemanticListeners();
// remove listeners to react to the application and remove of
// stereotypes
removeListenerFilter(STEREOTYPABLE_ELEMENT);
removeListenerFilter(STEREOTYPED_ELEMENT);
}
/**
* {@inheritDoc}
*/
@Override
public Element getUMLElement() {
return (Element) resolveSemanticElement();
}
/**
* get the list of stereotype to display from the eannotation
*
* @return the list of stereotypes to display
*/
public String stereotypesToDisplay() {
String stereotypesToDisplay = StereotypeDisplayHelper.getInstance().getStereotypeTextToDisplay((View) getModel());
return stereotypesToDisplay;
}
/**
* Returns the image to be displayed for the applied stereotypes.
*
* @return the image that represents the first applied stereotype or <code>null</code> if no image has to be displayed
*/
public Image stereotypeIconToDisplay() {
String stereotypespresentationKind = AppliedStereotypeHelper.getAppliedStereotypePresentationKind((View) getModel());
if (stereotypespresentationKind == null) {
return null;
}
if (stereotypespresentationKind.equals(UMLVisualInformationPapyrusConstant.ICON_STEREOTYPE_PRESENTATION) || stereotypespresentationKind.equals(UMLVisualInformationPapyrusConstant.TEXT_ICON_STEREOTYPE_PRESENTATION)) {
// retrieve the first stereotype in the list of displayed stereotype
String stereotypesToDisplay = AppliedStereotypeHelper.getStereotypesToDisplay((View) getModel());
StringTokenizer tokenizer = new StringTokenizer(stereotypesToDisplay, ",");
if (tokenizer.hasMoreTokens()) {
String firstStereotypeName = tokenizer.nextToken();
Stereotype stereotype = getUMLElement().getAppliedStereotype(firstStereotypeName);
return Activator.getIconElement(getUMLElement(), stereotype, false);
}
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
protected void setLineWidth(int width) {
getPrimaryShape().setLineWidth(width < 0 ? 1 : width);
}
}
|