Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1a95c8fc85e177238fe03156f8ab718415cd8b01 (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
/*****************************************************************************
 * Copyright (c) 2011-2012, 2019 CEA LIST.
 *
 * 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
 *	  Nicolas FAUVERGUE (CEA LIST) nicolas.fauvergue@cea.fr - Bug 494514
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.service.types.helper;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.common.core.command.IdentityCommand;
import org.eclipse.gmf.runtime.common.core.command.UnexecutableCommand;
import org.eclipse.gmf.runtime.emf.type.core.commands.ConfigureElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.commands.CreateRelationshipCommand;
import org.eclipse.gmf.runtime.emf.type.core.commands.SetValueCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRelationshipRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.uml.service.types.command.ControlFlowReorientCommand;
import org.eclipse.papyrus.uml.service.types.utils.RequestParameterUtils;
import org.eclipse.papyrus.uml.tools.utils.ActivityEdgeUtil;
import org.eclipse.papyrus.uml.tools.utils.ControlFlowUtil;
import org.eclipse.uml2.uml.ActivityNode;
import org.eclipse.uml2.uml.ActivityPartition;
import org.eclipse.uml2.uml.ControlFlow;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * Edit helper class for binary {@link ControlFlow}
 */
public class ControlFlowEditHelper extends ElementEditHelper {

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected ICommand getReorientRelationshipCommand(ReorientRelationshipRequest req) {
		ICommand resultCommand = new ControlFlowReorientCommand(req);

		// Get the control flow
		final ControlFlow controlFlow = (ControlFlow) req.getRelationship();

		// Get if this is a source re-orient or the target
		final boolean isReorientSource = req.getDirection() == ReorientRelationshipRequest.REORIENT_SOURCE;

		// Get the source and target from controlFlow and request for needed one
		final Object source = isReorientSource ? req.getNewRelationshipEnd() : controlFlow.getSource();
		final Object target = isReorientSource ? controlFlow.getTarget() : req.getNewRelationshipEnd();

		if ((null == source || source instanceof ActivityNode) && (null == target || target instanceof ActivityNode)) {
			// Get the command to update the InPartition feature
			final ICommand updatePartitionsCommand = getInPartitionUpdateCommand(controlFlow, (ActivityNode) source, (ActivityNode) target);
			if (null != updatePartitionsCommand) {
				resultCommand = CompositeCommand.compose(resultCommand, updatePartitionsCommand);
			}
		}

		return resultCommand;
	}

	/**
	 * Test if the relationship creation is allowed.
	 *
	 * @param source
	 *            the relationship source can be null
	 * @param target
	 *            the relationship target can be null
	 * @param sourceView
	 *            the relationship graphical source can be null
	 * @param targetView
	 *            the relationship graphical target can be null
	 * @return true if the creation is allowed
	 */
	protected boolean canCreate(EObject source, EObject target, View sourceView, View targetView) {
		return canCreate(source, target);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected ICommand getCreateRelationshipCommand(CreateRelationshipRequest req) {
		EObject source = req.getSource();
		EObject target = req.getTarget();

		boolean noSourceOrTarget = (source == null || target == null);
		boolean noSourceAndTarget = (source == null && target == null);

		if (!noSourceAndTarget && !canCreate(source, target, RequestParameterUtils.getSourceView(req), RequestParameterUtils.getTargetView(req))) {
			// Abort creation.
			return UnexecutableCommand.INSTANCE;
		}

		if (noSourceOrTarget && !noSourceAndTarget) {
			// The request isn't complete yet. Return the identity command so
			// that the create relationship gesture is enabled.
			// this can be before first creation => check the source
			if (source != null && !(source instanceof ActivityNode)) {
				return UnexecutableCommand.INSTANCE;
			}
			return IdentityCommand.INSTANCE;
		}

		// Propose a semantic container for the new control flow.
		Element proposedContainer = deduceContainer(req);
		if (proposedContainer == null) {
			return UnexecutableCommand.INSTANCE;
		}

		req.setContainer(proposedContainer);

		return new CreateRelationshipCommand(req);
	}

	protected Element deduceContainer(CreateRelationshipRequest request) {
		return ActivityEdgeUtil.deduceContainer(request.getSource(), request.getTarget());
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected ICommand getConfigureCommand(final ConfigureRequest req) {

		ICommand configureCommand = new ConfigureElementCommand(req) {

			@Override
			protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {

				ControlFlow element = (ControlFlow) req.getElementToConfigure();
				ActivityNode source = getSourceObject(req);
				ActivityNode target = getTargetObject(req);
				if (source != null) {
					element.setSource(getSourceObject(req));
				}
				if (target != null) {
					element.setTarget(target);
				}

				// Manage InPartitions
				if (null != source && null != target) {

					// Get the source InPartitions
					final List<ActivityPartition> partitions = new ArrayList<>(source.getInPartitions());
					// Keep the InPartitions contained in the source and target to add it into the control flow
					partitions.retainAll(target.getInPartitions());

					element.getInPartitions().addAll(partitions);
				}

				return CommandResult.newOKCommandResult(element);
			}
		};

		return CompositeCommand.compose(configureCommand, super.getConfigureCommand(req));
	}

	/**
	 * This method provides the object to be use as source.
	 *
	 * @return the source value
	 */
	protected ActivityNode getSourceObject(ConfigureRequest req) {
		Object result = req.getParameter(CreateRelationshipRequest.SOURCE);
		return (result instanceof ActivityNode) ? (ActivityNode) result : null;
	}

	/**
	 * This method provides the object to be used as target.
	 *
	 * @return the target value
	 */
	protected ActivityNode getTargetObject(ConfigureRequest req) {
		Object result = req.getParameter(CreateRelationshipRequest.TARGET);
		return (result instanceof ActivityNode) ? (ActivityNode) result : null;
	}

	/**
	 * {@inheritDoc}
	 */
	protected boolean canCreate(EObject source, EObject target) {
		if ((source != null) && !(source instanceof ActivityNode)) {
			return false;
		}

		if ((target != null) && !(target instanceof ActivityNode)) {
			return false;
		}

		Element container = ActivityEdgeUtil.deduceContainer(source, target);
		if (container == null) {
			return false;
		}
		return ControlFlowUtil.canExistControlFlow(container, null, (ActivityNode) source, (ActivityNode) target);
	}

	/**
	 * This allows to get the command to update the InPartition feature if needed.
	 *
	 * @param controlFlow
	 *            The control flow to update.
	 * @param source
	 *            The source activity node.
	 * @param target
	 *            The target activity node.
	 * @return The command to update the InPartition feature (can be <code>null</code>).
	 */
	protected ICommand getInPartitionUpdateCommand(final ControlFlow controlFlow, final ActivityNode source, final ActivityNode target) {
		List<ActivityPartition> partitions = new ArrayList<>();
		if (null != source && null != target) {
			// Get the source InPartitions
			partitions = new ArrayList<>(source.getInPartitions());
			// Keep the in partitions contained in the source and target to add it into the control flow
			partitions.retainAll(target.getInPartitions());

			if (!partitions.isEmpty()) {
				return new SetValueCommand(new SetRequest(controlFlow, UMLPackage.eINSTANCE.getActivityEdge_InPartition(), partitions));
			}
		}

		// If the partitions found are empty and the control flow InPartitions are not, clear this
		if (partitions.isEmpty() && !controlFlow.getInPartitions().isEmpty()) {
			return new SetValueCommand(new SetRequest(controlFlow, UMLPackage.eINSTANCE.getActivityEdge_InPartition(), partitions));
		}

		return null;
	}

}

Back to the top