Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 386bb347b38a0433263e3510cad1db18f5e136f5 (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*****************************************************************************
 * Copyright (c) 2017, 2018 CEA LIST, ALL4TEC, Christian W. Damus, and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   CEA LIST - Initial API and implementation
 *   Mickaël ADAM (ALL4TEC) mickael.adam@all4tec.net - Bug 519621, 526803
 *   Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Bug 531520
 *   Christian W. Damus - bugs 533672, 536486
 *****************************************************************************/

package org.eclipse.papyrus.uml.diagram.sequence.edit.parts;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;

import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateUnspecifiedTypeRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewAndElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.uml.diagram.common.draw2d.anchors.FixedAnchor;
import org.eclipse.papyrus.uml.diagram.sequence.LifelineNodePlate;
import org.eclipse.papyrus.uml.diagram.sequence.edit.policies.LifeLineRestorePositionEditPolicy;
import org.eclipse.papyrus.uml.diagram.sequence.edit.policies.LifelineSelectionEditPolicy;
import org.eclipse.papyrus.uml.diagram.sequence.edit.policies.UpdateNodeReferenceEditPolicy;
import org.eclipse.papyrus.uml.diagram.sequence.edit.policies.UpdateWeakReferenceForExecSpecEditPolicy;
import org.eclipse.papyrus.uml.diagram.sequence.edit.policies.UpdateWeakReferenceForMessageSpecEditPolicy;
import org.eclipse.papyrus.uml.diagram.sequence.figures.ILifelineInternalFigure;
import org.eclipse.papyrus.uml.diagram.sequence.figures.LifeLineLayoutManager;
import org.eclipse.papyrus.uml.diagram.sequence.figures.LifelineFigure;
import org.eclipse.papyrus.uml.diagram.sequence.figures.LifelineNodeFigure;
import org.eclipse.papyrus.uml.diagram.sequence.locator.MessageCreateLifelineAnchor;
import org.eclipse.papyrus.uml.diagram.sequence.locator.TimeElementLocator;
import org.eclipse.papyrus.uml.diagram.sequence.providers.UMLElementTypes;
import org.eclipse.papyrus.uml.diagram.sequence.referencialgrilling.DisplayEvent;
import org.eclipse.papyrus.uml.diagram.sequence.util.SequenceUtil;
import org.eclipse.uml2.uml.Message;
import org.eclipse.uml2.uml.MessageEnd;
import org.eclipse.uml2.uml.MessageSort;

/**
 * @author Patrick Tessier
 * @since 3.0
 *
 */
public class CLifeLineEditPart extends LifelineEditPart {

	/** The default height of the figure. */
	public static int DEFAUT_HEIGHT = 700;

	/** The default width of the figure. */
	public static int DEFAUT_WIDTH = 100;

	/**
	 * The minimum height of the figure.
	 *
	 * @since 4.0
	 */
	public static int MIN_HEIGHT = 100;
	private EditPart activeCreateFeedbackEditPart;


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

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.papyrus.infra.gmfdiag.common.editpart.NodeEditPart#createSVGNodePlate()
	 */
	@Override
	protected NodeFigure createSVGNodePlate() {
		if (null == svgNodePlate) {
			svgNodePlate = new LifelineNodePlate(this, -1, -1).withLinkLFEnabled();
			svgNodePlate.setDefaultNodePlate(createNodePlate());
		}
		return svgNodePlate;
	}

	@Override
	protected NodeFigure createNodeFigure() {
		return new LifelineNodeFigure(createMainFigureWithSVG());
	}

	/**
	 * @see org.eclipse.papyrus.uml.diagram.common.editparts.NamedElementEditPart#refresh()
	 *
	 */
	@Override
	public void refresh() {
		if (getPrimaryShape() instanceof LifelineFigure) {
			// Bug 531520: we redefine the border of the lifeline, in order to include the children
			// the message are connected to the middle line of the Lifeline, but they must be drawn as connected on the ExecutionSpeficiation
			final List<NodeFigure> childrenFigure = new ArrayList<>();
			for (final Object current : getChildren()) {
				if (current instanceof AbstractExecutionSpecificationEditPart) {
					NodeFigure figure = ((AbstractExecutionSpecificationEditPart) current).getPrimaryShape();
					childrenFigure.add(figure);
				}
			}
			getPrimaryShape().setChildrenFigure(childrenFigure);
		}

		super.refresh();
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.papyrus.uml.diagram.sequence.edit.parts.LifelineEditPart#createDefaultEditPolicies()
	 */
	@Override
	protected void createDefaultEditPolicies() {
		super.createDefaultEditPolicies();
		installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE, new LifelineSelectionEditPolicy());
		installEditPolicy(LifeLineRestorePositionEditPolicy.KEY, new LifeLineRestorePositionEditPolicy());
		installEditPolicy(UpdateNodeReferenceEditPolicy.UDPATE_NODE_REFERENCE, new UpdateNodeReferenceEditPolicy());
		installEditPolicy(UpdateWeakReferenceForMessageSpecEditPolicy.UDPATE_WEAK_REFERENCE_FOR_MESSAGE, new UpdateWeakReferenceForMessageSpecEditPolicy());
		installEditPolicy(UpdateWeakReferenceForExecSpecEditPolicy.UDPATE_WEAK_REFERENCE_FOR_EXECSPEC, new UpdateWeakReferenceForExecSpecEditPolicy());
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.papyrus.uml.diagram.common.editparts.UMLNodeEditPart#setLayoutConstraint(org.eclipse.gef.EditPart, org.eclipse.draw2d.IFigure, java.lang.Object)
	 */
	@Override
	public void setLayoutConstraint(EditPart child, IFigure childFigure, Object constraint) {
		if (!(childFigure instanceof ILifelineInternalFigure)) {
			getPrimaryShape().setConstraint(childFigure, constraint);
		}
	}

	/**
	 * @return the size of the header height
	 *         if the layout is null return -1
	 * @since 4.0
	 */
	public int getStickerHeight() {
		if (getPrimaryShape().getLifeLineLayoutManager() != null) {
			return ((LifeLineLayoutManager) getPrimaryShape().getLifeLineLayoutManager()).getBottomHeader() - getPrimaryShape().getBounds().y();
		}
		return -1;
	}

	/**
	 * Create specific anchor to handle connection on top, on center and on bottom of the lifeline
	 */
	@Override
	public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart connEditPart) {
		if (connEditPart instanceof MessageCreateEditPart) {
			// Create message anchor
			return new MessageCreateLifelineAnchor(getPrimaryShape(), this);

		} else if (connEditPart instanceof MessageDeleteEditPart) {
			// delete message anchor
			return new FixedAnchor(getPrimaryShape(), FixedAnchor.BOTTOM);
		}
		// if (connEditPart instanceof MessageAsyncEditPart) {// TODO_MIA test it
		// String terminal = AnchorHelper.getAnchorId(getEditingDomain(), connEditPart, false);
		// if (terminal.length() > 0) {
		// int start = terminal.indexOf("{") + 1;
		// PrecisionPoint pt = BaseSlidableAnchor.parseTerminalString(terminal);
		// boolean rightHand = true;
		// if (start > 0) {
		// if (terminal.charAt(start) == 'L') {
		// rightHand = false;
		// }
		// } else {
		// Connection c = (Connection) connEditPart.getFigure();
		// PointList list = c.getPoints();
		// if (list.getPoint(0).x > list.getPoint(1).x) {
		// rightHand = false;
		// }
		// }
		// return new AnchorHelper.SideAnchor(getNodeFigure(), pt, rightHand);
		// }
		// }
		return super.getTargetConnectionAnchor(connEditPart);
	}

	/**
	 * @since 5.0
	 */
	@Override
	public EditPart getTargetEditPart(Request request) {
		if (request instanceof CreateViewAndElementRequest) {
			CreateViewAndElementRequest req = (CreateViewAndElementRequest) request;

			// If we're creating an operand, it needs to be done by the covering combined fragment
			if (UMLElementTypes.InteractionOperand_Shape.equals(req.getViewAndElementDescriptor().getElementAdapter().getAdapter(IElementType.class))) {
				EditPart container = SequenceUtil.findInteractionFragmentContainerEditPartAt(req.getLocation(), this);
				if (container instanceof CInteractionOperandEditPart) {
					// Delegate again to the combined fragment
					container = SequenceUtil.getParentCombinedFragmentPart(container);
				}
				if (container != null) {
					return container.getTargetEditPart(request);
				}
			} else if (UMLElementTypes.CombinedFragment_Shape.equals(req.getViewAndElementDescriptor().getElementAdapter().getAdapter(IElementType.class))
					|| UMLElementTypes.InteractionUse_Shape.equals(req.getViewAndElementDescriptor().getElementAdapter().getAdapter(IElementType.class))) {
				EditPart container = SequenceUtil.getInteractionCompartment(this);
				if (container != null) {
					return container.getTargetEditPart(request);
				}
			}
		}

		return super.getTargetEditPart(request);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void showTargetFeedback(Request request) {
		if (request instanceof CreateUnspecifiedTypeRequest) {
			((CreateUnspecifiedTypeRequest) request).getElementTypes().forEach(t -> {
				CreateRequest req = ((CreateUnspecifiedTypeRequest) request).getRequestForType((IElementType) t);
				EditPart targetEP = getTargetEditPart(req);
				// as target EP may vary during time, moving along a lifeline, crossing several elements for example
				// storing the target EP seems interesting.
				if (activeCreateFeedbackEditPart != targetEP) {
					// erase active feedback if any
					if (activeCreateFeedbackEditPart != null) {
						activeCreateFeedbackEditPart.eraseTargetFeedback(request);
					}
					activeCreateFeedbackEditPart = targetEP;
				}
				if (targetEP != this) {
					targetEP.showTargetFeedback(request);
				} else {
					super.showTargetFeedback(request);
				}
			});
			return;
		}
		super.showTargetFeedback(request);

	}

	@SuppressWarnings("unchecked")
	@Override
	public void eraseTargetFeedback(Request request) {
		if (request instanceof CreateUnspecifiedTypeRequest) {
			((CreateUnspecifiedTypeRequest) request).getElementTypes().forEach(t -> {
				CreateRequest req = ((CreateUnspecifiedTypeRequest) request).getRequestForType((IElementType) t);
				EditPart targetEP = getTargetEditPart(req);
				if (activeCreateFeedbackEditPart != null && activeCreateFeedbackEditPart != this) {
					// erase active feedback from saved EP
					activeCreateFeedbackEditPart.eraseTargetFeedback(request);
					activeCreateFeedbackEditPart = null;
				}
				if (targetEP != this) {
					targetEP.eraseTargetFeedback(request);
				} else {
					super.eraseTargetFeedback(request);
				}
			});
			return;
		}
		super.eraseTargetFeedback(request);
	}

	@Override
	protected boolean addFixedChild(EditPart childEditPart) {
		Optional<MessageEnd> createEnd = TimeElementLocator.getTimedElement(childEditPart, MessageEnd.class)
				.filter(MessageEnd::isReceive)
				.filter(end -> end.getMessage().getMessageSort() == MessageSort.CREATE_MESSAGE_LITERAL);

		return createEnd.map(this::getCreateMessageIncomingSide)
				.filter(OptionalInt::isPresent)
				.map(side -> {
					getBorderedFigure().getBorderItemContainer()
							.add(((IGraphicalEditPart) childEditPart).getFigure(),
									new TimeElementLocator(getMainFigure(), this::getTimeElementSide));

					return true;
				}).orElseGet(() -> super.addFixedChild(childEditPart));
	}

	public OptionalInt getCreateMessageIncomingSide(Point where) {
		// The proposed location is in relative coordinates, but the DisplayEvent API is
		// in absolute terms (dealing with the mouse pointer)
		Point search = where.getCopy();
		getMainFigure().translateToAbsolute(search);

		DisplayEvent displayEvent = new DisplayEvent(this);
		MessageEnd end = displayEvent.getMessageEvent(getMainFigure(), search);
		if ((end != null) && end.isReceive() && (end.getMessage().getMessageSort() == MessageSort.CREATE_MESSAGE_LITERAL)) {
			return getCreateMessageIncomingSide(end);
		}

		return OptionalInt.empty();
	}

	private OptionalInt getCreateMessageIncomingSide(MessageEnd end) {
		LifelineFigure lifelineFigure = (LifelineFigure) svgNodePlate.getChildren().get(0);
		IFigure headerFigure = lifelineFigure.getHeaderFigure();

		OptionalInt result = OptionalInt.empty();

		for (Object next : getTargetConnections()) {
			ConnectionEditPart incoming = (ConnectionEditPart) next;
			EObject semantic = incoming.getAdapter(EObject.class);
			if (semantic instanceof Message) {
				Message message = (Message) semantic;
				if (message.getMessageSort() == MessageSort.CREATE_MESSAGE_LITERAL) {
					Point headCenter = headerFigure.getBounds().getCenter();
					lifelineFigure.translateToAbsolute(headCenter);
					Connection conn = (Connection) incoming.getFigure();
					Point target = conn.getPoints().getLastPoint();
					if (target.x() > headCenter.x()) {
						// The create message is incoming from the right
						result = OptionalInt.of(PositionConstants.EAST);
					} else {
						result = OptionalInt.of(PositionConstants.WEST);
					}
				}
			}
		}

		return result;
	}

	int getTimeElementSide(Rectangle proposedBounds) {
		int incoming = getCreateMessageIncomingSide(proposedBounds.getTopLeft())
				.orElse(PositionConstants.WEST);

		// Put the time element on the side opposite to the incoming create message
		return PositionConstants.EAST_WEST ^ incoming;
	}
}

Back to the top