Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f7887f3568ea7bcbd0800bcf441cdaf15a912012 (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*****************************************************************************
 * Copyright (c) 2013 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.service.types.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.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.emf.type.core.commands.EditElementCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientReferenceRelationshipRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.ReorientRelationshipRequest;
import org.eclipse.papyrus.uml.service.types.utils.ConnectorUtils;
import org.eclipse.papyrus.uml.service.types.utils.NamedElementHelper;
import org.eclipse.uml2.uml.ConnectableElement;
import org.eclipse.uml2.uml.Connector;
import org.eclipse.uml2.uml.ConnectorEnd;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Port;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.StructuredClassifier;

/**
 * 
 * This command manage the connector reorient at the semantic level
 * 
 */
public class ConnectorReorientSemanticCommand extends EditElementCommand {

	/**
	 * the opposite part (which is not retargeted)
	 */
	private EObject oppositeEnd;

	/**
	 * the new part
	 */
	private EObject newEnd;

	/**
	 * the direction for the retarget
	 */
	protected final int reorientDirection;

	/**
	 * the new part with port
	 */
	private Property newPartWithPort;

	/**
	 * the opposite part with port (which is not changed here)
	 */
	private Property oppositePartWithPort;


	/**
	 * Constructor.
	 */
	public ConnectorReorientSemanticCommand(final ReorientRelationshipRequest request) {
		super(request.getLabel(), request.getRelationship(), request);
		this.reorientDirection = request.getDirection();
		this.newEnd = request.getNewRelationshipEnd();
		if(getElementToEdit() instanceof Connector) {
			this.oppositeEnd = (reorientDirection == ReorientRelationshipRequest.REORIENT_SOURCE) ? ((Connector)getElementToEdit()).getEnds().get(1).getRole() : ((Connector)getElementToEdit()).getEnds().get(0).getRole();
			this.oppositePartWithPort = (reorientDirection == ReorientRelationshipRequest.REORIENT_SOURCE) ? ((Connector)getElementToEdit()).getEnds().get(1).getPartWithPort() : ((Connector)getElementToEdit()).getEnds().get(0).getPartWithPort();
		} else {
			this.oppositeEnd = null;
			this.oppositePartWithPort = null;
		}
		initFields();
	}

	public ConnectorReorientSemanticCommand(final ReorientReferenceRelationshipRequest request) {
		super(request.getLabel(), null, request);
		this.reorientDirection = request.getDirection();
		this.newEnd = request.getNewRelationshipEnd();

		if(getElementToEdit() instanceof Connector) {
			this.oppositeEnd = (reorientDirection == ReorientRelationshipRequest.REORIENT_SOURCE) ? ((Connector)getElementToEdit()).getEnds().get(1).getRole() : ((Connector)getElementToEdit()).getEnds().get(0).getRole();
			this.oppositePartWithPort = (reorientDirection == ReorientRelationshipRequest.REORIENT_SOURCE) ? ((Connector)getElementToEdit()).getEnds().get(1).getPartWithPort() : ((Connector)getElementToEdit()).getEnds().get(0).getPartWithPort();
		} else {
			this.oppositeEnd = null;
			this.oppositePartWithPort = null;
		}
		initFields();
	}

	/**
	 * This method allows to init the fields which can't be initialized in the constructor
	 */
	protected void initFields() {
		this.newPartWithPort = (Property)getRequest().getParameter(ConnectorUtils.PART_WITH_PORT);
	}

	/**
	 * Get the link to re-orient.
	 * 
	 * @return the edited {@link Connector}
	 */
	protected Connector getLink() {
		return (Connector)getElementToEdit();
	}

	/**
	 * Test if the command can be executed.
	 */
	public boolean canExecute() {
		if(!(getElementToEdit() instanceof Connector)) {
			return false;
		}
		//TODO
		//in fact, in UML ends>2 is allowed, but it is forbidden in SysML
		if(getLink().getEnds().size() != 2) {
			return false;
		}

		return canReorient(newEnd, oppositeEnd);
	}

	/**
	 * 
	 * @param newRole
	 * @param oppositeRole
	 * @return
	 *         <code>true</code> if the newRole can be used as role for connector
	 */
	private boolean canReorient(final EObject newRole, final EObject oppositeRole) {
		//the new role must be a connectable element
		if(!(newRole instanceof ConnectableElement)) {
			return false;
		}

		if(newRole != null && oppositeRole != null) {
			//the 2 roles must be not be equals
			if(newRole == oppositeRole) {
				return false;
			}


			//UML Standart, p.181 : [3] The ConnectableElements attached as roles to each ConnectorEnd owned by a Connector must be roles
			//of the Classifier, that owned the Connector, or they must be ports of such roles. (p.181)
			//in SysML this rules is not applied, that's why we are testing the context of this action
			if(ConnectorUtils.applyUMLRulesForConnector(getLink())) {
				final StructuredClassifier newContainer = deduceParentConnector(getLink(), (ConnectableElement)oppositeRole, (ConnectableElement)newRole, this.oppositePartWithPort, this.newPartWithPort);
				if(ConnectorUtils.applyUMLRulesForConnector(getLink()) && !ConnectorUtils.getUMLPossibleRoles(newContainer).contains(newRole)) {
					return false;
				}
			}

		}
		return true;
	}

	/**
	 * 
	 * @see org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor,
	 *      org.eclipse.core.runtime.IAdaptable)
	 * 
	 * @param monitor
	 * @param info
	 * @return
	 * @throws ExecutionException
	 */
	protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
		if(!canExecute()) {
			throw new ExecutionException("Invalid arguments in reorient link command"); //$NON-NLS-1$
		}

		final ConnectorEnd editedConnectorEnd;

		if(reorientDirection == ReorientRelationshipRequest.REORIENT_SOURCE) {
			editedConnectorEnd = getLink().getEnds().get(0);
		} else if(reorientDirection == ReorientRelationshipRequest.REORIENT_TARGET) {
			editedConnectorEnd = getLink().getEnds().get(1);
		} else {
			editedConnectorEnd = null;
		}
		if(editedConnectorEnd != null) {
			reorientEnd(editedConnectorEnd, (ConnectableElement)newEnd, newPartWithPort, null);
			if(ConnectorUtils.applyUMLRulesForConnector(getLink())) {
				final StructuredClassifier newContainer = deduceParentConnector(getLink(), (ConnectableElement)this.oppositeEnd, (ConnectableElement)this.newEnd, this.oppositePartWithPort, this.newPartWithPort);
				replaceOwner(getLink(), newContainer);
			}
			return CommandResult.newOKCommandResult();
		}
		throw new IllegalStateException();
	}

	/**
	 * 
	 * @param end
	 * @param role
	 * @param partWithPort
	 * @param oppositePartWithPort
	 * @return
	 * @throws ExecutionException
	 */
	protected CommandResult reorientEnd(final ConnectorEnd end, final ConnectableElement role, final Property partWithPort, final Property oppositePartWithPort) throws ExecutionException {
		end.setRole(role);
		end.setPartWithPort(partWithPort);
		//		oppositeEnd.setPartWithPort(oppositePartWithPort);
		return CommandResult.newOKCommandResult();
	}



	/**
	 * 
	 * @param newPartWithPort
	 */
	public void setNewPartWithPort(final Property newPartWithPort) {
		this.newPartWithPort = newPartWithPort;
	}

	/**
	 * 
	 * @param connector
	 *        the connector
	 * @param newOwner
	 *        the new owner for the connector
	 */
	protected void replaceOwner(final Connector connector, final StructuredClassifier newOwner) {
		// Change owner and Connector name (possibly already exists in new container)
		if(newOwner != connector.getOwner()) {
			if(newOwner.getOwnedConnector(connector.getName()) != null) {
				String replacementName = NamedElementHelper.getDefaultNameWithIncrementFromBase("connector", newOwner.eContents()); // //$NON-NLS-0$
				connector.setName(replacementName);
			}
			// Replace connector owner
			newOwner.getOwnedConnectors().add(connector);
		}
	}

	/**
	 * 
	 * @param connector
	 *        the edited connector
	 * @param role1
	 *        a role of this connector
	 * @param role2
	 *        the 2nd role for this connector
	 * @param partWithPort1
	 *        the part with port for the first role (could be <code>null</code>
	 * @param partWithPort2
	 *        the part with port for the second role (could be <code>null</code>
	 * @return
	 *         the new parent for the connector
	 */
	protected StructuredClassifier deduceParentConnector(final Connector connector, final ConnectableElement role1, final ConnectableElement role2, final Property partWithPort1, final Property partWithPort2) {
		final Element owner1 = role1.getOwner();
		final Element owner2 = role2.getOwner();
		if(owner1 == owner2 && owner1 instanceof StructuredClassifier) {
			return (StructuredClassifier)owner1;
		}
		if(role1 instanceof Port && role2 instanceof Port) {
			final StructuredClassifier partOwner1 = (StructuredClassifier)partWithPort1.getOwner();
			final StructuredClassifier partOwner2 = (StructuredClassifier)partWithPort2.getOwner();
			if(partOwner2.getOwnedElements().contains(partWithPort1)) {
				return partOwner2;
			}
			return partOwner1;
		}
		return (StructuredClassifier)connector.getOwner();
	}

	/**
	 * Setter for {@link #oppositeEnd}
	 * 
	 * @param oppositeEnd
	 *        the opposite end
	 */
	public final void setOppositeEnd(final EObject oppositeEnd) {
		this.oppositeEnd = oppositeEnd;
	}

	/**
	 * Setter for {@link #newEnd}
	 * 
	 * @param newEnd
	 *        the new end
	 */
	public final void setNewEnd(final EObject newEnd) {
		this.newEnd = newEnd;
	}

	/**
	 * Setter for {@link #oppositePartWithPort}
	 * 
	 * @param oppositePartWithPort
	 *        the opposite part with port
	 */
	public final void setOppositePartWithPort(Property oppositePartWithPort) {
		this.oppositePartWithPort = oppositePartWithPort;
	}

	/**
	 * @return the oppositeEnd
	 */
	public EObject getOppositeEnd() {
		return oppositeEnd;
	}


	/**
	 * @return the newEnd
	 */
	public EObject getNewEnd() {
		return newEnd;
	}


	/**
	 * @return the newPartWithPort
	 */
	protected Property getNewPartWithPort() {
		return newPartWithPort;
	}


	/**
	 * @return the oppositePartWithPort
	 */
	protected Property getOppositePartWithPort() {
		return oppositePartWithPort;
	}
}

Back to the top