Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 425c09c1436291dd3f91a71c9909574030b4258a (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
268
269
270
271
272
273
274
275
276
277
278
279
280
/*****************************************************************************
 * Copyright (c) 2009-2011 CEA LIST.
 *
 *    
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Yann Tanguy (CEA LIST) yann.tanguy@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.component.custom.edit.command;

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.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.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest;
import org.eclipse.papyrus.uml.diagram.component.custom.edit.policies.GraphicalNodeEditPolicy;
import org.eclipse.papyrus.uml.diagram.component.part.Messages;
import org.eclipse.papyrus.uml.diagram.component.providers.ElementInitializers;
import org.eclipse.uml2.uml.ConnectableElement;
import org.eclipse.uml2.uml.Connector;
import org.eclipse.uml2.uml.ConnectorEnd;
import org.eclipse.uml2.uml.Port;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.StructuredClassifier;
import org.eclipse.uml2.uml.UMLFactory;

/**
 * <pre>
 * This class is a custom creation command for Connector.
 * 
 * Difficulties for Connector creation:
 * - differences between semantic (ConnectableEnd) graphical (ConnectableElement) ends of Connector 
 * - requires to know not only the graphical end (Port, Property) but also the graphical parent in case of Port.
 * 
 * The Connector source and target are declared as ConnectorEnd in the GMFGEN model to ensure correct validation,
 * but the real source and target are ConnectableElement.
 * 
 * </pre>
 */
public class ConnectorCreateCommand extends org.eclipse.papyrus.uml.diagram.component.edit.commands.ConnectorCreateCommand {

	/** Graphical parent of Source **/
	protected Property sourcePartWithPort = null;

	/** Graphical parent of Target **/
	protected Property targetPartWithPort = null;

	/** Graphical EditPart of Source **/
	protected GraphicalEditPart sourceGraphicalEditPart = null;

	/** Graphical EditPart of Target **/
	protected GraphicalEditPart targetGraphicalEditPart = null;

	/**
	 * Constructor of Connector custom creation command
	 * 
	 * @param req
	 *        the creation request
	 * @param source
	 *        the connector source element
	 * @param target
	 *        the connector target element
	 */
	public ConnectorCreateCommand(CreateRelationshipRequest req, EObject source, EObject target) {

		super(req, source, target);

		// Resolve graphical parents of source and target store in request as Parameters
		// These parameters are added in request by (custom) GraphicalNodeEditPolicy
		if(req.getParameter(GraphicalNodeEditPolicy.CONNECTOR_CREATE_REQUEST_SOURCE_PARENT) instanceof Property) {
			sourcePartWithPort = (Property)req.getParameter(GraphicalNodeEditPolicy.CONNECTOR_CREATE_REQUEST_SOURCE_PARENT);
		}

		if(req.getParameter(GraphicalNodeEditPolicy.CONNECTOR_CREATE_REQUEST_TARGET_PARENT) instanceof Property) {
			targetPartWithPort = (Property)req.getParameter(GraphicalNodeEditPolicy.CONNECTOR_CREATE_REQUEST_TARGET_PARENT);
		}

		if(req.getParameter(GraphicalNodeEditPolicy.CONNECTOR_CREATE_REQUEST_SOURCE_GRAPHICAL) instanceof GraphicalEditPart) {
			sourceGraphicalEditPart = (GraphicalEditPart)req.getParameter(GraphicalNodeEditPolicy.CONNECTOR_CREATE_REQUEST_SOURCE_GRAPHICAL);
		}

		if(req.getParameter(GraphicalNodeEditPolicy.CONNECTOR_CREATE_REQUEST_TARGET_GRAPHICAL) instanceof GraphicalEditPart) {
			targetGraphicalEditPart = (GraphicalEditPart)req.getParameter(GraphicalNodeEditPolicy.CONNECTOR_CREATE_REQUEST_TARGET_GRAPHICAL);
		}

		container = deduceContainer(_getSource(), _getTarget());
	}

	/**
	 * Replaces the original getter which is cast as ConnectorEnd (expected end of Connector)
	 * 
	 * @return the element that is graphically connected to Connector as source
	 */
	protected ConnectableElement _getSource() {
		return (ConnectableElement)source;
	}

	/**
	 * Replaces the original getter which is cast as ConnectorEnd (expected end of Connector)
	 * 
	 * @return the element that is graphically connected to Connector as target
	 */
	protected ConnectableElement _getTarget() {
		return (ConnectableElement)target;
	}

	/**
	 * <pre>
	 * Checks if the Connector can be created or not.
	 * 
	 * {@inheritDoc}
	 * </pre>
	 */
	@Override
	public boolean canExecute() {
		if((source == null) && (target == null)) {
			return false;
		}
		if((source != null) && !(source instanceof ConnectableElement)) {
			return false;
		}
		if((target != null) && !(target instanceof ConnectableElement)) {
			return false;
		}
		if(source == null) {
			return true; // link creation is in progress; source is not defined yet
		}

		if((target != null) && (getContainer() == null)) {
			return false;
		}
		
		// return
		// UMLBaseItemSemanticEditPolicy.LinkConstraints.canCreateConnector_4013(getContainer(),
		// _getSource(),
		// _getTarget());
		// this returns true anyway...
		return true;
	}

	/**
	 * <pre>
	 * Creates the new Connector.
	 * 
	 * {@inheritDoc}
	 * </pre>
	 */
	@Override
	protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		if(!canExecute()) {
			throw new ExecutionException("Invalid arguments in create link command");
		}
		Connector newElement = UMLFactory.eINSTANCE.createConnector();
		getContainer().getOwnedConnectors().add(newElement);

		ConnectorEnd end0 = UMLFactory.eINSTANCE.createConnectorEnd();
		end0.setLower(1);
		end0.setUpper(1);
		if(_getSource() instanceof Port) {
			end0.setRole((Port)_getSource());
			end0.setPartWithPort(sourcePartWithPort);

		} else if(_getSource() instanceof ConnectableElement) {
			end0.setRole((ConnectableElement)_getSource());

		} else {
			throw new ExecutionException("Invalid source in create Connector command");
		}

		ConnectorEnd end1 = UMLFactory.eINSTANCE.createConnectorEnd();
		end1.setLower(1);
		end1.setUpper(1);
		if(_getTarget() instanceof Port) {
			end1.setRole((Port)_getTarget());
			end1.setPartWithPort(targetPartWithPort);

		} else if(_getTarget() instanceof ConnectableElement) {
			end1.setRole((ConnectableElement)_getTarget());

		} else {
			throw new ExecutionException("Invalid target in create Connector command");
		}

		newElement.getEnds().add(end0);
		newElement.getEnds().add(end1);

		ElementInitializers.getInstance().init_Connector_4019(newElement);

		doConfigure(newElement, monitor, info);
		((CreateElementRequest)getRequest()).setNewElement(newElement);

		return CommandResult.newOKCommandResult(newElement);
	}

	/**
	 * <pre>
	 * This method is the exact copy of the inherited method, except that it uses the local getters
	 * (_getSource(), _getTarget()). 
	 * 
	 * {@inheritDoc}
	 * </pre>
	 */
	@Override
	protected void doConfigure(Connector newElement, IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		IElementType elementType = ((CreateElementRequest)getRequest()).getElementType();
		ConfigureRequest configureRequest = new ConfigureRequest(getEditingDomain(), newElement, elementType);
		configureRequest.setClientContext(((CreateElementRequest)getRequest()).getClientContext());
		configureRequest.addParameters(getRequest().getParameters());
		configureRequest.setParameter(CreateRelationshipRequest.SOURCE, _getSource());
		configureRequest.setParameter(CreateRelationshipRequest.TARGET, _getTarget());
		ICommand configureCommand = elementType.getEditCommand(configureRequest);
		if(configureCommand != null && configureCommand.canExecute()) {
			configureCommand.execute(monitor, info);
		}
	}

	/**
	 * 
	 * Tries to find a common StructuredClassifier container to add the new Connector.
	 * 
	 * @see org.eclipse.papyrus.uml.diagram.composite.edit.commands.ConnectorCreateCommand#deduceContainer(org.eclipse.emf.ecore.EObject,
	 *      org.eclipse.emf.ecore.EObject)
	 * 
	 * @param source
	 *        the source object
	 * @param target
	 *        the target object
	 * @return a common StructuredClassifier container (graphical search)
	 */
	@Override
	protected StructuredClassifier deduceContainer(EObject source, EObject target) {

		StructuredClassifier containerProposedBySource = proposedContainer(sourceGraphicalEditPart);
		StructuredClassifier containerProposedByTarget = proposedContainer(targetGraphicalEditPart);
		
		StructuredClassifier deducedContainer = null;

		if((containerProposedBySource != null) && (containerProposedByTarget != null)) {
			if(containerProposedBySource == containerProposedByTarget) {
				deducedContainer = containerProposedBySource;
			}
		}

		return deducedContainer;
	}

	/**
	 * Parse graphical parent of the graphicalEditPart until a StructureClassifier is found.
	 * 
	 * @param graphicalEditPart
	 *        the graphical edit part
	 * @return null or a StructuredClassifier that graphically contains the graphicalEditPart
	 */
	private StructuredClassifier proposedContainer(GraphicalEditPart graphicalEditPart) {

		for(EditPart ep = graphicalEditPart; ep != null; ep = ep.getParent()) {
			if(ep instanceof GraphicalEditPart) {
				GraphicalEditPart parent = (GraphicalEditPart)ep;
				EObject semanticParent = parent.resolveSemanticElement();
				if(semanticParent instanceof StructuredClassifier) {
					return (StructuredClassifier)semanticParent;
				}
			}
		}

		return null;
	}
}

Back to the top