Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 55113aa318b0dbda342c3fb7b55f10d49bfbd8b4 (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
/*****************************************************************************
 * Copyright (c) 2015 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:
 *  Francois Le Fevre (CEA LIST) francois.le-fevre@cea.fr - Initial API and implementation
 *  Pauline DEVILLE (CEA LIST) pauline.deville@cea.fr - Bug 546413
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.activity.dnd.commands;

import java.util.Iterator;
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.core.runtime.NullProgressMonitor;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.common.core.command.AbstractCommand;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.diagram.core.edithelpers.CreateElementRequestAdapter;
import org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest.ViewDescriptor;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.IHintedType;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
import org.eclipse.papyrus.uml.diagram.activity.edit.parts.ActivityActivityContentCompartmentEditPart;

/**
 * A class creating a new element of type T
 * when drop an element of type S in an element of type E.
 * This class also update the newly created element to set
 * one of its feature with the dropped element.
 * 
 * @since 3.5.0
 */
public class CreateTAndUpdateCommand<T extends EObject, E extends EObject, S extends EObject> extends AbstractCommand {

	protected final EditPart targetEditPart;
	protected final boolean headless;
	protected final Class<T> typeParameterClass;
	protected final E targetElement;
	protected final S sourceElement;
	private final IHintedType typeToCreate;
	private final Point location;
	private String dropEditPartVisualID;
	private EStructuralFeature structuralFeatureToSet;

	public CreateTAndUpdateCommand(EditPart targetEditPart, Class<T> typeParameterClass, E targetElement, S sourceElement, boolean headless, IHintedType typeToCreate, EStructuralFeature structuralFeatureToSet, Point location,
			String dropEditPartVisualID) {
		super("Create and update in drop action"); //$NON-NLS-1$

		this.targetEditPart = targetEditPart;
		this.headless = headless;
		this.typeParameterClass = typeParameterClass;

		this.targetElement = targetElement;
		this.sourceElement = sourceElement;
		this.typeToCreate = typeToCreate;
		this.location = location;
		this.dropEditPartVisualID = dropEditPartVisualID;
		this.structuralFeatureToSet = structuralFeatureToSet;
	}

	@SuppressWarnings("unchecked")
	protected T getNewEObject(CreateElementRequest request) {
		if (typeParameterClass.isAssignableFrom(request.getNewElement().getClass())) {
			return (T) request.getNewElement();
		}
		return null;
	}

	/**
	 * Retrieves the new object from a CommandResult
	 *
	 * @param commandResult
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public T getNewEObject(CommandResult commandResult) {
		Object objectResult = commandResult.getReturnValue();
		if (objectResult instanceof List) {
			// Result of the semantic + graphical creation command
			List<?> listResult = (List<?>) objectResult;
			for (Object elementResult : listResult) {
				if (elementResult instanceof CreateElementRequestAdapter) {
					CreateElementRequest request = (CreateElementRequest) ((CreateElementRequestAdapter) elementResult).getAdapter(CreateElementRequest.class);
					if (request != null) {
						EObject newElement = request.getNewElement();
						if (typeParameterClass.isAssignableFrom(newElement.getClass())) {
							T newObject = (T) newElement;
							return newObject;
						}
					}
				}
			}
		} else if (typeParameterClass.isAssignableFrom(commandResult.getReturnValue().getClass())) {
			// Result of the semantic creation command
			return (T) commandResult.getReturnValue();
		}

		return null;
	}


	/**
	 * @see org.eclipse.gmf.runtime.common.core.command.AbstractCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
	 *
	 * @param progressMonitor
	 * @param info
	 * @return
	 * @throws ExecutionException
	 */
	@Override
	protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
		// Creates the slot
		TransactionalEditingDomain domain = (TransactionalEditingDomain) EMFHelper.resolveEditingDomain(targetEditPart);
		CreateElementRequest createElementRequest = new CreateElementRequest(domain, targetElement, typeToCreate);
		ICommand creationElementCommand = null;

		// creationElementCommand = new CreateElementCommand(createElementRequest);
		IElementEditService provider = ElementEditServiceUtils.getCommandProvider(targetElement);
		if (provider != null) {
			creationElementCommand = provider.getEditCommand(createElementRequest);
		}
		if (creationElementCommand != null) {
			creationElementCommand.execute(new NullProgressMonitor(), null);

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

			// Retrieve the created slot, and update its properties
			T newCreatedElement = getNewEObject(commandResult);
			if (newCreatedElement == null) {
				newCreatedElement = getNewEObject(createElementRequest);
			}

			if (newCreatedElement != null) {
				updateNewlyCreatedEObjectWithEObjectDragged(newCreatedElement, sourceElement);
			} else {
				return CommandResult.newErrorCommandResult("Could not create the action"); //$NON-NLS-1$
			}

			// Create the element graphically if possible
			final EditPart ownerEditPart = getOwnerEditPart();
			dropCreatedElement(ownerEditPart, newCreatedElement);
		}
		return CommandResult.newOKCommandResult();
	}

	/**
	 * @param contentCompartmentEditPart
	 * @param createdElement
	 */
	protected void dropCreatedElement(EditPart contentCompartmentEditPart, T createdElement) {
		// If the content compartment edit part of the activity is visible, add the created Action
		if (null != contentCompartmentEditPart) {
			// Create the view request, its associated command and execute it
			final ViewDescriptor descriptor = new ViewDescriptor(new EObjectAdapter(createdElement), Node.class, dropEditPartVisualID, ((IGraphicalEditPart) targetEditPart).getDiagramPreferencesHint());
			final CreateViewRequest createViewRequest = new CreateViewRequest(descriptor);
			createViewRequest.setLocation(location);
			final Command createViewCommand = contentCompartmentEditPart.getCommand(createViewRequest);
			if (null != createViewCommand && createViewCommand.canExecute()) {
				createViewCommand.execute();
			}
		}
	}

	/**
	 *
	 */
	protected EditPart getOwnerEditPart() {
		// Get the compartment edit part
		CompartmentEditPart compartmentEditPart = null;
		if (targetEditPart instanceof ActivityActivityContentCompartmentEditPart) {
			compartmentEditPart = (ActivityActivityContentCompartmentEditPart) targetEditPart;
		} else {
			Iterator<?> children = targetEditPart.getChildren().iterator();
			while (children.hasNext() && null == compartmentEditPart) {
				Object child = children.next();
				if (child instanceof ActivityActivityContentCompartmentEditPart) {
					compartmentEditPart = (ActivityActivityContentCompartmentEditPart) child;
				}
			}
		}
		return compartmentEditPart;
	}

	/**
	 * Sets the slot's property (definingFeature) and initialize its value (property#defaultValue)
	 *
	 * @param elementToSet
	 * @param value
	 * @throws ExecutionException
	 */
	protected void updateNewlyCreatedEObjectWithEObjectDragged(T elementToSet, S value) throws ExecutionException {
		setElementFeature(elementToSet, structuralFeatureToSet, value);
	}

	/**
	 * Sets the feature of the elementToSet to the value
	 *
	 * @param elementToSet
	 * @param structuralFeature
	 * @param value
	 * @throws ExecutionException
	 */
	protected void setElementFeature(EObject elementToSet, EStructuralFeature structuralFeature, Object value) throws ExecutionException {
		SetRequest setFeatureRequest = new SetRequest(elementToSet, structuralFeature, value);
		IElementEditService provider = ElementEditServiceUtils.getCommandProvider(elementToSet);
		if (provider != null) {
			ICommand setCommand = provider.getEditCommand(setFeatureRequest);
			if (setCommand != null && setCommand.canExecute()) {
				setCommand.execute(new NullProgressMonitor(), null);
			}
		}
	}

	/**
	 * @see org.eclipse.gmf.runtime.common.core.command.AbstractCommand#doRedoWithResult(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
	 *
	 * @param progressMonitor
	 * @param info
	 * @return
	 * @throws ExecutionException
	 */
	@Override
	protected CommandResult doRedoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
		throw new ExecutionException("not implemented"); //$NON-NLS-1$
	}

	/**
	 * @see org.eclipse.gmf.runtime.common.core.command.AbstractCommand#doUndoWithResult(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
	 *
	 * @param progressMonitor
	 * @param info
	 * @return
	 * @throws ExecutionException
	 */
	@Override
	protected CommandResult doUndoWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException {
		throw new ExecutionException("not implemented"); //$NON-NLS-1$
	}

}

Back to the top