diff options
| author | Pierre-Charles David | 2016-04-05 15:52:14 +0000 |
|---|---|---|
| committer | Pierre-Charles David | 2016-04-19 12:28:13 +0000 |
| commit | 094df8768a5219d949711937864187cc8508b64b (patch) | |
| tree | fff86746a7ce9c81ab0a0234f14dcd922b084268 | |
| parent | afbd80111677b634970facbfd63034a704182686 (diff) | |
| download | org.eclipse.sirius-094df8768a5219d949711937864187cc8508b64b.tar.gz org.eclipse.sirius-094df8768a5219d949711937864187cc8508b64b.tar.xz org.eclipse.sirius-094df8768a5219d949711937864187cc8508b64b.zip | |
[491251] Encapsulate the dependency on EMF Tx on the Sirius side
Bug: 491251
Change-Id: Ibfbf7b3198a54c17c8ca768e971a5d48c65021c1
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
2 files changed, 120 insertions, 2 deletions
diff --git a/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/TransactionalEditingDomainContextAdapter.java b/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/TransactionalEditingDomainContextAdapter.java new file mode 100644 index 0000000000..c7fc1f8b48 --- /dev/null +++ b/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/TransactionalEditingDomainContextAdapter.java @@ -0,0 +1,116 @@ +/******************************************************************************* + * Copyright (c) 2016 Obeo. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.ui.properties.internal; + +import java.util.List; + +import org.eclipse.eef.core.api.EditingContextAdapter; +import org.eclipse.eef.core.api.controllers.IConsumer; +import org.eclipse.emf.common.notify.Notification; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.transaction.NotificationFilter; +import org.eclipse.emf.transaction.RecordingCommand; +import org.eclipse.emf.transaction.ResourceSetChangeEvent; +import org.eclipse.emf.transaction.ResourceSetListener; +import org.eclipse.emf.transaction.ResourceSetListenerImpl; +import org.eclipse.emf.transaction.TransactionalEditingDomain; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; + +/** + * Integrates EEF properties views with a specific + * {@link TransactionalEditingDomain} (for example the associated with a Sirius + * session). + * + * @author pcdavid + * + */ +public class TransactionalEditingDomainContextAdapter implements EditingContextAdapter { + /** + * Describes the model changes we want to react to. + */ + private static final NotificationFilter FILTER = NotificationFilter.NOT_TOUCH.and(NotificationFilter.createNotifierTypeFilter(EObject.class)); + + /** + * The editing domain to integrate with. + */ + private final TransactionalEditingDomain ted; + + /** + * The pre-commit listener used to detect model changes and call back EEF. + */ + private final ResourceSetListener preCommitListener = new Listener(); + + /** + * The callback to invoke to notify the EEF side when the model has changed. + */ + private IConsumer<List<Notification>> callback; + + /** + * Create a new detector. + * + * @param ted + * the editing domain to integrate with. + */ + public TransactionalEditingDomainContextAdapter(TransactionalEditingDomain ted) { + this.ted = Preconditions.checkNotNull(ted); + } + + @Override + public void performModelChange(final Runnable effect) { + RecordingCommand cmd = new RecordingCommand(ted) { + protected void doExecute() { + effect.run(); + }; + }; + ted.getCommandStack().execute(cmd); + } + + @Override + public synchronized void onModelChange(IConsumer<List<Notification>> trigger) { + if (this.callback == null) { + ted.addResourceSetListener(preCommitListener); + } + this.callback = trigger; + } + + @Override + public void removeModelChangeConsumer() { + this.callback = null; + ted.removeResourceSetListener(preCommitListener); + } + + /** + * The actual implementation of the pre-commit listener. + * + * @author pcdavid + * + */ + private class Listener extends ResourceSetListenerImpl { + public Listener() { + super(FILTER); + } + + @Override + public boolean isPostcommitOnly() { + return true; + } + + @Override + public void resourceSetChanged(final ResourceSetChangeEvent event) { + IConsumer<List<Notification>> t = callback; + if (t != null) { + t.apply(Lists.newArrayList(event.getNotifications())); + } + } + } +} diff --git a/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/tabprovider/SiriusTabDescriptorProvider.java b/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/tabprovider/SiriusTabDescriptorProvider.java index 0f639b36c1..9c7d081c49 100644 --- a/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/tabprovider/SiriusTabDescriptorProvider.java +++ b/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/tabprovider/SiriusTabDescriptorProvider.java @@ -41,6 +41,7 @@ import org.eclipse.sirius.ui.properties.internal.Messages; import org.eclipse.sirius.ui.properties.internal.SiriusInputDescriptor; import org.eclipse.sirius.ui.properties.internal.SiriusInterpreter; import org.eclipse.sirius.ui.properties.internal.SiriusUIPropertiesPlugin; +import org.eclipse.sirius.ui.properties.internal.TransactionalEditingDomainContextAdapter; import org.eclipse.sirius.viewpoint.description.DescriptionPackage; import org.eclipse.sirius.viewpoint.description.Group; import org.eclipse.sirius.viewpoint.description.Viewpoint; @@ -131,11 +132,12 @@ public class SiriusTabDescriptorProvider implements IEEFTabDescriptorProvider { return descriptors; } - private EEFView createEEFView(Session session, SiriusInputDescriptor input, EEFViewDescription viewDescription) { + private EEFView createEEFView(final Session session, SiriusInputDescriptor input, EEFViewDescription viewDescription) { IVariableManager variableManager = new VariableManagerFactory().createVariableManager(); variableManager.put(EEFExpressionUtils.SELF, input.getSemanticElement()); variableManager.put(EEFExpressionUtils.INPUT, input); - EEFView eefView = new EEFViewFactory().createEEFView(viewDescription, variableManager, new SiriusInterpreter(session), session.getTransactionalEditingDomain(), input); + TransactionalEditingDomainContextAdapter eca = new TransactionalEditingDomainContextAdapter(session.getTransactionalEditingDomain()); + EEFView eefView = new EEFViewFactory().createEEFView(viewDescription, variableManager, new SiriusInterpreter(session), eca, input); return eefView; } |
