From 4e199a413f3091232f1786d1c93222450f7b32c2 Mon Sep 17 00:00:00 2001 From: Camille Letavernier Date: Tue, 26 May 2015 17:43:38 +0200 Subject: 467863: [CSS - Properties View] Read-only exception when trying to silently create a ModelStyleSheet element https://bugs.eclipse.org/bugs/show_bug.cgi?id=467863 - Create the Stylesheet in the Notation resource only when we actually try to modify model stylesheets Change-Id: Ia83053016069b0745f41f511465315fef6f83ee1 Signed-off-by: Camille Letavernier --- .../databinding/ModelStyleSheetObservableList.java | 81 +++++++++++++++++++--- .../properties/modelelement/CSSModelElement.java | 29 +------- .../infra/gmfdiag/css/engine/ModelCSSEngine.java | 15 ++++ 3 files changed, 91 insertions(+), 34 deletions(-) (limited to 'plugins/infra/gmfdiag') diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/src/org/eclipse/papyrus/infra/gmfdiag/css/properties/databinding/ModelStyleSheetObservableList.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/src/org/eclipse/papyrus/infra/gmfdiag/css/properties/databinding/ModelStyleSheetObservableList.java index 7594aad37ab..1b3a7028b62 100644 --- a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/src/org/eclipse/papyrus/infra/gmfdiag/css/properties/databinding/ModelStyleSheetObservableList.java +++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/src/org/eclipse/papyrus/infra/gmfdiag/css/properties/databinding/ModelStyleSheetObservableList.java @@ -7,7 +7,7 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Mickaël Adam (ALL4TEC) mickael.adam@all4tec.net - Initial API and implementation + * Micka�l Adam (ALL4TEC) mickael.adam@all4tec.net - Initial API and implementation * *****************************************************************************/ package org.eclipse.papyrus.infra.gmfdiag.css.properties.databinding; @@ -16,17 +16,21 @@ import java.util.Collection; import java.util.List; import org.eclipse.core.databinding.observable.IChangeListener; +import org.eclipse.emf.common.command.AbstractCommand; import org.eclipse.emf.common.command.Command; import org.eclipse.emf.common.command.CompoundCommand; import org.eclipse.emf.common.util.EList; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EStructuralFeature; import org.eclipse.emf.ecore.resource.Resource; -import org.eclipse.emf.edit.domain.EditingDomain; +import org.eclipse.emf.transaction.RecordingCommand; import org.eclipse.emf.transaction.TransactionalEditingDomain; import org.eclipse.gmf.runtime.notation.EObjectListValueStyle; import org.eclipse.papyrus.infra.emf.databinding.EMFObservableList; +import org.eclipse.papyrus.infra.gmfdiag.css.engine.ExtendedCSSEngine; +import org.eclipse.papyrus.infra.gmfdiag.css.engine.ModelCSSEngine; import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSDiagram; +import org.eclipse.papyrus.infra.gmfdiag.css.resource.CSSNotationResource; import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.ModelStyleSheets; @@ -43,7 +47,7 @@ public class ModelStyleSheetObservableList extends EMFObservableList implements private Resource notationResource; /** The domain. */ - private EditingDomain domain; + private TransactionalEditingDomain domain; /** The listener. */ private CustomModelStyleSheetListener listener; @@ -62,7 +66,7 @@ public class ModelStyleSheetObservableList extends EMFObservableList implements * @param feature * the feature */ - public ModelStyleSheetObservableList(Resource notationResource, List wrappedList, EditingDomain domain, EObject source, EStructuralFeature feature) { + public ModelStyleSheetObservableList(Resource notationResource, List wrappedList, TransactionalEditingDomain domain, EObject source, EStructuralFeature feature) { super(wrappedList, domain, source, feature); this.notationResource = notationResource; this.domain = domain; @@ -81,8 +85,69 @@ public class ModelStyleSheetObservableList extends EMFObservableList implements @Override public Command getAddAllCommand(Collection values) { CompoundCommand compoundCommand = new CompoundCommand(); + + if (source.eResource() == null) { + compoundCommand.append(new RecordingCommand(domain, "Create ModelStylesheet") { + /** + * @see org.eclipse.emf.transaction.RecordingCommand#doExecute() + * + */ + @Override + protected void doExecute() { + notationResource.getContents().add(source); + } + + }); + + compoundCommand.append(new AbstractCommand("Initialize ModelCSSEngine Adapter") { + + public void redo() { + execute(); + } + + public void execute() { + ExtendedCSSEngine engine = CSSNotationResource.getEngine(notationResource); + if (engine instanceof ModelCSSEngine) { + ((ModelCSSEngine) engine).initAdapter(); + } + } + + /** + * @see org.eclipse.emf.common.command.AbstractCommand#canUndo() + * + * @return + */ + @Override + public boolean canUndo() { + return true; + } + + /** + * @see org.eclipse.emf.common.command.AbstractCommand#undo() + * + */ + @Override + public void undo() { + ExtendedCSSEngine engine = CSSNotationResource.getEngine(notationResource); + if (engine instanceof ModelCSSEngine) { + ((ModelCSSEngine) engine).disposeAdapter(); + } + } + + /** + * @see org.eclipse.emf.common.command.AbstractCommand#prepare() + * + * @return + */ + @Override + protected boolean prepare() { + return true; + } + }); + } + compoundCommand.append(super.getAddAllCommand(values)); - compoundCommand.append(new AddAllModelStyleSheetCommand((TransactionalEditingDomain) domain, notationResource, values)); + compoundCommand.append(new AddAllModelStyleSheetCommand(domain, notationResource, values)); return compoundCommand; } @@ -111,8 +176,8 @@ public class ModelStyleSheetObservableList extends EMFObservableList implements for (Object styleSheetReference : ((EObjectListValueStyle) objectFromDiagram).getEObjectListValue()) { // If the current style sheet to delete from model exist on a diagram, add it on the root if (value == styleSheetReference) { - compoundCommand.append(new RemoveObjectCommand((TransactionalEditingDomain) domain, (EObject) styleSheetReference)); - compoundCommand.append(new AddModelStyleSheetCommand((TransactionalEditingDomain) domain, notationResource, (EObject) styleSheetReference)); + compoundCommand.append(new RemoveObjectCommand(domain, (EObject) styleSheetReference)); + compoundCommand.append(new AddModelStyleSheetCommand(domain, notationResource, (EObject) styleSheetReference)); } } } @@ -132,7 +197,7 @@ public class ModelStyleSheetObservableList extends EMFObservableList implements */ @Override public Command getRemoveAllCommand(Collection values) { - return new RemoveAllModelStyleSheetValueCommand((TransactionalEditingDomain) domain, notationResource, values); + return new RemoveAllModelStyleSheetValueCommand(domain, notationResource, values); } /** diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/src/org/eclipse/papyrus/infra/gmfdiag/css/properties/modelelement/CSSModelElement.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/src/org/eclipse/papyrus/infra/gmfdiag/css/properties/modelelement/CSSModelElement.java index df00838d2fb..7c59a7a3920 100644 --- a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/src/org/eclipse/papyrus/infra/gmfdiag/css/properties/modelelement/CSSModelElement.java +++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/src/org/eclipse/papyrus/infra/gmfdiag/css/properties/modelelement/CSSModelElement.java @@ -17,21 +17,18 @@ import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.util.EcoreUtil; import org.eclipse.emf.edit.domain.EditingDomain; +import org.eclipse.emf.transaction.TransactionalEditingDomain; import org.eclipse.gmf.runtime.notation.Diagram; import org.eclipse.gmf.runtime.notation.View; import org.eclipse.jface.viewers.ILabelProvider; -import org.eclipse.papyrus.infra.gmfdiag.css.engine.ExtendedCSSEngine; -import org.eclipse.papyrus.infra.gmfdiag.css.engine.ModelCSSEngine; import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSDiagram; import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSStyles; -import org.eclipse.papyrus.infra.gmfdiag.css.properties.Activator; import org.eclipse.papyrus.infra.gmfdiag.css.properties.creation.StyleSheetFactory; import org.eclipse.papyrus.infra.gmfdiag.css.properties.databinding.DiagramStyleSheetObservableList; import org.eclipse.papyrus.infra.gmfdiag.css.properties.databinding.ModelStyleSheetObservableList; import org.eclipse.papyrus.infra.gmfdiag.css.properties.provider.CSSStyleSheetContentProvider; import org.eclipse.papyrus.infra.gmfdiag.css.properties.provider.CSSStyleSheetLabelProvider; import org.eclipse.papyrus.infra.gmfdiag.css.provider.CSSClassContentProvider; -import org.eclipse.papyrus.infra.gmfdiag.css.resource.CSSNotationResource; import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.ModelStyleSheets; import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StylesheetsFactory; import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StylesheetsPackage; @@ -70,7 +67,7 @@ public class CSSModelElement extends CustomStyleModelElement { @Override public IObservable doGetObservable(String propertyPath) { if (CSSStyles.CSS_DIAGRAM_STYLESHEETS_KEY.equals(propertyPath)) { - if (source instanceof View){ + if (source instanceof View) { return new DiagramStyleSheetObservableList((View) source, domain, propertyPath); } } @@ -85,28 +82,8 @@ public class CSSModelElement extends CustomStyleModelElement { // The model styleSheet final ModelStyleSheets modelStyleSheetsSource = modelStyleSheetObject instanceof ModelStyleSheets ? (ModelStyleSheets) modelStyleSheetObject : StylesheetsFactory.eINSTANCE.createModelStyleSheets(); // If the modelStylesheet doesn't exist - if (!(modelStyleSheetObject instanceof ModelStyleSheets)) { - try { - org.eclipse.papyrus.infra.core.sasheditor.di.contentprovider.utils.TransactionHelper.run(domain, new Runnable() { - public void run() { - // Add modelStylesheet to the resource without command - notationResource.getContents().add(modelStyleSheetsSource); - } - }); - } catch (Exception e) { - Activator.log.error(e); - } - // Initialize the adapter of the engine to listen model styleSheet - if (notationResource instanceof CSSNotationResource ){ - ExtendedCSSEngine engine = ((CSSNotationResource) notationResource).getModelEngine(); - - if (engine instanceof ModelCSSEngine) { - ((ModelCSSEngine) engine).initAdapter(); - } - } - } - return new ModelStyleSheetObservableList(notationResource, modelStyleSheetsSource.getStylesheets(), domain, modelStyleSheetsSource, StylesheetsPackage.Literals.MODEL_STYLE_SHEETS__STYLESHEETS); + return new ModelStyleSheetObservableList(notationResource, modelStyleSheetsSource.getStylesheets(), (TransactionalEditingDomain) domain, modelStyleSheetsSource, StylesheetsPackage.Literals.MODEL_STYLE_SHEETS__STYLESHEETS); } return super.doGetObservable(propertyPath); } diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/engine/ModelCSSEngine.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/engine/ModelCSSEngine.java index 2958bd4cc07..89a6695c0c6 100644 --- a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/engine/ModelCSSEngine.java +++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/engine/ModelCSSEngine.java @@ -86,6 +86,21 @@ public class ModelCSSEngine extends ExtendedCSSEngineImpl { } } + /** + * Disposes the adapter attached to model styleSheet. + * + */ + public void disposeAdapter() { + for (EObject eObject : model.getContents()) { + if (eObject instanceof ModelStyleSheets) { + ModelStyleSheets styleSheets = (ModelStyleSheets) eObject; + styleSheets.eAdapters().remove(adapter); + for (StyleSheet styleSheet : styleSheets.getStylesheets()) { + styleSheet.eAdapters().remove(adapter); + } + } + } + } private static ExtendedCSSEngine getParentCSSEngine(Resource resource) { ExtendedCSSEngine result; -- cgit v1.2.3