Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e4eefd867e97cd30d2127c19b6ea967c3fbd5f87 (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
/*****************************************************************************
 * Copyright (c) 2010 CEA
 *
 *
 * 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:
 *   Soyatec - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.sequence.edit.policies;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Connection;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.requests.CreateConnectionRequest;
import org.eclipse.gef.requests.ReconnectRequest;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.INodeEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.GraphicalNodeEditPolicy;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.uml.diagram.sequence.command.AnnotatedLinkEditCommand;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.AnnotatedLinkEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.CustomDurationConstraintEditPart;
import org.eclipse.uml2.uml.TimeObservation;

/**
 * An editpolicy for handling connections start from Comment, Constraint and Observations.
 *
 * @see AnnotatedLinkEndEditPolicy
 *
 * @author Jin Liu (jin.liu@soyatec.com)
 */
public class AnnotatedLinkStartEditPolicy extends GraphicalNodeEditPolicy {

	public static final String ANNOTATED_LINK_START_ROLE = "Annotated Link Start Edit Policy";

	public static final String REQ_ANNOTATED_LINK_START = "annotated link start";

	public static final String REQ_ANNOTATED_LINK_REORIENT_START = "annotated link reorient start";

	@Override
	public EditPart getTargetEditPart(Request request) {
		if (REQ_ANNOTATED_LINK_START.equals(request.getType()) || REQ_ANNOTATED_LINK_REORIENT_START.equals(request.getType())) {
			return getConnectableEditPart();
		}
		return null;
	}

	/**
	 * Get connectable INodeEditPart safely for Constraint Label, Comment body and so on.
	 */
	@Override
	protected INodeEditPart getConnectableEditPart() {
		EditPart host = getHost();
		if (host instanceof INodeEditPart) {
			return (INodeEditPart) host;
		} else {
			IGraphicalEditPart hostGraphical = host.getAdapter(IGraphicalEditPart.class);
			if (hostGraphical == null) {
				return null;
			}
			EObject element = hostGraphical.resolveSemanticElement();
			if (element == null) {
				return null;
			}
			EditPart parent = host.getParent();
			if (!(parent instanceof INodeEditPart)) {
				return null;
			}
			IGraphicalEditPart parentGraphical = parent.getAdapter(IGraphicalEditPart.class);
			if (parentGraphical != null && element == parentGraphical.resolveSemanticElement()) {
				return (INodeEditPart) parent;
			}
		}
		return null;
	}

	@Override
	public Command getCommand(Request request) {
		if (REQ_ANNOTATED_LINK_START.equals(request.getType())) {
			return getConnectionCreateCommand((CreateConnectionRequest) request);
		} else if (REQ_ANNOTATED_LINK_REORIENT_START.equals(request.getType())) {
			return getReconnectSourceCommand((ReconnectRequest) request);
		}
		return null;
	}

	@Override
	public void showSourceFeedback(Request request) {
		if (AnnotatedLinkEndEditPolicy.REQ_ANNOTATED_LINK_END.equals(request.getType())) {
			showCreationFeedback((CreateConnectionRequest) request);
		}
	}

	@Override
	public void eraseSourceFeedback(Request request) {
		if (AnnotatedLinkEndEditPolicy.REQ_ANNOTATED_LINK_END.equals(request.getType())) {
			eraseCreationFeedback((CreateConnectionRequest) request);
		}
	}

	@Override
	protected Connection createDummyConnection(Request req) {
		Connection conn = super.createDummyConnection(req);
		conn.setForegroundColor(ColorConstants.black);
		return conn;
	}

	@Override
	protected Command getReconnectSourceCommand(ReconnectRequest request) {
		EditPart host = getHost();
		// Quickly failed for TimeObservation, the event of a TimeObservation can only one.
		if (host instanceof GraphicalEditPart) {
			View primaryView = ((GraphicalEditPart) host).getPrimaryView();
			EObject element = ViewUtil.resolveSemanticElement(primaryView);
			if (element instanceof TimeObservation && ((TimeObservation) element).getEvent() != null) {
				return UnexecutableCommand.INSTANCE;
			}
		}
		ICommandProxy c = (ICommandProxy) super.getReconnectSourceCommand(request);
		if (c == null) {
			return null;
		}
		if (request.getConnectionEditPart() instanceof AnnotatedLinkEditPart) {
			if (getHost() instanceof CustomDurationConstraintEditPart && !((CustomDurationConstraintEditPart) getHost()).canCreateLink(request.getLocation())) {
				return UnexecutableCommand.INSTANCE; // only 2 links are allowed, one for each side
			}

			CompositeCommand cc = (CompositeCommand) c.getICommand();
			AnnotatedLinkEditCommand ac = new AnnotatedLinkEditCommand(getEditingDomain());
			ac.setAnnotatedLink((AnnotatedLinkEditPart) request.getConnectionEditPart());
			ac.setSource(getHost());
			cc.add(ac);
			return c;
		}
		return UnexecutableCommand.INSTANCE;
	}

	@Override
	protected Command getConnectionCreateCommand(CreateConnectionRequest request) {
		EditPart host = getHost();
		// Quickly failed for TimeObservation, the event of a TimeObservation can only one.
		if (host instanceof GraphicalEditPart) {
			View primaryView = ((GraphicalEditPart) host).getPrimaryView();
			EObject element = ViewUtil.resolveSemanticElement(primaryView);
			if (element instanceof TimeObservation && ((TimeObservation) element).getEvent() != null) {
				return UnexecutableCommand.INSTANCE;
			}
			if (host instanceof CustomDurationConstraintEditPart) {
				boolean can = ((CustomDurationConstraintEditPart) host).canCreateLink(request.getLocation());
				if (!can) {
					return UnexecutableCommand.INSTANCE;
				}
			}
		}
		Command command = super.getConnectionCreateCommand(request);
		if (command instanceof ICommandProxy) {
			CompositeCommand cc = (CompositeCommand) ((ICommandProxy) command).getICommand();
			AnnotatedLinkEditCommand operation = new AnnotatedLinkEditCommand(getEditingDomain());
			operation.setSource(getHost());
			cc.add(operation);
		}
		return command;
	}

	private TransactionalEditingDomain getEditingDomain() {
		return ((IGraphicalEditPart) getHost()).getEditingDomain();
	}
}

Back to the top