diff options
7 files changed, 124 insertions, 33 deletions
diff --git a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/AbstractBaseModel.java b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/AbstractBaseModel.java index c9b798632be..b93d8b805e3 100644 --- a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/AbstractBaseModel.java +++ b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/AbstractBaseModel.java @@ -1,6 +1,5 @@ /***************************************************************************** - * Copyright (c) 2010, 2014 CEA LIST, Christian W. Damus, and others. - * + * Copyright (c) 2010, 2015 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 @@ -12,6 +11,7 @@ * Christian W. Damus (CEA) - manage models by URI, not IFile (CDO) * Christian W. Damus (CEA) - bug 437052 * Christian W. Damus - bug 399859 + * Christian W. Damus - bug 481149 * *****************************************************************************/ package org.eclipse.papyrus.infra.core.resource; @@ -35,7 +35,7 @@ import org.eclipse.emf.ecore.xmi.impl.URIHandlerImpl.PlatformSchemeAware; import org.eclipse.papyrus.infra.core.Activator; /** - * An abstract implmeentation of model. This class should be subclassed to fit + * An abstract implementation of model. This class should be subclassed to fit * the required model. * * @author cedric dumoulin @@ -124,6 +124,9 @@ public abstract class AbstractBaseModel extends AbstractModel implements IVersio resource = getModelManager().createResource(resourceURI); } configureResource(resource); + + // And start snippets + startSnippets(); } protected void configureResource(Resource resource) { @@ -186,7 +189,7 @@ public abstract class AbstractBaseModel extends AbstractModel implements IVersio configureResource(resource); // call registered snippets - snippets.performStart(this); + startSnippets(); } /** @@ -257,7 +260,7 @@ public abstract class AbstractBaseModel extends AbstractModel implements IVersio @Override public void unload() { // call registered snippets - snippets.performDispose(this); + stopSnippets(); // Do unloading if (resource != null) { diff --git a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/AbstractDynamicModel.java b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/AbstractDynamicModel.java index 9a2097107a2..fcecfdd5e32 100644 --- a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/AbstractDynamicModel.java +++ b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/AbstractDynamicModel.java @@ -1,6 +1,17 @@ -/**
+/*****************************************************************************
+ * Copyright (c) 2010, 2015 LIFL, 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
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * LIFL - Initial API and implementation
+ * Christian W. Damus - bug 481149
+ *
+ *****************************************************************************/
+
package org.eclipse.papyrus.infra.core.resource;
import java.io.IOException;
@@ -56,8 +67,9 @@ public abstract class AbstractDynamicModel<T extends EObject> extends AbstractBa }
// The resource do not exist, crate it.
createModel(fullPathWithoutExtension);
+
// call registered snippets
- snippets.performStart(this);
+ startSnippets();
}
/**
diff --git a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/AbstractModel.java b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/AbstractModel.java index 4d9940c47a5..a11561b9a58 100644 --- a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/AbstractModel.java +++ b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/AbstractModel.java @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2013 CEA LIST. + * Copyright (c) 2013, 2015 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 * Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Bug 436952 + * Christian W. Damus - bug 481149 * *****************************************************************************/ package org.eclipse.papyrus.infra.core.resource; @@ -33,6 +34,8 @@ public abstract class AbstractModel implements IModel { /** List of attached snippets */ protected ModelSnippetList snippets = new ModelSnippetList(); + private boolean snippetsStarted; + /** list of Models (referenced by identifiers) that should be loaded before this one can be loaded */ protected List<String> afterLoadModelIdentifiers; @@ -76,7 +79,10 @@ public abstract class AbstractModel implements IModel { */ @Override public void addModelSnippet(IModelSnippet snippet) { - snippets.add(snippet); + if (snippets.add(snippet) && snippetsStarted) { + // Snippets have already started, so start this one, too + snippet.start(this); + } } /** @@ -114,6 +120,8 @@ public abstract class AbstractModel implements IModel { @Override public void unload() { this.modelSet = null; + + stopSnippets(); snippets.clear(); } @@ -133,4 +141,24 @@ public abstract class AbstractModel implements IModel { // Nothing to do } + + /** + * Starts my registered snippets. + */ + protected void startSnippets() { + if (!snippetsStarted) { + snippetsStarted = true; + snippets.performStart(this); + } + } + + /** + * Stops my registered snippets. + */ + protected void stopSnippets() { + if (snippetsStarted) { + snippetsStarted = false; + snippets.performDispose(this); + } + } } diff --git a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/IModel.java b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/IModel.java index 2e4d9ed0ca3..837eca7ede3 100644 --- a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/IModel.java +++ b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/IModel.java @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2010, 2013 CEA LIST and others. + * Copyright (c) 2010, 2015 CEA LIST, Christian W. Damus, and others. * * * All rights reserved. This program and the accompanying materials @@ -11,6 +11,7 @@ * CEA LIST - Initial API and implementation * Christian W. Damus (CEA) - manage models by URI, not IFile (CDO) * Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Bug 436952 + * Christian W. Damus - bug 481149 * * *****************************************************************************/ @@ -141,7 +142,9 @@ public interface IModel { /** * Add a snippet to this model. The snippet is called just after model is * initialized, and before it is disposed or unloaded. Snippet can be shared - * among models. + * among models. If this model's snippets have already been started (the + * model was previously loaded), then this new {@code snippet} is started + * also. * * @param snippet * The snippet to add. diff --git a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/additional/AdditionalResourcesModel.java b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/additional/AdditionalResourcesModel.java index b674258142f..df98aa77d7f 100644 --- a/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/additional/AdditionalResourcesModel.java +++ b/plugins/infra/core/org.eclipse.papyrus.infra.core/src/org/eclipse/papyrus/infra/core/resource/additional/AdditionalResourcesModel.java @@ -1,6 +1,5 @@ /***************************************************************************** - * Copyright (c) 2010, 2014 Atos Origin, CEA LIST, and others. - * + * Copyright (c) 2010, 2015 Atos Origin, 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 @@ -13,6 +12,7 @@ * Christian W. Damus (CEA LIST) - support control mode in CDO resources * Christian W. Damus (CEA) - bug 437052 * Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Bug 436952 + * Christian W. Damus - bug 481149 * * *****************************************************************************/ @@ -67,13 +67,13 @@ public class AdditionalResourcesModel extends AbstractModel implements IModel { @Deprecated public void loadModel(IPath path) { // call registered snippets - snippets.performStart(this); + startSnippets(); } @Override public void loadModel(URI uri) { // call registered snippets - snippets.performStart(this); + startSnippets(); } @Override @@ -122,7 +122,7 @@ public class AdditionalResourcesModel extends AbstractModel implements IModel { @Override public void unload() { // call registered snippets - snippets.performDispose(this); + stopSnippets(); // Unload remaining resources for (int i = 0; i < modelSet.getResources().size(); i++) { diff --git a/plugins/uml/decoratormodel/org.eclipse.papyrus.uml.decoratormodel/src/org/eclipse/papyrus/uml/decoratormodel/model/DecoratorModel.java b/plugins/uml/decoratormodel/org.eclipse.papyrus.uml.decoratormodel/src/org/eclipse/papyrus/uml/decoratormodel/model/DecoratorModel.java index cc806975c51..43bc784a95f 100644 --- a/plugins/uml/decoratormodel/org.eclipse.papyrus.uml.decoratormodel/src/org/eclipse/papyrus/uml/decoratormodel/model/DecoratorModel.java +++ b/plugins/uml/decoratormodel/org.eclipse.papyrus.uml.decoratormodel/src/org/eclipse/papyrus/uml/decoratormodel/model/DecoratorModel.java @@ -11,6 +11,7 @@ * Christian W. Damus - bug 399859 * Christian W. Damus - bug 458655 * Christian W. Damus - bug 458197 + * Christian W. Damus - bug 481149 * *****************************************************************************/ package org.eclipse.papyrus.uml.decoratormodel.model; @@ -33,7 +34,6 @@ import org.eclipse.emf.transaction.impl.InternalTransactionalEditingDomain; import org.eclipse.emf.transaction.util.TransactionUtil; import org.eclipse.papyrus.infra.core.resource.AbstractBaseModel; import org.eclipse.papyrus.infra.core.resource.AbstractModel; -import org.eclipse.papyrus.infra.core.resource.IModelSnippet; import org.eclipse.papyrus.infra.core.resource.ModelSet; import org.eclipse.papyrus.infra.core.resource.ModelUtils; import org.eclipse.papyrus.uml.decoratormodel.Activator; @@ -112,8 +112,9 @@ public class DecoratorModel extends AbstractModel { Activator.log.error(e); } } + // call registered snippets - snippets.performStart(this); + startSnippets(); } /** @@ -165,7 +166,8 @@ public class DecoratorModel extends AbstractModel { @Override public void unload() { // call registered snippets - snippets.performDispose(this); + stopSnippets(); + List<Resource> resources = getResources(); for (Resource resource : resources) { resource.unload(); @@ -173,11 +175,6 @@ public class DecoratorModel extends AbstractModel { } @Override - public void addModelSnippet(IModelSnippet snippet) { - snippets.add(snippet); - } - - @Override public Set<URI> getModifiedURIs() { Set<URI> result = Sets.newHashSet(); diff --git a/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/ModelSetTest.java b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/ModelSetTest.java index 244f1cdf841..d06b7d25e1c 100644 --- a/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/ModelSetTest.java +++ b/tests/junit/plugins/core/org.eclipse.papyrus.infra.core.tests/test/org/eclipse/papyrus/infra/core/resource/ModelSetTest.java @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2010, 2014 LIFL, CEA, and others. + * Copyright (c) 2010, 2015 LIFL, CEA, 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 @@ -11,6 +11,7 @@ * Christian W. Damus (CEA) - bug 422257 * Christian W. Damus (CEA) - bug 436047 * Christian W. Damus (CEA) - bug 437052 + * Christian W. Damus - bug 481149 * *****************************************************************************/ package org.eclipse.papyrus.infra.core.resource; @@ -24,6 +25,7 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import java.io.IOException; +import java.util.Set; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; @@ -54,6 +56,9 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; + /** * Test for {@link ModelSet}. <br> * Run as "JUnit Plugin-Test" @@ -211,7 +216,7 @@ public class ModelSetTest extends AbstractPapyrusTest { mngr.registerModel(model2); IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject("org.eclipse.papyrus.infra.core"); - if(!p.exists()) { + if (!p.exists()) { p.create(new NullProgressMonitor()); } p.open(new NullProgressMonitor()); @@ -333,7 +338,7 @@ public class ModelSetTest extends AbstractPapyrusTest { assertThat("EMF resource not created", model.getResource().getContents().isEmpty(), is(false)); assertThat("EMF resource not created", res2.getContents().isEmpty(), is(false)); - // Change the referenced resource's URI. This should make the resource and its dependents dirty + // Change the referenced resource's URI. This should make the resource and its dependents dirty final URI modelURI2New = modelURI2.trimSegments(1).appendSegment("library1").appendFileExtension(model.getModelFileExtension()); res2.setURI(modelURI2New); @@ -362,19 +367,19 @@ public class ModelSetTest extends AbstractPapyrusTest { protected void createResources(String... filenames) throws CoreException, IOException { IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(PLUGIN_PROJECT_NAME); - if(!p.exists()) { + if (!p.exists()) { p.create(new NullProgressMonitor()); } p.open(new NullProgressMonitor()); ResourceSet resourceSet = houseKeeper.createResourceSet(); - for(String filename : filenames) { + for (String filename : filenames) { createResource(p, resourceSet, filename); } // Save created resources - for(Resource resource : resourceSet.getResources()) { + for (Resource resource : resourceSet.getResources()) { resource.save(null); } } @@ -415,7 +420,7 @@ public class ModelSetTest extends AbstractPapyrusTest { mngr.registerModel(model2); IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject("org.eclipse.papyrus.infra.core"); - if(!p.exists()) { + if (!p.exists()) { p.create(new NullProgressMonitor()); } p.open(new NullProgressMonitor()); @@ -533,7 +538,7 @@ public class ModelSetTest extends AbstractPapyrusTest { // force model creation testSnippetCalledAfterCreateModels(); - // now do load. + // now do load. ModelSet mngr = houseKeeper.cleanUpLater(new ModelSet()); // Add snippets @@ -640,4 +645,47 @@ public class ModelSetTest extends AbstractPapyrusTest { } + + /** + * Tests that model snippets are started if added to a model after it has loaded. + */ + @Test + public void testStartLateSnippet_bug481149() throws IOException, CoreException, ModelMultiException { + ModelSet modelSet = houseKeeper.cleanUpLater(new ModelSet()); + + String model1Key = "ecore"; + String model2Key = "genmodel"; + + FakeModel model1 = new FakeModel(model1Key); + FakeModel model2 = new FakeModel(model2Key); + + modelSet.registerModel(model1); + modelSet.registerModel(model2); + + class TestSnippet implements IModelSnippet { + Set<IModel> active = Sets.newHashSet(); + + public void start(IModel startingModel) { + active.add(startingModel); + } + + public void dispose(IModel stoppingModel) { + active.remove(stoppingModel); + } + } + + TestSnippet earlySnippet = new TestSnippet(); + TestSnippet lateSnippet = new TestSnippet(); + + model1.addModelSnippet(earlySnippet); + model2.addModelSnippet(earlySnippet); + + modelSet.createModels(URI.createURI("bogus://test")); + + model1.addModelSnippet(lateSnippet); + model2.addModelSnippet(lateSnippet); + + assertThat(earlySnippet.active, is((Set<IModel>) ImmutableSet.<IModel> of(model1, model2))); + assertThat(lateSnippet.active, is((Set<IModel>) ImmutableSet.<IModel> of(model1, model2))); + } } |