diff options
| author | Laurent Fasani | 2015-01-30 16:40:20 +0000 |
|---|---|---|
| committer | Laurent Fasani | 2015-03-19 16:07:29 +0000 |
| commit | 637da94b5e0cbc404ee480d2a3f88aa8199ca025 (patch) | |
| tree | 0ca501c66a9b613c17b56c5a66e15a4d5970b3f5 | |
| parent | acedc8f898da575e8b74c838ec4a80135ce0c509 (diff) | |
| download | org.eclipse.sirius-637da94b5e0cbc404ee480d2a3f88aa8199ca025.tar.gz org.eclipse.sirius-637da94b5e0cbc404ee480d2a3f88aa8199ca025.tar.xz org.eclipse.sirius-637da94b5e0cbc404ee480d2a3f88aa8199ca025.zip | |
[458826] Attach SessionLazyCrossReferencer on semantic fragments
update SessionLazyCrossReferencer.initialize() to attach this adapter to
controlled semantic resources
update controlledResources when adding a semantic resource
(cherry picked from Bug: 458825 commit
cbd6c9106322a66b513915b2e00d8a5b234a4671)
Bug: 458826
Change-Id: Ice9b47bb8ce4708cce6371961d2e4671fc6f9bfa
Signed-off-by: Laurent Fasani <laurent.fasani@obeo.fr>
5 files changed, 165 insertions, 136 deletions
diff --git a/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/common/SiriusCrossReferenceAdapterTests.java b/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/common/SiriusCrossReferenceAdapterTests.java index c8f98dc418..14e14d30d0 100644 --- a/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/common/SiriusCrossReferenceAdapterTests.java +++ b/plugins/org.eclipse.sirius.tests.junit/src/org/eclipse/sirius/tests/unit/common/SiriusCrossReferenceAdapterTests.java @@ -16,6 +16,7 @@ import java.util.Iterator; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.emf.common.notify.Adapter; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; @@ -23,11 +24,13 @@ import org.eclipse.emf.transaction.RecordingCommand; import org.eclipse.emf.transaction.TransactionalEditingDomain; import org.eclipse.emf.transaction.internal.EMFTransactionStatusCodes; import org.eclipse.sirius.common.tools.api.resource.ResourceSetSync; +import org.eclipse.sirius.common.tools.api.util.LazyCrossReferencer; import org.eclipse.sirius.common.tools.api.util.SiriusCrossReferenceAdapter; import org.eclipse.sirius.tests.sample.component.Component; import org.eclipse.sirius.tests.sample.component.ComponentFactory; import org.eclipse.sirius.tests.support.api.SiriusTestCase; import org.eclipse.sirius.tools.api.command.ICommandFactory; +import org.eclipse.sirius.viewpoint.DAnalysisSessionEObject; /** * Class containing tests to update of {@link SiriusCrossReferenceAdapter} after @@ -38,128 +41,141 @@ import org.eclipse.sirius.tools.api.command.ICommandFactory; */ public class SiriusCrossReferenceAdapterTests extends SiriusTestCase { - private TransactionalEditingDomain editingDomain; - - static final String fragmentFileName = "fragComponent.component"; - - @Override - protected void setUp() throws Exception { - super.setUp(); - setWarningCatchActive(true); - - } - - /** - * Check that fragmented resource is not reloaded during its unload when it - * has been externally modified. - * - * @throws Exception - */ - public void testDisablingCrossReferencerWhileReloadingResource() throws Exception { - genericSetUp(); - - initSemanticResource(); - - // simulation an EXTERNAL CHANGE of fragmentResource - File fragFile = ResourcesPlugin.getWorkspace().getRoot().getProject(TEMPORARY_PROJECT_NAME).getFile(fragmentFileName).getLocation().toFile(); - fragFile.setLastModified(System.currentTimeMillis()); - - ResourceSetSync resourceSetSync = ResourceSetSync.getResourceSetSync(editingDomain).get(); - resourceSetSync.statusChanged(editingDomain.getResourceSet().getResources().get(2), ResourceSetSync.ResourceStatus.SYNC, ResourceSetSync.ResourceStatus.EXTERNAL_CHANGED); - - //check that no warning "loading resource while unloading it" has been dispatched - for (Iterator<IStatus> warning = warnings.values().iterator(); warning.hasNext();) { - IStatus status = warning.next(); - if (status.getCode() == EMFTransactionStatusCodes.RELOAD_DURING_UNLOAD) { - fail("Resource is being reloaded during its unload."); - } + private TransactionalEditingDomain editingDomain; + + static final String FRAGMENT_FILE_NAME = "fragComponent.component"; + + @Override + protected void setUp() throws Exception { + super.setUp(); + setWarningCatchActive(true); + + } + + /** + * Check that fragmented resource is not reloaded during its unload when it + * has been externally modified. + * + * @throws Exception + */ + public void testDisablingCrossReferencerWhileReloadingResource() throws Exception { + genericSetUp(); + + initSemanticResource(); + + // check that semantic crossRefAdapter is set on fragmented resource + Resource fragmentedResource = ((DAnalysisSessionEObject) session).getControlledResources().get(0); + assertNotNull(FRAGMENT_FILE_NAME + " should be part of session controlled resource", fragmentedResource); + boolean found = false; + for (Adapter adapter : fragmentedResource.eAdapters()) { + if (adapter instanceof LazyCrossReferencer) { + found = true; + break; + } + } + assertTrue("The LazyCrossReferencer adapter is not set on fragmented resource", found); + + // simulation an EXTERNAL CHANGE of fragmentResource + File fragFile = ResourcesPlugin.getWorkspace().getRoot().getProject(TEMPORARY_PROJECT_NAME).getFile(FRAGMENT_FILE_NAME).getLocation().toFile(); + fragFile.setLastModified(System.currentTimeMillis()); + + ResourceSetSync resourceSetSync = ResourceSetSync.getResourceSetSync(editingDomain).get(); + resourceSetSync.statusChanged(fragmentedResource, ResourceSetSync.ResourceStatus.SYNC, ResourceSetSync.ResourceStatus.EXTERNAL_CHANGED); + + // check that no warning "loading resource while unloading it" has been + // dispatched + for (Iterator<IStatus> warning = warnings.values().iterator(); warning.hasNext();) { + IStatus status = warning.next(); + if (status.getCode() == EMFTransactionStatusCodes.RELOAD_DURING_UNLOAD) { + fail("Resource is being reloaded during its unload."); + } + } + } + + /** + * Check that fragmented resource is not reloaded during its unload when it + * has been externally deleted. + * + * @throws Exception + */ + public void testDisablingCrossReferencerWhileDeletingResource() throws Exception { + + // create session with empty aird + genericSetUp(); + + // add semantic resources + initSemanticResource(); + + // simulation of DELETION of fragmentResource + File fragFile = ResourcesPlugin.getWorkspace().getRoot().getProject(TEMPORARY_PROJECT_NAME).getFile(FRAGMENT_FILE_NAME).getLocation().toFile(); + fragFile.delete(); + ResourceSetSync resourceSetSync = ResourceSetSync.getResourceSetSync(editingDomain).get(); + resourceSetSync.statusChanged(editingDomain.getResourceSet().getResources().get(2), ResourceSetSync.ResourceStatus.SYNC, ResourceSetSync.ResourceStatus.DELETED); + // Warning : Avoid using ResourcesPlugin because event are sent too + // late. + // ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, + // null); + + // check that no warning "loading resource while unloading it" has been + // dispatched + for (Iterator<IStatus> warning = warnings.values().iterator(); warning.hasNext();) { + IStatus status = warning.next(); + if (status.getCode() == EMFTransactionStatusCodes.RELOAD_DURING_UNLOAD) { + fail("Resource is being reloaded during its unload."); + } + } + } + + /** + * Initialize semantic resources + */ + private void initSemanticResource() { + final ResourceSet rset = session.getTransactionalEditingDomain().getResourceSet(); + editingDomain = session.getTransactionalEditingDomain(); + + // initialize model + final Component componentRoot = ComponentFactory.eINSTANCE.createComponent(); + componentRoot.setName("compoRoot"); + componentRoot.setPayload(true); + + final Component component1 = ComponentFactory.eINSTANCE.createComponent(); + component1.setName("compo1"); + component1.setPayload(true); + + componentRoot.getChildren().add(component1); + + Component component2 = ComponentFactory.eINSTANCE.createComponent(); + component2.setName("compo2"); + component1.getChildren().add(component2); + component2.setReference(component1); + + // create resources + final URI fileMainComponentUri = URI.createPlatformResourceURI("/" + TEMPORARY_PROJECT_NAME + "/" + "Maincomponent.component", true); + final Resource rsMainComponent = rset.createResource(fileMainComponentUri); + + final URI fileFragComponentUri = URI.createPlatformResourceURI("/" + TEMPORARY_PROJECT_NAME + "/" + FRAGMENT_FILE_NAME, true); + final Resource rsFragComponent = rset.createResource(fileFragComponentUri); + + editingDomain.getCommandStack().execute(new RecordingCommand(editingDomain) { + + @Override + protected void doExecute() { + // add content to resource + rsMainComponent.getContents().add(componentRoot); + rsFragComponent.getContents().add(component1); + + // add resources to session + session.addSemanticResource(fileMainComponentUri, new NullProgressMonitor()); + + // save session + session.save(new NullProgressMonitor()); + } + }); } - } - - /** - * Check that fragmented resource is not reloaded during its unload when it - * has been externally deleted. - * - * @throws Exception - */ - public void testDisablingCrossReferencerWhileDeletingResource() throws Exception { - - // create session with empty aird - genericSetUp(); - - // add semantic resources - initSemanticResource(); - - // simulation of DELETION of fragmentResource - File fragFile = ResourcesPlugin.getWorkspace().getRoot().getProject(TEMPORARY_PROJECT_NAME).getFile(fragmentFileName).getLocation().toFile(); - fragFile.delete(); - ResourceSetSync resourceSetSync = ResourceSetSync.getResourceSetSync(editingDomain).get(); - resourceSetSync.statusChanged(editingDomain.getResourceSet().getResources().get(2), ResourceSetSync.ResourceStatus.SYNC, ResourceSetSync.ResourceStatus.DELETED); - // Warning : Avoid using ResourcesPlugin because event are sent too late. - // ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, - // null); - - // check that no warning "loading resource while unloading it" has been - // dispatched - for (Iterator<IStatus> warning = warnings.values().iterator(); warning.hasNext();) { - IStatus status = warning.next(); - if (status.getCode() == EMFTransactionStatusCodes.RELOAD_DURING_UNLOAD) { - fail("Resource is being reloaded during its unload."); - } + + @Override + protected ICommandFactory getCommandFactory() { + return null; } - } - - /** - * Initialize semantic resources - */ - private void initSemanticResource() { - final ResourceSet rset = session.getTransactionalEditingDomain().getResourceSet(); - editingDomain = session.getTransactionalEditingDomain(); - - // initialize model - final Component componentRoot = ComponentFactory.eINSTANCE.createComponent(); - componentRoot.setName("compoRoot"); - componentRoot.setPayload(true); - - final Component component1 = ComponentFactory.eINSTANCE.createComponent(); - component1.setName("compo1"); - component1.setPayload(true); - - componentRoot.getChildren().add(component1); - - Component component2 = ComponentFactory.eINSTANCE.createComponent(); - component2.setName("compo2"); - component1.getChildren().add(component2); - component2.setReference(component1); - - // create resources - final URI fileMainComponentUri = URI.createPlatformResourceURI("/" + TEMPORARY_PROJECT_NAME + "/" + "Maincomponent.component", true); - final Resource rsMainComponent = rset.createResource(fileMainComponentUri); - - final URI fileFragComponentUri = URI.createPlatformResourceURI("/" + TEMPORARY_PROJECT_NAME + "/" + fragmentFileName, true); - final Resource rsFragComponent = rset.createResource(fileFragComponentUri); - - editingDomain.getCommandStack().execute(new RecordingCommand(editingDomain) { - - @Override - protected void doExecute() { - // add content to resource - rsMainComponent.getContents().add(componentRoot); - rsFragComponent.getContents().add(component1); - - // add resources to session - session.addSemanticResource(fileMainComponentUri, new NullProgressMonitor()); - session.addSemanticResource(fileFragComponentUri, new NullProgressMonitor()); - - // save session - session.save(new NullProgressMonitor()); - } - }); - } - - @Override - protected ICommandFactory getCommandFactory() { - return null; - } } diff --git a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/ControlledResourcesDetector.java b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/ControlledResourcesDetector.java index 1fa262a57b..965e6927a3 100644 --- a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/ControlledResourcesDetector.java +++ b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/ControlledResourcesDetector.java @@ -82,7 +82,11 @@ public class ControlledResourcesDetector extends ResourceSetListenerImpl { session = null; } - private void detectControlledResources() { + /** + * Detects controlled resources and update controlledResourcesList in + * {@link DAnalysisSessionImpl}. + */ + public void detectControlledResources() { final Collection<Resource> semantics = session.getSemanticResources(); final Collection<Resource> resourcesToCheck = new ArrayList<Resource>(session.getTransactionalEditingDomain().getResourceSet().getResources()); Collection<Resource> newControlledResources = new LinkedHashSet<Resource>(); @@ -90,6 +94,7 @@ public class ControlledResourcesDetector extends ResourceSetListenerImpl { for (final Resource resource : Iterables.filter(resourcesToCheck, Predicates.not(Predicates.in(controlledResources)))) { if (hasControlledRootInSemantics(resource, semantics) && !controlledResources.contains(resource)) { newControlledResources.add(resource); + session.registerResourceInCrossReferencer(resource); } } @@ -97,11 +102,11 @@ public class ControlledResourcesDetector extends ResourceSetListenerImpl { session.getControlledResources().addAll(newControlledResources); session.notifyListeners(SessionListener.SEMANTIC_CHANGE); } - } private boolean hasControlledRootInSemantics(Resource resource, Collection<Resource> semantics) { Predicate<EObject> isControlled = new Predicate<EObject>() { + @Override public boolean apply(EObject input) { return AdapterFactoryEditingDomain.isControlled(input); } diff --git a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/DAnalysisSessionImpl.java b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/DAnalysisSessionImpl.java index 6daa6e36ed..1c826e3f68 100644 --- a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/DAnalysisSessionImpl.java +++ b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/DAnalysisSessionImpl.java @@ -603,6 +603,9 @@ public class DAnalysisSessionImpl extends DAnalysisSessionEObjectImpl implements analysis.getModels().add(root); } } + + tracker.detectControlledResources(); + registerResourceInCrossReferencer(newResource); } diff --git a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/SessionLazyCrossReferencer.java b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/SessionLazyCrossReferencer.java index e599edc9ee..10cd26cf50 100644 --- a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/SessionLazyCrossReferencer.java +++ b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/SessionLazyCrossReferencer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2014 THALES GLOBAL SERVICES. + * Copyright (c) 2014, 2015 THALES GLOBAL SERVICES. * 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 @@ -10,12 +10,16 @@ *******************************************************************************/ package org.eclipse.sirius.business.internal.session.danalysis; +import java.util.Collection; import java.util.List; +import java.util.Set; import org.eclipse.emf.common.notify.Adapter; +import org.eclipse.emf.common.util.EList; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.sirius.common.tools.api.util.LazyCrossReferencer; -import org.eclipse.sirius.viewpoint.DAnalysis; + +import com.google.common.collect.Iterables; /** * A LazyCrossReferencer for the session. @@ -41,17 +45,14 @@ public class SessionLazyCrossReferencer extends LazyCrossReferencer { @Override protected void initialize() { super.initialize(); - for (Resource res : session.getSemanticResources()) { - List<Adapter> adapters = res.eAdapters(); - // add only if it was not added between creation and - // initialization - if (!adapters.contains(this)) { - adapters.add(this); - } - } - - for (DAnalysis analysis : session.allAnalyses()) { - List<Adapter> adapters = analysis.eResource().eAdapters(); + + Collection<Resource> semanticResources = session.getSemanticResources(); + EList<Resource> controlledResources = session.getControlledResources(); + Set<Resource> allSessionResources = session.getAllSessionResources(); + + Iterable<Resource> resources = Iterables.concat(semanticResources, controlledResources, allSessionResources); + for (Resource resource : resources) { + List<Adapter> adapters = resource.eAdapters(); // add only if it was not added between creation and // initialization if (!adapters.contains(this)) { diff --git a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/SessionResourcesTracker.java b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/SessionResourcesTracker.java index 1c08cfe687..c142da8810 100644 --- a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/SessionResourcesTracker.java +++ b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/SessionResourcesTracker.java @@ -164,4 +164,8 @@ class SessionResourcesTracker { semanticResources.clear(); } } + + void detectControlledResources() { + controlledResourcesDetector.detectControlledResources(); + } } |
