diff options
author | Ansgar Radermacher | 2015-10-23 12:34:57 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2015-11-06 15:51:31 +0000 |
commit | df9e84a5455e1a7c750f6618dc6881d87a497390 (patch) | |
tree | 857bc9667134e0d14d52d4aa636b4c0299711087 /plugins/infra/services/org.eclipse.papyrus.infra.services.edit | |
parent | efbbd8cca60713d73019744381df9d477a3f814f (diff) | |
download | org.eclipse.papyrus-df9e84a5455e1a7c750f6618dc6881d87a497390.tar.gz org.eclipse.papyrus-df9e84a5455e1a7c750f6618dc6881d87a497390.tar.xz org.eclipse.papyrus-df9e84a5455e1a7c750f6618dc6881d87a497390.zip |
480511 - [ServiceEdit] Enhance utility functions
Change-Id: I5edd3f05abc5402fc0a941402323a3af37f9a1a7
Diffstat (limited to 'plugins/infra/services/org.eclipse.papyrus.infra.services.edit')
3 files changed, 127 insertions, 0 deletions
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);
+ }
}
|