Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bd8dc168121d0d6633b9ddcde59606a03ff0eae3 (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
/*****************************************************************************
 * Copyright (c) 2019 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
 * http://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Pauline DEVILLE (CEA LIST) - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.uml.diagram.activity.dnd.commands;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.IHintedType;
import org.eclipse.gmf.runtime.emf.type.core.commands.CreateElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.uml.diagram.activity.dnd.Messages;
import org.eclipse.papyrus.uml.service.types.element.UMLElementTypes;
import org.eclipse.uml2.uml.AcceptEventAction;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Signal;
import org.eclipse.uml2.uml.SignalEvent;
import org.eclipse.uml2.uml.Trigger;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * Extends the generic class to create and update an AcceptEventAction,
 * create and trigger and a signalEvent then set the trigger
 *
 * @since 3.5.0
 */
public class CreateAcceptEventActionAndUpdateCommand<T extends EObject, E extends EObject, S extends EObject> extends CreateTAndUpdateCommand<T, E, S> {

	private static final String EVENT_PACKAGE_NAME = Messages.CreateAcceptEventActionAndUpdateCommand_PackageName;

	/**
	 * Constructor.
	 *
	 * @param targetEditPart
	 * @param typeParameterClass
	 * @param targetElementDiagram
	 * @param sourceElement
	 * @param headless
	 * @param typeToCreate
	 * @param location
	 * @param dropEditPartVisualID
	 */
	public CreateAcceptEventActionAndUpdateCommand(EditPart targetEditPart, Class<T> typeParameterClass, E targetElementDiagram, S sourceElement, boolean headless, IHintedType typeToCreate, Point location,
			String dropEditPartVisualID) {
		super(targetEditPart, typeParameterClass, targetElementDiagram, sourceElement, headless, typeToCreate, null, location, dropEditPartVisualID);
	}

	/**
	 * @see org.eclipse.papyrus.uml.diagram.activity.dnd.commands.CreateTAndUpdateCommand#updateNewlyCreatedEObjectWithEObjectDragged(org.eclipse.emf.ecore.EObject, org.eclipse.emf.ecore.EObject)
	 *
	 * @param slot
	 * @param property
	 * @throws ExecutionException
	 */
	@Override
	protected void updateNewlyCreatedEObjectWithEObjectDragged(T slot, S property) throws ExecutionException {
		AcceptEventAction action = (AcceptEventAction) slot;
		action.setIsUnmarshall(true);
		Signal signal = (Signal) sourceElement;

		// Retrieve the created slot, and update its properties
		EObject newCreatedElement = null;
		if (!action.getTriggers().isEmpty()) {
			newCreatedElement = action.getTriggers().get(0);
		} else {
			newCreatedElement = createElement(action, UMLElementTypes.TRIGGER);
		}
		if (newCreatedElement == null || false == newCreatedElement instanceof Trigger) {
			return;
		}
		final Trigger trigger = (Trigger) newCreatedElement;

		Package packageEvent = getEventPackage(action);

		newCreatedElement = createElement(packageEvent, UMLElementTypes.SIGNAL_EVENT);
		if (newCreatedElement == null || false == newCreatedElement instanceof SignalEvent) {
			return;
		}
		final SignalEvent signalEvent = (SignalEvent) newCreatedElement;

		setElementFeature(signalEvent, UMLPackage.eINSTANCE.getSignalEvent_Signal(), signal);
		setElementFeature(trigger, UMLPackage.eINSTANCE.getTrigger_Event(), signalEvent);
	}

	private Package getEventPackage(Element context) throws ExecutionException {
		Package ret = null;
		Model root = context.getModel();

		ret = root.getNestedPackage(EVENT_PACKAGE_NAME);
		if (ret == null) {
			EObject newElement = createElement(root, UMLElementTypes.PACKAGE);
			if (newElement == null || false == newElement instanceof Package) {
				return null;
			}
			ret = (Package) newElement;
			ret.setName(EVENT_PACKAGE_NAME);
		}
		return ret;
	}

	private EObject createElement(EObject owner, IElementType type) throws ExecutionException {
		TransactionalEditingDomain domain = (TransactionalEditingDomain) EMFHelper.resolveEditingDomain(targetEditPart);
		CreateElementRequest createElementRequest = new CreateElementRequest(domain, owner, type);
		ICommand creationElementCommand;

		creationElementCommand = new CreateElementCommand(createElementRequest);

		if (creationElementCommand.canExecute()) {
			creationElementCommand.execute(new NullProgressMonitor(), null);
		}

		CommandResult commandResult = creationElementCommand.getCommandResult();
		if (commandResult != null) {
			if (!commandResult.getStatus().isOK()) {
				return null;
			}
		}

		// Retrieve the created slot, and update its properties
		return commandResult.getReturnValue() instanceof EObject ? (EObject) commandResult.getReturnValue() : null;
	}

}

Back to the top