[466566] Add adapters on new referenced analysis after reload

Add adapters on new referenced analysis after reload. 
Control the order of the workspace impacting notifications to place the
representation files changes after the semantic files changes. The
previous order was the folder/project children resources order (by
name), it used to forbid the controlled resource detection after an
external control.

Bug: 466566
Change-Id: Ic3e078e3b73029cb0378111f5829300badaea13d
Signed-off-by: Maxime Porhel <maxime.porhel@obeo.fr>
diff --git a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/DAnalysisRefresher.java b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/DAnalysisRefresher.java
index 170387a..a668df6 100644
--- a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/DAnalysisRefresher.java
+++ b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/DAnalysisRefresher.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2012, 2014 THALES GLOBAL SERVICES and others.
+ * Copyright (c) 2012, 2015 THALES GLOBAL SERVICES 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
@@ -25,6 +25,7 @@
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.emf.ecore.EReference;
 import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.util.ECrossReferenceAdapter;
 import org.eclipse.emf.edit.command.AddCommand;
 import org.eclipse.emf.transaction.NotificationFilter;
 import org.eclipse.emf.transaction.RecordingCommand;
@@ -35,6 +36,7 @@
 import org.eclipse.emf.transaction.TransactionalEditingDomain;
 import org.eclipse.sirius.business.api.query.URIQuery;
 import org.eclipse.sirius.business.api.session.Session;
+import org.eclipse.sirius.business.api.session.danalysis.DAnalysisSession;
 import org.eclipse.sirius.ecore.extender.tool.api.ModelUtils;
 import org.eclipse.sirius.ext.emf.EReferencePredicate;
 import org.eclipse.sirius.viewpoint.DAnalysis;
@@ -43,6 +45,7 @@
 import org.eclipse.sirius.viewpoint.ViewpointPackage;
 
 import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Iterators;
 import com.google.common.collect.Lists;
@@ -120,8 +123,9 @@
     }
 
     /**
-     * Check the resources in the resourceSet and add all new resources as
-     * semantic resources of this session. New resources are :
+     * Check the resources in the resourceSet. Detect new resources and add them
+     * to the session as new semantic resources or referenced session resources.<BR><BR>
+     * New semantic resources are :
      * <UL>
      * <LI>Resources that are not in the <code>knownResources</code> list</LI>
      * <LI>Resources that are not in the semantic resources of this session</LI>
@@ -129,20 +133,45 @@
      * resources of this session</LI>
      * <LI>Resources that are not the Sirius environment resource</LI>
      * </UL>
+     * <BR>
+     * New referenced session resources are :
+     * <UL>
+     * <LI>Resources that are not in the <code>knownResources</code> list</LI>
+     * <LI>Resources that are in the referenced representations files resources
+     * of this session (the list is computed from the allAnalyses() result)</LI>
+     * </UL>
      * 
      * @param knownResources
      *            List of resources that is already loaded before the resolveAll
      *            of the representations file load.
      */
-    public void addAutomaticallyLoadedResourcesToSemanticResources(List<Resource> knownResources) {
+    public void manageAutomaticallyLoadedResources(List<Resource> knownResources) {
         TransactionalEditingDomain domain = session.getTransactionalEditingDomain();
         List<Resource> resourcesAfterLoadOfSession = Lists.newArrayList(domain.getResourceSet().getResources());
         // Remove the known resources
         Iterators.removeAll(resourcesAfterLoadOfSession.iterator(), knownResources);
+
+        if (resourcesAfterLoadOfSession.isEmpty()) {
+            return;
+        }
+
+        Set<Resource> referencedSessionResources = session.getReferencedSessionResources();
+        Collection<Resource> newReferencedSessionResources = Lists.newArrayList(Iterables.filter(resourcesAfterLoadOfSession, Predicates.in(referencedSessionResources)));
+        if (!newReferencedSessionResources.isEmpty() && session instanceof DAnalysisSession) {
+            for (Resource newReferencedSessionResource : newReferencedSessionResources) {
+                // session.registerResourceInCrossReferencer(newReferencedSessionResource);
+                // private method in DAnalysisSessionImpl
+                registerResourceInCrossReferencer(newReferencedSessionResource);
+                for (DAnalysis refAnalysis : Iterables.filter(newReferencedSessionResource.getContents(), DAnalysis.class)) {
+                    ((DAnalysisSession) session).addAdaptersOnAnalysis(refAnalysis);
+                }
+            }
+        }
+
         // Remove the known semantic resources
         Iterators.removeAll(resourcesAfterLoadOfSession.iterator(), session.getSemanticResources());
         // Remove the known referenced representations file resources
-        Iterators.removeAll(resourcesAfterLoadOfSession.iterator(), session.getReferencedSessionResources());
+        Iterators.removeAll(resourcesAfterLoadOfSession.iterator(), referencedSessionResources);
         // Remove the Sirius Environment resource
         final Iterable<Resource> newSemanticResourcesIterator = Iterables.filter(resourcesAfterLoadOfSession, new Predicate<Resource>() {
             public boolean apply(Resource resource) {
@@ -254,4 +283,13 @@
         session = null;
     }
 
+    private void registerResourceInCrossReferencer(final Resource newResource) {
+        ECrossReferenceAdapter crossReferencer = session.getSemanticCrossReferencer();
+        if (crossReferencer != null) {
+            if (!newResource.eAdapters().contains(crossReferencer)) {
+                newResource.eAdapters().add(crossReferencer);
+            }
+        }
+    }
+
 }
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 ec435f9..f899158 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
@@ -544,9 +544,8 @@
             dAnalysisRefresher.forceLoadingOfEveryLinkedResource();
             monitor.worked(10);
 
-            // Add the unknown resources to the semantic resources of this
-            // session.
-            dAnalysisRefresher.addAutomaticallyLoadedResourcesToSemanticResources(resourcesBeforeLoadOfSession);
+            // Manage the unknown resources.
+            dAnalysisRefresher.manageAutomaticallyLoadedResources(resourcesBeforeLoadOfSession);
             monitor.worked(1);
             setSynchronizeStatusofEveryResource();
             monitor.worked(1);
@@ -1732,12 +1731,18 @@
 
     private Multimap<ResourceStatus, Resource> getImpactingNewStatuses(Collection<ResourceStatusChange> changes) {
         Multimap<ResourceStatus, Resource> impactingNewStatuses = LinkedHashMultimap.create();
-        Iterable<Resource> resources = Iterables.concat(getSemanticResources(), getAllSessionResources(), getControlledResources());
+        Multimap<ResourceStatus, Resource> sessionResourcesNewStatuses = LinkedHashMultimap.create();
+        Iterable<Resource> semanticOrControlledresources = Iterables.concat(getSemanticResources(), getControlledResources());
+        Set<Resource> allSessionResources = getAllSessionResources();
         for (ResourceStatusChange change : changes) {
-            if (isResourceOfSession(change.getResource(), resources)) {
+            if (isResourceOfSession(change.getResource(), semanticOrControlledresources)) {
                 impactingNewStatuses.put(change.getNewStatus(), change.getResource());
+            } else if (isResourceOfSession(change.getResource(), allSessionResources)) {
+                sessionResourcesNewStatuses.put(change.getNewStatus(), change.getResource());
             }
         }
+        // Add session resource impacting status after semantic ones.
+        impactingNewStatuses.putAll(sessionResourcesNewStatuses);
         return impactingNewStatuses;
     }
 
@@ -1854,10 +1859,9 @@
                         mainDAnalysis = (DAnalysis) sessionResource.getContents().get(0);
                     }
                 }
-                // Add the unknown resources to the semantic resources of this
-                // session.
+                // Manage the unknown resources.
                 if (dAnalysisRefresher != null) {
-                    dAnalysisRefresher.addAutomaticallyLoadedResourcesToSemanticResources(resourcesBeforeReload);
+                    dAnalysisRefresher.manageAutomaticallyLoadedResources(resourcesBeforeReload);
                 }
                 notifyListeners(SessionListener.REPLACED);
             }
diff --git a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/ReloadRepresentationsFileCmd.java b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/ReloadRepresentationsFileCmd.java
index d58d868..4e264e6 100644
--- a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/ReloadRepresentationsFileCmd.java
+++ b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/ReloadRepresentationsFileCmd.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010 THALES GLOBAL SERVICES.
+ * Copyright (c) 2010, 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
@@ -100,7 +100,6 @@
                 Adapter existingAirDCrossReferenceAdapter = EcoreUtil.getExistingAdapter(resource.getResourceSet(), AirDCrossReferenceAdapter.class);
                 if (existingAirDCrossReferenceAdapter instanceof AirDCrossReferenceAdapter) {
                     resource.eAdapters().add(existingAirDCrossReferenceAdapter);
-                    System.out.println("Add airDCrossReferenceAdapter on resource " + resource.getURI().toString());
                 }
             }
         }
diff --git a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/RepresentationsChangeAdapter.java b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/RepresentationsChangeAdapter.java
index d6ae272..31c4e7e 100644
--- a/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/RepresentationsChangeAdapter.java
+++ b/plugins/org.eclipse.sirius/src/org/eclipse/sirius/business/internal/session/danalysis/RepresentationsChangeAdapter.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2010 THALES GLOBAL SERVICES.
+ * Copyright (c) 2010, 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
@@ -102,7 +102,9 @@
      *            the analysis
      */
     public void registerAnalysis(final DAnalysis analysis) {
-        analysis.eAdapters().add(this);
+        if (!analysis.eAdapters().contains(this)) {
+            analysis.eAdapters().add(this);
+        }
         for (final DView view : analysis.getOwnedViews()) {
             registerView(view);
         }
@@ -135,7 +137,9 @@
      *            the editor
      */
     private void registerView(final DView view) {
-        view.eAdapters().add(this);
+        if (!view.eAdapters().contains(this)) {
+            view.eAdapters().add(this);
+        }
     }
 
     /**