Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e91e09df8357a5373515955748811743c87a1148 (plain) (blame)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*****************************************************************************
 * Copyright (c) 2010 CEA
 *
 *    
 * 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:
 *   Soyatec - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.sequence.edit.parts;

import java.util.List;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.transaction.RunnableWithResult;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.requests.DropRequest;
import org.eclipse.gef.requests.ReconnectRequest;
import org.eclipse.gmf.runtime.common.core.util.Log;
import org.eclipse.gmf.runtime.common.core.util.Trace;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.diagram.ui.editparts.INodeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
import org.eclipse.gmf.runtime.diagram.ui.figures.IBorderItemLocator;
import org.eclipse.gmf.runtime.diagram.ui.internal.DiagramUIDebugOptions;
import org.eclipse.gmf.runtime.diagram.ui.internal.DiagramUIPlugin;
import org.eclipse.gmf.runtime.diagram.ui.internal.DiagramUIStatusCodes;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
import org.eclipse.gmf.runtime.notation.Anchor;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.IdentityAnchor;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.emf.appearance.helper.AppearanceHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.editpart.IPapyrusEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.edit.policies.AppliedStereotypeCommentCreationEditPolicyEx;
import org.eclipse.papyrus.uml.diagram.sequence.edit.policies.CustomConnectionHandleEditPolicy;
import org.eclipse.papyrus.uml.diagram.sequence.providers.UMLElementTypes;
import org.eclipse.papyrus.uml.diagram.sequence.util.ElementIconUtil;
import org.eclipse.papyrus.uml.diagram.stereotype.edition.editpolicies.AppliedStereotypeCommentCreationEditPolicy;
import org.eclipse.swt.graphics.Image;

/**
 * Add implementing IPapyrusEditPart to support displaying Stereotype as a Comment, because the label is always selected when a TimeObservation is
 * selected.
 * 
 * @author Jin Liu (jin.liu@soyatec.com)
 */
public class CustomTimeObservationLabelEditPart extends TimeObservationLabelEditPart implements INodeEditPart, IPapyrusEditPart {

	/**
	 * Constructor.
	 * 
	 * @param view
	 */
	public CustomTimeObservationLabelEditPart(View view) {
		super(view);
	}

	/**
	 * @Override
	 */
	@Override
	protected void createDefaultEditPolicies() {
		super.createDefaultEditPolicies();
		installEditPolicy(EditPolicyRoles.CONNECTION_HANDLES_ROLE, new CustomConnectionHandleEditPolicy());
		//install a editpolicy to display stereotypes
		installEditPolicy(AppliedStereotypeCommentCreationEditPolicy.APPLIED_STEREOTYPE_COMMENT, new AppliedStereotypeCommentCreationEditPolicyEx());
	}

	/**
	 * @Override
	 */
	@Override
	protected String getLabelTextHelper(IFigure figure) {
		if(figure instanceof NodeFigure && getWrappingLabel(figure) != null) {
			return getWrappingLabel(figure).getText();
		}
		return super.getLabelTextHelper(figure);
	}

	/**
	 * @Override
	 */
	@Override
	protected void setLabelTextHelper(IFigure figure, String text) {
		if(figure instanceof NodeFigure && getWrappingLabel(figure) != null) {
			getWrappingLabel(figure).setText(text);
		} else {
			super.setLabelTextHelper(figure, text);
		}
	}

	/**
	 * @Override
	 */
	@Override
	protected Image getLabelIconHelper(IFigure figure) {
		if(figure instanceof NodeFigure && getWrappingLabel(figure) != null) {
			return getWrappingLabel(figure).getIcon();
		} else {
			return super.getLabelIconHelper(figure);
		}
	}

	/**
	 * @Override
	 */
	@Override
	protected void setLabelIconHelper(IFigure figure, Image icon) {
		if(figure instanceof NodeFigure && getWrappingLabel(figure) != null) {
			getWrappingLabel(figure).setIcon(icon);
		} else {
			super.setLabelIconHelper(figure, icon);
		}
	}

	/**
	 * @Override
	 */
	@Override
	protected void handleNotificationEvent(Notification event) {
		Object feature = event.getFeature();
		if(NotationPackage.eINSTANCE.getView_SourceEdges().equals(feature)) {
			refreshSourceConnections();
		} else if(NotationPackage.eINSTANCE.getView_TargetEdges().equals(feature)) {
			refreshTargetConnections();
		}
		if(ElementIconUtil.isIconNotification(event)) {
			refreshLabel();
		}
		super.handleNotificationEvent(event);
	}

	@Override
	protected NodeFigure createFigure() {
		NodeFigure figure = createNodePlate();
		figure.setLayoutManager(new StackLayout());
		IFigure shape = createNodeShape();
		figure.add(shape);
		return figure;
	}

	protected NodeFigure createNodePlate() {
		NodeFigure result = new NodeFigure();
		return result;
	}

	protected IFigure createNodeShape() {
		IFigure label = createFigurePrim();
		return label;
	}

	protected WrappingLabel getWrappingLabel(IFigure nodeFigure) {
		if(nodeFigure instanceof NodeFigure) {
			return ((WrappingLabel)(nodeFigure.getChildren().get(0)));
		}
		return null;
	}

	//Fix https://bugs.eclipse.org/bugs/show_bug.cgi?id=364826
	@Override
	protected List getModelSourceConnections() {
		return ViewUtil.getSourceConnectionsConnectingVisibleViews((View)getModel());
	}

	@Override
	protected List getModelTargetConnections() {
		List list = ViewUtil.getTargetConnectionsConnectingVisibleViews((View)getModel());
		return list;
	}

	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		if(request instanceof ReconnectRequest) {
			if(((DropRequest)request).getLocation() == null) {
				return ((NodeFigure)getFigure()).getSourceConnectionAnchorAt(null);
			}
			Point pt = ((DropRequest)request).getLocation().getCopy();
			return ((NodeFigure)getFigure()).getSourceConnectionAnchorAt(pt);
		} else if(request instanceof DropRequest) {
			return ((NodeFigure)getFigure()).getSourceConnectionAnchorAt(((DropRequest)request).getLocation());
		}
		return ((NodeFigure)getFigure()).getSourceConnectionAnchorAt(null);
	}

	public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart connEditPart) {
		final org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart connection = (org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart)connEditPart;
		String t = ""; //$NON-NLS-1$
		try {
			t = (String)getEditingDomain().runExclusive(new RunnableWithResult.Impl() {

				public void run() {
					Anchor a = ((Edge)connection.getModel()).getSourceAnchor();
					if(a instanceof IdentityAnchor) {
						setResult(((IdentityAnchor)a).getId());
					} else {
						setResult(""); //$NON-NLS-1$
					}
				}
			});
		} catch (InterruptedException e) {
			Trace.catching(DiagramUIPlugin.getInstance(), DiagramUIDebugOptions.EXCEPTIONS_CATCHING, getClass(), "getSourceConnectionAnchor", e); //$NON-NLS-1$
			Log.error(DiagramUIPlugin.getInstance(), DiagramUIStatusCodes.IGNORED_EXCEPTION_WARNING, "getSourceConnectionAnchor", e); //$NON-NLS-1$
		}
		return ((NodeFigure)getFigure()).getConnectionAnchor(t);
	}

	public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart connEditPart) {
		final org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart connection = (org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart)connEditPart;
		String t = ""; //$NON-NLS-1$
		try {
			t = (String)getEditingDomain().runExclusive(new RunnableWithResult.Impl() {

				public void run() {
					Anchor a = ((Edge)connection.getModel()).getTargetAnchor();
					if(a instanceof IdentityAnchor) {
						setResult(((IdentityAnchor)a).getId());
					} else {
						setResult(""); //$NON-NLS-1$
					}
				}
			});
		} catch (InterruptedException e) {
			Trace.catching(DiagramUIPlugin.getInstance(), DiagramUIDebugOptions.EXCEPTIONS_CATCHING, getClass(), "getTargetConnectionAnchor", e); //$NON-NLS-1$
			Log.error(DiagramUIPlugin.getInstance(), DiagramUIStatusCodes.IGNORED_EXCEPTION_WARNING, "getTargetConnectionAnchor", e); //$NON-NLS-1$
		}
		return ((NodeFigure)getFigure()).getConnectionAnchor(t);
	}

	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		if(request instanceof ReconnectRequest) {
			if(((DropRequest)request).getLocation() == null) {
				return ((NodeFigure)getFigure()).getTargetConnectionAnchorAt(null);
			}
			Point pt = ((DropRequest)request).getLocation().getCopy();
			return ((NodeFigure)getFigure()).getTargetConnectionAnchorAt(pt);
		} else if(request instanceof DropRequest) {
			return ((NodeFigure)getFigure()).getTargetConnectionAnchorAt(((DropRequest)request).getLocation());
		}
		return ((NodeFigure)getFigure()).getTargetConnectionAnchorAt(null);
	}

	public String mapConnectionAnchorToTerminal(ConnectionAnchor c) {
		return ((NodeFigure)getFigure()).getConnectionAnchorTerminal(c);
	}

	public ConnectionAnchor mapTerminalToConnectionAnchor(String terminal) {
		return ((NodeFigure)getFigure()).getConnectionAnchor(terminal);
	}

	public boolean canAttachNote() {
		return true;
	}

	@Override
	protected Image getLabelIcon() {
		if(AppearanceHelper.showElementIcon(getNotationView())) {
			return UMLElementTypes.getImage(resolveSemanticElement().eClass());
		}
		return null;
	}

	@Override
	public void refreshBounds() {
		super.refreshBounds();
		//Update location manually.
		IBorderItemLocator locator = getBorderItemLocator();
		if(locator != null) {
			locator.relocate(getFigure());
		}
	}

	public IFigure getPrimaryShape() {
		return getFigure();
	}
}

Back to the top