|
Lines 13-34
Link Here
|
| 13 |
*****************************************************************************/ |
13 |
*****************************************************************************/ |
| 14 |
package org.eclipse.papyrus.sysml.service.types.helper.advice; |
14 |
package org.eclipse.papyrus.sysml.service.types.helper.advice; |
| 15 |
|
15 |
|
|
|
16 |
import java.util.List; |
| 17 |
|
| 16 |
import org.eclipse.core.commands.ExecutionException; |
18 |
import org.eclipse.core.commands.ExecutionException; |
| 17 |
import org.eclipse.core.runtime.IAdaptable; |
19 |
import org.eclipse.core.runtime.IAdaptable; |
| 18 |
import org.eclipse.core.runtime.IProgressMonitor; |
20 |
import org.eclipse.core.runtime.IProgressMonitor; |
|
|
21 |
import org.eclipse.emf.transaction.TransactionalEditingDomain; |
| 19 |
import org.eclipse.gmf.runtime.common.core.command.CommandResult; |
22 |
import org.eclipse.gmf.runtime.common.core.command.CommandResult; |
| 20 |
import org.eclipse.gmf.runtime.common.core.command.ICommand; |
23 |
import org.eclipse.gmf.runtime.common.core.command.ICommand; |
| 21 |
import org.eclipse.gmf.runtime.common.core.command.UnexecutableCommand; |
24 |
import org.eclipse.gmf.runtime.common.core.command.UnexecutableCommand; |
| 22 |
import org.eclipse.gmf.runtime.emf.type.core.commands.ConfigureElementCommand; |
25 |
import org.eclipse.gmf.runtime.emf.type.core.commands.ConfigureElementCommand; |
| 23 |
import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest; |
26 |
import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest; |
| 24 |
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest; |
27 |
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest; |
|
|
28 |
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest; |
| 29 |
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils; |
| 30 |
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService; |
| 25 |
import org.eclipse.papyrus.sysml.service.types.Activator; |
31 |
import org.eclipse.papyrus.sysml.service.types.Activator; |
| 26 |
import org.eclipse.papyrus.uml.service.types.utils.ClassifierUtils; |
32 |
import org.eclipse.papyrus.uml.service.types.utils.ClassifierUtils; |
|
|
33 |
import org.eclipse.papyrus.uml.service.types.utils.NamedElementHelper; |
| 27 |
import org.eclipse.uml2.uml.Association; |
34 |
import org.eclipse.uml2.uml.Association; |
| 28 |
import org.eclipse.uml2.uml.Classifier; |
35 |
import org.eclipse.uml2.uml.Classifier; |
|
|
36 |
import org.eclipse.uml2.uml.Element; |
| 29 |
import org.eclipse.uml2.uml.Property; |
37 |
import org.eclipse.uml2.uml.Property; |
| 30 |
import org.eclipse.uml2.uml.Type; |
38 |
import org.eclipse.uml2.uml.Type; |
| 31 |
import org.eclipse.uml2.uml.UMLFactory; |
39 |
import org.eclipse.uml2.uml.UMLFactory; |
|
|
40 |
import org.eclipse.uml2.uml.UMLPackage; |
| 32 |
|
41 |
|
| 33 |
/** |
42 |
/** |
| 34 |
* Edit helper advice for {@link Association} with "aggregation = none" (used for creation purpose only). |
43 |
* Edit helper advice for {@link Association} with "aggregation = none" (used for creation purpose only). |
|
Lines 66-104
Link Here
|
| 66 |
} |
75 |
} |
| 67 |
|
76 |
|
| 68 |
/** |
77 |
/** |
| 69 |
* Creates a new source {@link Property} from the targetType. |
78 |
* Creates a new {@link Property} from the propertyType in the propertyContainer |
| 70 |
* |
79 |
* |
| 71 |
* @param targetType |
80 |
* @param propertyContainer |
|
|
81 |
* the container of the {@link Property} |
| 82 |
* @param propertyType |
| 72 |
* the type of the {@link Property} |
83 |
* the type of the {@link Property} |
| 73 |
* @return the new {@link Property} |
84 |
* @return the new {@link Property} |
| 74 |
*/ |
85 |
* @throws ExecutionException |
| 75 |
protected Property createSourceProperty(Type targetType) { |
86 |
*/ |
| 76 |
|
87 |
protected Property createTargetProperty(Property targetProperty, Classifier propertyContainer, Type propertyType, Association association, TransactionalEditingDomain editingDomain, IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { |
| 77 |
Property sourceProperty = UMLFactory.eINSTANCE.createProperty(); |
88 |
addSourceInModel(targetProperty, propertyContainer, propertyType, association); |
| 78 |
sourceProperty.setType(targetType); |
89 |
setPropertyType(targetProperty, propertyType, editingDomain, progressMonitor, info); |
| 79 |
sourceProperty.setName(targetType.getName().toLowerCase()); |
90 |
setPropertyName(targetProperty); |
| 80 |
|
|
|
| 81 |
return sourceProperty; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Creates a new target {@link Property} from the sourceType. |
| 86 |
* |
| 87 |
* @param sourceType |
| 88 |
* the type of the {@link Property} |
| 89 |
* @return the new {@link Property} |
| 90 |
*/ |
| 91 |
protected Property createTargetProperty(Type sourceType) { |
| 92 |
|
| 93 |
Property targetProperty = UMLFactory.eINSTANCE.createProperty(); |
| 94 |
targetProperty.setType(sourceType); |
| 95 |
targetProperty.setName(sourceType.getName().toLowerCase()); |
| 96 |
|
| 97 |
return targetProperty; |
91 |
return targetProperty; |
| 98 |
} |
92 |
} |
|
|
93 |
|
| 94 |
protected Property createSourceProperty(Property sourceProperty, Classifier propertyContainer, Type propertyType, Association association, TransactionalEditingDomain editingDomain, IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { |
| 95 |
addTargetInModel(sourceProperty, propertyContainer, propertyType, association); |
| 96 |
setPropertyType(sourceProperty, propertyType, editingDomain, progressMonitor, info); |
| 97 |
setPropertyName(sourceProperty); |
| 98 |
return sourceProperty; |
| 99 |
} |
| 100 |
|
| 101 |
protected void setPropertyType(Property property, Type propertyType, TransactionalEditingDomain editingDomain, IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { |
| 102 |
// Set type using all AdviceHelper (use ServiceEdit instead of manually set) |
| 103 |
SetRequest request = new SetRequest(property, UMLPackage.eINSTANCE.getTypedElement_Type(), propertyType); |
| 104 |
request.setEditingDomain(editingDomain); |
| 105 |
IElementEditService commandProvider = ElementEditServiceUtils.getCommandProvider(property); |
| 106 |
ICommand editCommand = commandProvider.getEditCommand(request); |
| 107 |
editCommand.execute(progressMonitor, info); |
| 108 |
} |
| 109 |
|
| 110 |
private void setPropertyName(Property property) { |
| 111 |
String baseName = property.getType().getName().toLowerCase(); |
| 112 |
Element owner = property.getOwner(); |
| 113 |
if (owner instanceof Classifier) { |
| 114 |
List<Property> ownedAttributes = ClassifierUtils.getOwnedAttributes((Classifier)owner); |
| 115 |
String defaultNameWithIncrementFromBase = NamedElementHelper.getDefaultNameWithIncrementFromBase( |
| 116 |
property.getType().getName().toLowerCase(), ownedAttributes, "_"); |
| 117 |
property.setName(defaultNameWithIncrementFromBase); |
| 118 |
} |
| 119 |
else { |
| 120 |
// default |
| 121 |
property.setName(baseName); |
| 122 |
} |
| 123 |
} |
| 99 |
|
124 |
|
| 100 |
/** |
125 |
/** |
| 101 |
* Add the source {@link Property} in the correct container. |
126 |
* This method has to be specialized by subclasses (AggregationKind) |
|
|
127 |
* @param sourceProperty |
| 128 |
* The property to configure |
| 129 |
*/ |
| 130 |
protected void configureSourceProperty(Property sourceProperty) { |
| 131 |
// do nothing |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* This method has to be specialized by subclasses (AggregationKind) |
| 136 |
* @param sourceProperty |
| 137 |
* The property to configure |
| 138 |
*/ |
| 139 |
protected void configureTargetProperty(Property targetProperty) { |
| 140 |
// do nothing |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* This method has to be specialized by subclasses (owner) |
| 102 |
* |
145 |
* |
| 103 |
* @param sourceEnd |
146 |
* @param sourceEnd |
| 104 |
* the semantic end |
147 |
* the semantic end |
|
Lines 110-125
Link Here
|
| 110 |
* the association |
153 |
* the association |
| 111 |
* @throws UnsupportedOperationException |
154 |
* @throws UnsupportedOperationException |
| 112 |
*/ |
155 |
*/ |
| 113 |
protected void addSourceInModel(final Property sourceEnd, Classifier owner, Classifier targetType, Association association) throws UnsupportedOperationException { |
156 |
protected void addSourceInModel(final Property sourceEnd, Classifier owner, Type targetType, Association association) throws UnsupportedOperationException { |
| 114 |
boolean added = ClassifierUtils.addOwnedAttribute(owner, sourceEnd); |
157 |
// set the container in order to allow Stereotype appliance |
| 115 |
|
158 |
boolean added = ClassifierUtils.addOwnedAttribute(owner, sourceEnd); |
| 116 |
if(!added) { |
159 |
if(!added) { |
| 117 |
throw new UnsupportedOperationException("Cannot add a Property on Classifier " + owner.getQualifiedName()); |
160 |
throw new UnsupportedOperationException("Cannot add a Property on Classifier " + owner.getQualifiedName()); |
| 118 |
} |
161 |
} |
| 119 |
} |
162 |
} |
| 120 |
|
163 |
|
| 121 |
/** |
164 |
/** |
| 122 |
* Add the source {@link Property} in the correct container. |
165 |
* This method has to be specialized by subclasses (owner) |
| 123 |
* |
166 |
* |
| 124 |
* @param targetEnd |
167 |
* @param targetEnd |
| 125 |
* the semantic end |
168 |
* the semantic end |
|
Lines 131-139
Link Here
|
| 131 |
* the association |
174 |
* the association |
| 132 |
* @throws UnsupportedOperationException |
175 |
* @throws UnsupportedOperationException |
| 133 |
*/ |
176 |
*/ |
| 134 |
protected void addTargetInModel(Property targetEnd, Classifier owner, Classifier sourceType, Association association) { |
177 |
protected void addTargetInModel(Property targetEnd, Classifier owner, Type sourceType, Association association) { |
| 135 |
boolean added = ClassifierUtils.addOwnedAttribute(owner, targetEnd); |
178 |
// set the container in order to allow Stereotype appliance |
| 136 |
|
179 |
boolean added = ClassifierUtils.addOwnedAttribute(owner, targetEnd); |
| 137 |
if(!added) { |
180 |
if(!added) { |
| 138 |
throw new UnsupportedOperationException("Cannot add a Property on Classifier " + owner.getQualifiedName()); |
181 |
throw new UnsupportedOperationException("Cannot add a Property on Classifier " + owner.getQualifiedName()); |
| 139 |
} |
182 |
} |
|
Lines 162-179
Link Here
|
| 162 |
|
205 |
|
| 163 |
protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { |
206 |
protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { |
| 164 |
|
207 |
|
| 165 |
// Create source and target ends |
|
|
| 166 |
Property sourceEnd = createSourceProperty(targetType); |
| 167 |
Property targetEnd = createTargetProperty(sourceType); |
| 168 |
|
| 169 |
// Add association ends references |
| 170 |
association.getMemberEnds().add(sourceEnd); |
| 171 |
association.getMemberEnds().add(targetEnd); |
| 172 |
|
| 173 |
// Add end properties in the model |
| 174 |
try { |
208 |
try { |
| 175 |
addSourceInModel(sourceEnd, sourceType, targetType, association); |
209 |
Property targetProperty = UMLFactory.eINSTANCE.createProperty(); |
| 176 |
addTargetInModel(targetEnd, targetType, sourceType, association); |
210 |
Property sourceProperty = UMLFactory.eINSTANCE.createProperty(); |
|
|
211 |
|
| 212 |
// TODO: problem with SysML. Link are inversed. A -> B => memberEnd={a, b} instead of {b, a}. |
| 213 |
// Problem seems to come from cached derivedFeature /endTypes |
| 214 |
// So we force to set memberEnd in this order before doing anything with the created properties |
| 215 |
association.getMemberEnds().add(targetProperty); |
| 216 |
association.getMemberEnds().add(sourceProperty); |
| 217 |
|
| 218 |
// Create source and target ends |
| 219 |
createTargetProperty(targetProperty, sourceType, targetType, association, request.getEditingDomain(), progressMonitor, info); |
| 220 |
configureSourceProperty(targetProperty); |
| 221 |
createSourceProperty(sourceProperty, targetType, sourceType, association, request.getEditingDomain(), progressMonitor, info); |
| 222 |
configureTargetProperty(sourceProperty); |
| 223 |
|
| 177 |
} catch (Exception e) { |
224 |
} catch (Exception e) { |
| 178 |
Activator.log.error(e); |
225 |
Activator.log.error(e); |
| 179 |
return CommandResult.newCancelledCommandResult(); |
226 |
return CommandResult.newCancelledCommandResult(); |