use of org.eclipse.sirius.ecore.extender.business.api.accessor.EcoreMetamodelDescriptor in project sirius-components by eclipse-sirius.
the class CreateInstanceOperationHandler method handle.
@Override
public IStatus handle(Map<String, Object> variables) {
String typeName = this.createInstance.getTypeName();
String referenceName = this.createInstance.getReferenceName();
String variableName = this.createInstance.getVariableName();
Map<String, Object> childVariables = new HashMap<>(variables);
if (typeName != null && !typeName.isBlank() && referenceName != null && !referenceName.isBlank()) {
// @formatter:off
Optional<EObject> optionalOwnerEObject = Optional.ofNullable(variables.get(VariableManager.SELF)).filter(EObject.class::isInstance).map(EObject.class::cast);
Optional<EditingDomain> optionalEditingDomain = Optional.of(variables.get(IEditingContext.EDITING_CONTEXT)).filter(EditingContext.class::isInstance).map(EditingContext.class::cast).map(EditingContext::getDomain);
// @formatter:on
Matcher matcher = SEPARATOR.matcher(typeName);
if (optionalEditingDomain.isPresent() && optionalOwnerEObject.isPresent() && matcher.find()) {
EditingDomain editingDomain = optionalEditingDomain.get();
EObject ownerEObject = optionalOwnerEObject.get();
String packageName = typeName.substring(0, matcher.start());
String className = typeName.substring(matcher.end());
Optional<EPackage> optionalEPackage = this.ePackageService.findEPackage(editingDomain.getResourceSet().getPackageRegistry(), packageName);
EcoreIntrinsicExtender ecoreIntrinsicExtender = new EcoreIntrinsicExtender();
if (optionalEPackage.isPresent()) {
EPackage ePackage = optionalEPackage.get();
ecoreIntrinsicExtender.updateMetamodels(Collections.singleton(new EcoreMetamodelDescriptor(ePackage)));
}
EObject createdInstance = ecoreIntrinsicExtender.createInstance(className);
if (createdInstance == null) {
// $NON-NLS-1$
this.logger.warn("The creation of an instance of Type {} failed.", typeName);
} else {
Object addedObject = ecoreIntrinsicExtender.eAdd(ownerEObject, referenceName, createdInstance);
if (addedObject == null) {
// $NON-NLS-1$
this.logger.warn("The feature {} does not exist on {}.", referenceName, typeName);
} else if (variableName != null && !variableName.isBlank()) {
childVariables.put(variableName, createdInstance);
}
// Sub-operations are executed in the context of the new element
childVariables.put(VariableManager.SELF, createdInstance);
}
}
}
List<ModelOperation> subModelOperations = this.createInstance.getSubModelOperations();
return this.childModelOperationHandler.handle(this.objectService, this.representationMetadataSearchService, this.identifierProvider, this.interpreter, childVariables, subModelOperations);
}
use of org.eclipse.sirius.ecore.extender.business.api.accessor.EcoreMetamodelDescriptor in project sirius-components by eclipse-sirius.
the class ToolImageProvider method getInstance.
private Optional<EObject> getInstance(String domainClass) {
Matcher matcher = SEPARATOR.matcher(domainClass);
if (matcher.find()) {
String packageName = domainClass.substring(0, matcher.start());
String className = domainClass.substring(matcher.end());
var optionalEPackage = new EPackageService().findEPackage(this.ePackageRegistry, packageName);
if (optionalEPackage.isPresent()) {
EPackage ePackage = optionalEPackage.get();
EcoreIntrinsicExtender ecoreIntrinsicExtender = new EcoreIntrinsicExtender();
ecoreIntrinsicExtender.updateMetamodels(List.of(new EcoreMetamodelDescriptor(ePackage)));
EObject instance = ecoreIntrinsicExtender.createInstance(className);
return Optional.ofNullable(instance);
}
}
return Optional.empty();
}
Aggregations