Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7a7b93785ca3c8ffa366d192edb7b430b4896e1c (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
/*****************************************************************************
 * Copyright (c) 2015 CEA LIST 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
 *   
 *****************************************************************************/

package org.eclipse.papyrus.uml.diagram.activity.edit.policies;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.common.core.command.CompositeCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.diagram.ui.commands.CommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.l10n.DiagramUIMessages;
import org.eclipse.gmf.runtime.diagram.ui.requests.EditCommandRequestWrapper;
import org.eclipse.gmf.runtime.emf.type.core.requests.MoveRequest;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.gmfdiag.common.editpolicies.DefaultCreationEditPolicy;
import org.eclipse.papyrus.uml.diagram.activity.commands.ActivityEdgeReparentCommand;
import org.eclipse.papyrus.uml.service.types.helper.ActivityNodeHelper;
import org.eclipse.uml2.uml.ActivityEdge;
import org.eclipse.uml2.uml.ActivityNode;
import org.eclipse.uml2.uml.ActivityPartition;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.InterruptibleActivityRegion;
import org.eclipse.uml2.uml.Pin;


/**
 * Class provide extended reparent command in case of:
 * - drag&drop from ActivityPartition or InterruptibleActivityRegion
 * - drag&drop of an ActivityEdge (reparent according to its new container)
 */
public class ActivityCompartmentCreationEditPolicy extends DefaultCreationEditPolicy {

	/**
	 * Set in moveRequest additional parameters then reparent from ActivityPartition
	 * or InterruptibleActivityRegion
	 * Reparent ActivityEdge if necessary
	 */
	@Override
	protected ICommand getReparentCommand(IGraphicalEditPart gep) {
		CompositeCommand cc = new CompositeCommand(DiagramUIMessages.AddCommand_Label);
		View container = (View) getHost().getModel();
		EObject context = ViewUtil.resolveSemanticElement(container);
		View view = (View) gep.getModel();
		EObject element = ViewUtil.resolveSemanticElement(view);

		TransactionalEditingDomain editingDomain = ((IGraphicalEditPart) getHost())
				.getEditingDomain();

		// semantic
		if (element != null) {
			// Set in moveRequest additional parameters then reparent from ActivityPartition
			// or InterruptibleActivityRegion
			MoveRequest req = new MoveRequest(editingDomain, context, element);
			EObject currentParentSemantic = ((IGraphicalEditPart) gep.getParent()).resolveSemanticElement();
			if (currentParentSemantic instanceof ActivityPartition) {
				req.setParameter(ActivityNodeHelper.OUT_FROM_PARTITION, currentParentSemantic);
			}
			if (currentParentSemantic instanceof InterruptibleActivityRegion) {
				req.setParameter(ActivityNodeHelper.OUT_FROM_INTERRUPTIBLE_REGION, currentParentSemantic);
			}
			Command moveSemanticCmd = getHost().getCommand(new EditCommandRequestWrapper(req));

			if (moveSemanticCmd == null) {
				return org.eclipse.gmf.runtime.common.core.command.UnexecutableCommand.INSTANCE;
			}

			cc.compose(new CommandProxy(moveSemanticCmd));

			// Reparent ActivityEdge if necessary
			if (context != null && shouldReparent(element, context)) {
				if (element instanceof ActivityNode) {
					ActivityNode activityNode = (ActivityNode) element;
					// correct container of ControlFlow
					for (ActivityEdge edge : activityNode.getOutgoings()) {
						cc.compose(new ActivityEdgeReparentCommand(editingDomain, edge));
					}
					for (ActivityEdge edge : activityNode.getIncomings()) {
						cc.compose(new ActivityEdgeReparentCommand(editingDomain, edge));
					}
					// correct container of ObjectFlow
					for (Element ownedElement : activityNode.getOwnedElements()) {
						if (ownedElement instanceof Pin) {
							for (ActivityEdge edge : ((Pin) ownedElement).getOutgoings()) {
								cc.compose(new ActivityEdgeReparentCommand(editingDomain, edge));
							}
							for (ActivityEdge edge : ((Pin) ownedElement).getIncomings()) {
								cc.compose(new ActivityEdgeReparentCommand(editingDomain, edge));
							}
						}
					}
				}
			}
		}

		// notation
		cc.compose(getReparentViewCommand(gep));
		return cc;
	}
}

Back to the top