Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnsgar Radermacher2015-10-23 08:34:57 -0400
committerGerrit Code Review @ Eclipse.org2015-11-06 10:51:31 -0500
commitdf9e84a5455e1a7c750f6618dc6881d87a497390 (patch)
tree857bc9667134e0d14d52d4aa636b4c0299711087 /plugins
parentefbbd8cca60713d73019744381df9d477a3f814f (diff)
downloadorg.eclipse.papyrus-df9e84a5455e1a7c750f6618dc6881d87a497390.tar.gz
org.eclipse.papyrus-df9e84a5455e1a7c750f6618dc6881d87a497390.tar.xz
org.eclipse.papyrus-df9e84a5455e1a7c750f6618dc6881d87a497390.zip
480511 - [ServiceEdit] Enhance utility functions
Diffstat (limited to 'plugins')
-rw-r--r--plugins/infra/org.eclipse.papyrus.infra.newchild/src/org/eclipse/papyrus/infra/newchild/CreationMenuFactory.java61
-rw-r--r--plugins/infra/services/org.eclipse.papyrus.infra.services.edit/messages.properties1
-rw-r--r--plugins/infra/services/org.eclipse.papyrus.infra.services.edit/src/org/eclipse/papyrus/infra/services/edit/messages/Messages.java1
-rw-r--r--plugins/infra/services/org.eclipse.papyrus.infra.services.edit/src/org/eclipse/papyrus/infra/services/edit/service/ElementEditServiceUtils.java125
4 files changed, 131 insertions, 57 deletions
diff --git a/plugins/infra/org.eclipse.papyrus.infra.newchild/src/org/eclipse/papyrus/infra/newchild/CreationMenuFactory.java b/plugins/infra/org.eclipse.papyrus.infra.newchild/src/org/eclipse/papyrus/infra/newchild/CreationMenuFactory.java
index be072a00c2b..7f61e14c5e0 100644
--- a/plugins/infra/org.eclipse.papyrus.infra.newchild/src/org/eclipse/papyrus/infra/newchild/CreationMenuFactory.java
+++ b/plugins/infra/org.eclipse.papyrus.infra.newchild/src/org/eclipse/papyrus/infra/newchild/CreationMenuFactory.java
@@ -16,12 +16,9 @@ package org.eclipse.papyrus.infra.newchild;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.UnexecutableCommand;
import org.eclipse.emf.common.util.EList;
@@ -30,29 +27,22 @@ import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
-import org.eclipse.emf.transaction.RollbackException;
-import org.eclipse.emf.transaction.Transaction;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
-import org.eclipse.emf.transaction.impl.InternalTransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateRelationshipRequest;
-import org.eclipse.gmf.runtime.emf.type.core.requests.GetEditContextRequest;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ILabelProvider;
-import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject;
import org.eclipse.papyrus.infra.filters.Filter;
import org.eclipse.papyrus.infra.newchild.elementcreationmenumodel.CreateRelationshipMenu;
import org.eclipse.papyrus.infra.newchild.elementcreationmenumodel.CreationMenu;
import org.eclipse.papyrus.infra.newchild.elementcreationmenumodel.Folder;
-import org.eclipse.papyrus.infra.services.edit.internal.context.TypeContext;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
-import org.eclipse.papyrus.infra.services.edit.utils.IRequestCacheEntries;
import org.eclipse.papyrus.infra.services.edit.utils.RequestCacheEntries;
import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
import org.eclipse.papyrus.infra.widgets.editors.TreeSelectorDialog;
@@ -180,55 +170,12 @@ public class CreationMenuFactory {
*/
protected boolean constructMenu(EObject selectedObject, Menu menu, CreationMenu currentCreationMenu, Map<?, ?> adviceCache) {
// find the destination owner
- GetEditContextRequest editContextRequest = new GetEditContextRequest(editingDomain, buildRequest(null, selectedObject, currentCreationMenu, adviceCache), selectedObject);
-
- editContextRequest.setParameter(IRequestCacheEntries.Cache_Maps, adviceCache);
- editContextRequest.setEditContext(selectedObject);
- try {
- editContextRequest.setClientContext(TypeContext.getContext());
- } catch (ServiceException e) {
- Activator.log.error(e);
+ final EObject target = ElementEditServiceUtils.getTargetFromContext(editingDomain, selectedObject,
+ buildRequest(null, selectedObject, currentCreationMenu, adviceCache));
+ if (target == null) {
return false;
}
- IElementEditService provider = ElementEditServiceUtils.getCommandProvider(selectedObject);
- if (provider == null) {
- return false;
- }
-
- EObject target = selectedObject;
- Object result = null;
- final ICommand getEditContextCommand = provider.getEditCommand(editContextRequest);
- if (getEditContextCommand != null) {
- IStatus status = null;
- try {
- // this command could run in an unprotected transaction, it is not supposed to modify the model
- InternalTransactionalEditingDomain domain = (InternalTransactionalEditingDomain) editingDomain;
- Map<String, Object> options = new HashMap<String, Object>();
- options.put(Transaction.OPTION_NO_NOTIFICATIONS, true);
- options.put(Transaction.OPTION_NO_VALIDATION, true);
- options.put(Transaction.OPTION_NO_TRIGGERS, true);
- Transaction transaction = domain.startTransaction(false, options);
- try {
- status = getEditContextCommand.execute(null, null);
- } finally {
- transaction.commit();
- }
- } catch (InterruptedException e) {
- Activator.log.error(e);
- } catch (ExecutionException e) {
- Activator.log.error(e);
- } catch (RollbackException e) {
- Activator.log.error(e);
- }
- if (!(status == null || !status.isOK())) {
- result = getEditContextCommand.getCommandResult().getReturnValue();
- }
- if (result instanceof EObject) {
- target = (EObject) result;
- }
- }
-
// find the feature between children and owner
ArrayList<EStructuralFeature> possibleEFeatures = getEreferences(target, currentCreationMenu);
@@ -273,7 +220,7 @@ public class CreationMenuFactory {
}
/**
- * display an icon from a specified url or from Element type
+ * display an icon from a specified URL or from Element type
*
* @param currentCreationMenu
* @param item
diff --git a/plugins/infra/services/org.eclipse.papyrus.infra.services.edit/messages.properties b/plugins/infra/services/org.eclipse.papyrus.infra.services.edit/messages.properties
index fed94c61004..61324862b68 100644
--- a/plugins/infra/services/org.eclipse.papyrus.infra.services.edit/messages.properties
+++ b/plugins/infra/services/org.eclipse.papyrus.infra.services.edit/messages.properties
@@ -9,3 +9,4 @@ ElementEditServiceProvider_NoIElementTypeFound=No IElementType bound to Papyrus
ElementEditServiceUtils_UnableToFindElementType=Unable to get ElementType edit service for {0}.
ElementEditServiceUtils_UnableToFindServiceProvider=Unable to get ElementType edit service provider.
+ElementEditServiceUtils_CantGetEditingDomainProvider=Impossible to get the editing domain provider.
diff --git a/plugins/infra/services/org.eclipse.papyrus.infra.services.edit/src/org/eclipse/papyrus/infra/services/edit/messages/Messages.java b/plugins/infra/services/org.eclipse.papyrus.infra.services.edit/src/org/eclipse/papyrus/infra/services/edit/messages/Messages.java
index 19183497ab5..3cdbf69dcaa 100644
--- a/plugins/infra/services/org.eclipse.papyrus.infra.services.edit/src/org/eclipse/papyrus/infra/services/edit/messages/Messages.java
+++ b/plugins/infra/services/org.eclipse.papyrus.infra.services.edit/src/org/eclipse/papyrus/infra/services/edit/messages/Messages.java
@@ -31,6 +31,7 @@ public class Messages extends NLS {
public static String ElementEditServiceUtils_UnableToFindElementType;
public static String ElementEditServiceUtils_UnableToFindServiceProvider;
+ public static String ElementEditServiceUtils_CantGetEditingDomainProvider;
static {
NLS.initializeMessages("messages", Messages.class); //$NON-NLS-1$
diff --git a/plugins/infra/services/org.eclipse.papyrus.infra.services.edit/src/org/eclipse/papyrus/infra/services/edit/service/ElementEditServiceUtils.java b/plugins/infra/services/org.eclipse.papyrus.infra.services.edit/src/org/eclipse/papyrus/infra/services/edit/service/ElementEditServiceUtils.java
index 148b306ec7d..bd7fcfe4eb4 100644
--- a/plugins/infra/services/org.eclipse.papyrus.infra.services.edit/src/org/eclipse/papyrus/infra/services/edit/service/ElementEditServiceUtils.java
+++ b/plugins/infra/services/org.eclipse.papyrus.infra.services.edit/src/org/eclipse/papyrus/infra/services/edit/service/ElementEditServiceUtils.java
@@ -14,13 +14,31 @@
*****************************************************************************/
package org.eclipse.papyrus.infra.services.edit.service;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.transaction.RollbackException;
+import org.eclipse.emf.transaction.Transaction;
+import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.emf.transaction.impl.InternalTransactionalEditingDomain;
+import org.eclipse.emf.transaction.util.TransactionUtil;
+import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
+import org.eclipse.gmf.runtime.emf.type.core.IHintedType;
+import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
+import org.eclipse.gmf.runtime.emf.type.core.requests.GetEditContextRequest;
+import org.eclipse.gmf.runtime.emf.type.core.requests.IEditCommandRequest;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
import org.eclipse.papyrus.infra.services.edit.Activator;
import org.eclipse.papyrus.infra.services.edit.internal.ElementEditServiceProvider;
+import org.eclipse.papyrus.infra.services.edit.internal.context.TypeContext;
import org.eclipse.papyrus.infra.services.edit.messages.Messages;
+import org.eclipse.papyrus.infra.services.edit.utils.IRequestCacheEntries;
/**
* <pre>
@@ -104,4 +122,111 @@ public class ElementEditServiceUtils {
return ElementEditServiceProvider.getInstance();
}
+ /**
+ * Return a target after executing a passed context request
+ *
+ * @param editingDomain
+ * the editing domain
+ * @param owner
+ * the parent object in which we want to create an element
+ * @param hintedType
+ * The information which element to create.
+ * @return the resulting target object or null in case of a failure
+ */
+ public static EObject getTargetFromContext(TransactionalEditingDomain editingDomain, EObject owner, IEditCommandRequest editRequest) {
+ GetEditContextRequest editContextRequest = new GetEditContextRequest(editingDomain, editRequest, owner);
+
+ editContextRequest.setParameter(IRequestCacheEntries.Cache_Maps, new HashMap<Object, Object>());
+ editContextRequest.setEditContext(owner);
+ try {
+ editContextRequest.setClientContext(TypeContext.getContext());
+ } catch (ServiceException e) {
+ Activator.log.error(e);
+ return null;
+ }
+
+ IElementEditService provider = ElementEditServiceUtils.getCommandProvider(owner);
+ if (provider == null) {
+ Activator.log.debug(Messages.ElementEditServiceUtils_CantGetEditingDomainProvider);
+ return null;
+ }
+
+ EObject target = owner;
+ Object result = null;
+ final ICommand getEditContextCommand = provider.getEditCommand(editContextRequest);
+ if (getEditContextCommand != null) {
+ IStatus status = null;
+ try {
+ // this command could run in an unprotected transaction, it is not supposed to modify the model
+ InternalTransactionalEditingDomain domain = (InternalTransactionalEditingDomain) editingDomain;
+ Map<String, Object> options = new HashMap<String, Object>();
+ options.put(Transaction.OPTION_NO_NOTIFICATIONS, true);
+ options.put(Transaction.OPTION_NO_VALIDATION, true);
+ options.put(Transaction.OPTION_NO_TRIGGERS, true);
+ Transaction transaction = domain.startTransaction(false, options);
+ try {
+ status = getEditContextCommand.execute(null, null);
+ } finally {
+ transaction.commit();
+ }
+ } catch (InterruptedException e) {
+ Activator.log.error(e);
+ } catch (ExecutionException e) {
+ Activator.log.error(e);
+ } catch (RollbackException e) {
+ Activator.log.error(e);
+ }
+ if (status != null && status.isOK()) {
+ result = getEditContextCommand.getCommandResult().getReturnValue();
+ }
+ if (result instanceof EObject) {
+ target = (EObject) result;
+ }
+ }
+ return target;
+ }
+
+ /**
+ * Convenience function that determines editing domain automatically.
+ *
+ * @see {@link #getTargetFromContext(TransactionalEditingDomain, EObject, IEditCommandRequest)}
+ */
+ public static EObject getTargetFromContext(EObject owner, IEditCommandRequest editRequest) {
+ return getTargetFromContext(TransactionUtil.getEditingDomain(owner), owner, editRequest);
+
+ }
+
+ /**
+ * Support creation of a child within another object with a (default) context
+ *
+ * @param editingDomain
+ * the editing domain
+ * @param owner
+ * the parent object in which we want to create an element
+ * @param hintedType
+ * The information which element to create.
+ * @return a GMF command that will create child
+ */
+ public static ICommand getCreateChildCommandWithContext(TransactionalEditingDomain editingDomain, EObject owner, IHintedType hintedType) {
+ TransactionalEditingDomain transactionalEditingDomain = TransactionUtil.getEditingDomain(owner);
+ final EObject target = getTargetFromContext(transactionalEditingDomain, owner,
+ new CreateElementRequest(transactionalEditingDomain, owner, hintedType));
+
+ IElementEditService provider = ElementEditServiceUtils.getCommandProvider(target);
+ if (provider == null) {
+ return org.eclipse.gmf.runtime.common.core.command.UnexecutableCommand.INSTANCE;
+ }
+
+ ICommand createGMFCommand = provider.getEditCommand(new CreateElementRequest(transactionalEditingDomain, target, hintedType));
+ return createGMFCommand;
+ }
+
+ /**
+ * Convenience function that determines editing domain automatically
+ *
+ * @see {@link #getCreateChildCommandWithContext(TransactionalEditingDomain, EObject, IHintedType)}
+ */
+ public static ICommand getCreateChildCommandWithContext(EObject owner, IHintedType hintedType) {
+ return getCreateChildCommandWithContext(TransactionUtil.getEditingDomain(owner), owner, hintedType);
+ }
}

Back to the top