Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian W. Damus2016-11-16 16:33:57 +0000
committerGerrit Code Review @ Eclipse.org2016-11-17 13:49:37 +0000
commit5f62e7c6b9c1e92791afab32444ce573315415ec (patch)
tree98cfb5632a9003f8db9d094f78b326b8d2402770 /plugins/infra/gmfdiag
parent4cca0d88fe8bff062b402cf10f25fe463ac1cbab (diff)
downloadorg.eclipse.papyrus-5f62e7c6b9c1e92791afab32444ce573315415ec.tar.gz
org.eclipse.papyrus-5f62e7c6b9c1e92791afab32444ce573315415ec.tar.xz
org.eclipse.papyrus-5f62e7c6b9c1e92791afab32444ce573315415ec.zip
Bug 507618: CreateEditBasedElementCommand does not use the factory registered in the resource set
Use the factory registered in the resource set to create UML elements. https://bugs.eclipse.org/bugs/show_bug.cgi?id=507618 Change-Id: If7a137b79ecd28379e8de23d29b989b08f2ec3ee
Diffstat (limited to 'plugins/infra/gmfdiag')
-rw-r--r--plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/CreateEditBasedElementCommand.java52
1 files changed, 44 insertions, 8 deletions
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/CreateEditBasedElementCommand.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/CreateEditBasedElementCommand.java
index 853ada99096..12b9091ca5e 100644
--- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/CreateEditBasedElementCommand.java
+++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/commands/CreateEditBasedElementCommand.java
@@ -1,5 +1,5 @@
/*****************************************************************************
- * Copyright (c) 2013, 2014 CEA LIST and others.
+ * Copyright (c) 2013, 2016 CEA LIST, Christian W. Damus, and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -9,6 +9,7 @@
* Contributors:
* Remi Schnekenburger (CEA LIST) - Initial API and implementation
* Christian W. Damus (CEA) - bug 431109
+ * Christian W. Damus - bug 507618
*
*****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.commands;
@@ -27,8 +28,10 @@ import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.UnexecutableCommand;
import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EFactory;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
+import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.command.CommandParameter;
import org.eclipse.emf.edit.command.CreateChildCommand;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
@@ -120,9 +123,10 @@ public class CreateEditBasedElementCommand extends CreateElementCommand {
EReference containment = getContainmentFeature();
if (containment != null) {
IEditingDomainItemProvider editingDomainItemProvider = AdapterFactoryEditingDomain.getEditingDomainItemProviderFor(element);
+ ResourceSet resourceSet = EMFHelper.getResourceSet(element);
for (EClass eClassToCreateCommandsFor : eClassesToCreateCommandsFor) {
-
- EObject newElement = eClassToCreateCommandsFor.getEPackage().getEFactoryInstance().create(eClassToCreateCommandsFor);
+ // Use the resource set's registered factory, if possible (UML-RT)
+ EObject newElement = getFactory(eClassToCreateCommandsFor, resourceSet).create(eClassToCreateCommandsFor);
Command possibleCommand = editingDomainItemProvider.createCommand(element, getEditingDomain(), CreateChildCommand.class, new CommandParameter(element, containment, new CommandParameter(null, containment, newElement)));
if (possibleCommand == null) {
possibleCommands.add(UnexecutableCommand.INSTANCE);
@@ -139,6 +143,39 @@ public class CreateEditBasedElementCommand extends CreateElementCommand {
}
/**
+ * Obtain the best factory for instantiation of the given {@code eClass}.
+ * This is either
+ * <ul>
+ * <li>the factory registered locally in the {@code resourceSet} for the
+ * {@code eClass}'s package, or</li>
+ * <li>in the very unusual case that the package is not registered, or
+ * if the {@code resourceSet} is {@code null}, the static factory
+ * of the {@code eClass}'s static package</li>
+ * </ul>
+ *
+ * @param eClass
+ * an Ecore class to instantiate
+ * @param resourceSet
+ * the resource set in which context the model is being edited.
+ * May be {@code null}
+ *
+ * @return the factory to use for instantiation of the {@code eClass}
+ */
+ EFactory getFactory(EClass eClass, ResourceSet resourceSet) {
+ EFactory result = null;
+
+ if (resourceSet != null) {
+ result = resourceSet.getPackageRegistry().getEFactory(eClass.getEPackage().getNsURI());
+ }
+
+ if (result == null) {
+ result = eClass.getEPackage().getEFactoryInstance();
+ }
+
+ return result;
+ }
+
+ /**
* Prepares the EMF command to which we delegate execution, if
* it has not already been created. This is not safe to do in a
* read-only context if our request does not {@link CreateEditBasedElementCommand#hasElementToEdit() have
@@ -194,7 +231,9 @@ public class CreateEditBasedElementCommand extends CreateElementCommand {
if (containment != null) {
EObject element = getElementToEdit();
if (element != null) {
- newElement = eClass.getEPackage().getEFactoryInstance().create(eClass);
+ ResourceSet resourceSet = EMFHelper.getResourceSet(element);
+ // Use the resource set's registered factory, if possible (UML-RT)
+ newElement = getFactory(eClass, resourceSet).create(eClass);
IEditingDomainItemProvider editingDomainItemProvider = AdapterFactoryEditingDomain.getEditingDomainItemProviderFor(element);
commandDone = editingDomainItemProvider.createCommand(element, getEditingDomain(), CreateChildCommand.class, new CommandParameter(element, containment, new CommandParameter(null, containment, newElement)));
}
@@ -336,10 +375,7 @@ public class CreateEditBasedElementCommand extends CreateElementCommand {
return (configureStatus == null) ? CommandResult.newOKCommandResult(newElement) : new CommandResult(configureStatus, newElement);
}
};
- // executes the command and then adds it to the list
- compositeEMFOperation.add(configureTransactionalCommand); // add
- // before
- // execution
+ compositeEMFOperation.add(configureTransactionalCommand);
}
return compositeEMFOperation;
}

Back to the top