Resolve to correct ts project.

Change-Id: I3b3afc246a4e0668db8133fd1199ee049b16aa62
diff --git a/core/org.eclipse.tigerstripe.api/src/main/java/org/eclipse/tigerstripe/workbench/project/ITigerstripeModelProject.java b/core/org.eclipse.tigerstripe.api/src/main/java/org/eclipse/tigerstripe/workbench/project/ITigerstripeModelProject.java
index bcfa75f..98ca039 100644
--- a/core/org.eclipse.tigerstripe.api/src/main/java/org/eclipse/tigerstripe/workbench/project/ITigerstripeModelProject.java
+++ b/core/org.eclipse.tigerstripe.api/src/main/java/org/eclipse/tigerstripe/workbench/project/ITigerstripeModelProject.java
@@ -69,7 +69,9 @@
 	 */
 	public boolean hasDependency(IDependency dep) throws TigerstripeException;
 	
-	public IModelReference makeReference(String modelId);
+	IModelReference makeReference(String modelId);
+    
+	ITigerstripeModelProject getReferenceAsProject(String modelId);
 
 	public boolean hasReference(ITigerstripeModelProject project)
 			throws TigerstripeException;
diff --git a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/api/impl/BaseTigerstripeModelProject.java b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/api/impl/BaseTigerstripeModelProject.java
index 826d994..81ae4ea 100644
--- a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/api/impl/BaseTigerstripeModelProject.java
+++ b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/api/impl/BaseTigerstripeModelProject.java
@@ -31,24 +31,23 @@
 import org.eclipse.tigerstripe.workbench.project.IProjectDetails;

 import org.eclipse.tigerstripe.workbench.project.ITigerstripeModelProject;

 

-public class BaseTigerstripeModelProject 

-    extends AbstractTigerstripeProjectHandle implements ITigerstripeModelProject 

-    {

+public class BaseTigerstripeModelProject extends AbstractTigerstripeProjectHandle implements ITigerstripeModelProject {

 

-    // Eventually this needs to implement the necessary parts of ITigerstripeModelProject

+    // Eventually this needs to implement the necessary parts of

+    // ITigerstripeModelProject

     protected IProjectDescriptor tsProject;

     private ArtifactManager manager;

     protected IArtifactManagerSession artifactMgrSession;

-    

+

     protected ITigerstripeRuntime runtime;

 

     private final Map<String, IModelReference> loadedReferencesMap = new HashMap<>();

-    

+

     public BaseTigerstripeModelProject(ITigerstripeRuntime runtime, URI projectContainerURI) {

         super(projectContainerURI);

         this.runtime = runtime;

     }

-    

+

     @Override

     public void dispose() {

         super.dispose();

@@ -76,11 +75,11 @@
 

         return artifactMgrSession;

     }

-    

+

     protected synchronized void setArtifactManagerSession(IArtifactManagerSession session) {

         artifactMgrSession = session;

     }

-    

+

     public synchronized IProjectDescriptor getTSProject() throws TigerstripeException {

         if (tsProject == null) {

             // try and create a project for the URI

@@ -99,7 +98,7 @@
     protected IProjectDescriptor createProjectDescriptor(File baseDir) {

         return new TigerstripeProject(runtime, baseDir, this);

     }

-    

+

     public IProjectDetails getProjectDetails() throws TigerstripeException {

         return getTSProject().getProjectDetails();

     }

@@ -110,11 +109,11 @@
             String name = super.getName();

             if (name == null) {

                 return "";

-            } 

+            }

             return name;

-        } 

+        }

         return modelId;

-        

+

     }

 

     @Override

@@ -133,7 +132,7 @@
     @Override

     public boolean exists() {

         // check that a descriptor can be found and that it is valid

-        return findProjectDescriptor();     

+        return findProjectDescriptor();

     }

 

     /**

@@ -147,11 +146,10 @@
         return new File(projectContainer, "tigerstripe.xml").exists();

     }

 

-

     protected IM1Generator getGenerator(ITigerstripeModelProject project, IM1RunConfig config) {

         return new BaseM1Generator(this, config);

     }

-    

+

     @Override

     public IPluginRunStatus[] generate(IM1RunConfig config, IProgressMonitor monitor) throws TigerstripeException {

         IM1Generator generator = getGenerator(this, config);

@@ -163,80 +161,74 @@
     @Override

     public IPluginConfig[] getPluginConfigs() throws TigerstripeException {

         return getTSProject().getPluginConfigs();

-    }  

+    }

 

     @Override

     public IRunConfig makeRunConfig(int runType) throws TigerstripeException {

         switch (runType) {

         case IRunConfig.M0:

-            return  new M0RunConfig(this);

+            return new M0RunConfig(this);

         case IRunConfig.M1:

             return new M1RunConfig(this);

         }

         throw new IllegalArgumentException("Unknown generation config type (" + runType + ").");

 

-    

     }

-    

-    //Advanced properties

+

+    // Advanced properties

 

     @Override

-    public String getAdvancedProperty(String property)

-            throws TigerstripeException {

+    public String getAdvancedProperty(String property) throws TigerstripeException {

         return getTSProject().getAdvancedProperty(property);

     }

 

-

     @Override

-    public String getAdvancedProperty(String property, String defaultValue)

-            throws TigerstripeException {

+    public String getAdvancedProperty(String property, String defaultValue) throws TigerstripeException {

         return getTSProject().getAdvancedProperty(property, defaultValue);

     }

 

-    //Facets

+    // Facets

 

     @Override

     public IFacetReference getActiveFacet() throws TigerstripeException {

         return getArtifactManagerSession().getActiveFacet();

     }

-    

+

     @Override

     public void resetActiveFacet() throws TigerstripeException {

         getArtifactManagerSession().resetActiveFacet();

-        

+

     }

 

-  @Override

-  public IFacetReference[] getFacetReferences() throws TigerstripeException {

-      return getTSProject().getFacetReferences();

-  }

+    @Override

+    public IFacetReference[] getFacetReferences() throws TigerstripeException {

+        return getTSProject().getFacetReferences();

+    }

 

-  @Override

-  public IUseCaseReference makeUseCaseReference(String projectRelativePath)

-          throws TigerstripeException {

+    @Override

+    public IUseCaseReference makeUseCaseReference(String projectRelativePath) throws TigerstripeException {

         return new UseCaseReference(projectRelativePath, getTSProject());

-  }

+    }

 

-  @Override

-  public void addFacetReference(IFacetReference facetRef) throws TigerstripeException {

-      //TODO 

-      

-  }

+    @Override

+    public void addFacetReference(IFacetReference facetRef) throws TigerstripeException {

+        // TODO

+

+    }

 

     @Override

     public void setActiveFacet(IFacetReference facet, IProgressMonitor monitor) throws TigerstripeException {

         getArtifactManagerSession().setActiveFacet(facet, monitor);

-        

+

     }

 

-    //Working copy - should not really ever be called

-    

+    // Working copy - should not really ever be called

+

     @Override

     protected WorkingCopyManager doCreateCopy(IProgressMonitor monitor) throws TigerstripeException {

-        return new BaseTigerstripeModelProject(runtime,getURI());

+        return new BaseTigerstripeModelProject(runtime, getURI());

     }

 

-

     @Override

     protected void doCommit(IProgressMonitor monitor) throws TigerstripeException {

         // Not supported here

@@ -249,26 +241,23 @@
     }

 

     @Override

-    public boolean isDirty() { 

+    public boolean isDirty() {

         // Not supported here

         return false;

-    }    

-    

-    //References

-    

+    }

+

+    // References

+

     @Override

-    public boolean hasReference(ITigerstripeModelProject project)

-            throws TigerstripeException {

+    public boolean hasReference(ITigerstripeModelProject project) throws TigerstripeException {

         return getTSProject().hasReference(project);

     }

 

     @Override

-    public ITigerstripeModelProject[] getReferencedProjects()

-            throws TigerstripeException {

+    public ITigerstripeModelProject[] getReferencedProjects() throws TigerstripeException {

         return getTSProject().getReferencedProjects();

     }

 

-

     @Override

     public ITigerstripeModelProject[] getEnabledReferencedProjects() throws TigerstripeException {

         return getTSProject().getEnabledReferencedProjects();

@@ -279,31 +268,28 @@
         return getTSProject().getModelReferences();

     }

 

-

     @Override

     public IModelReference[] getEnabledModelReferences() throws TigerstripeException {

         return getTSProject().getEnabledModelReferences();

     }

 

-

     @Override

     public IModelReference[] getReferencingModels(int level) throws TigerstripeException {

         return new IModelReference[0];

     }

 

-    //Dependencies

+    // Dependencies

 

     @Override

     public IDependency[] getDependencies() throws TigerstripeException {

         return getTSProject().getDependencies();

     }

 

-

     @Override

     public IDependency[] getEnabledDependencies() throws TigerstripeException {

         return getTSProject().getEnabledDependencies();

     }

-    

+

     @Override

     public boolean hasDependency(IDependency dep) throws TigerstripeException {

         if (dep == null || !dep.isValid())

@@ -316,22 +302,30 @@
     public ITigerstripeRuntime getRuntime() {

         return runtime;

     }

-    

+

     @Override

     public final IModelReference makeReference(String modelId) {

         synchronized (loadedReferencesMap) {

             return loadedReferencesMap.computeIfAbsent(modelId, this::newRef);

         }

     }

-    

+

+    public final ITigerstripeModelProject getReferenceAsProject(String modelId) {

+        IModelReference ref = null;

+        synchronized (loadedReferencesMap) {

+            ref = loadedReferencesMap.get(modelId);

+        }

+        return ref != null ? ref.getResolvedModel() : null;

+    }

+

     protected IModelReference clearReference(String modelId) {

         synchronized (loadedReferencesMap) {

             return loadedReferencesMap.remove(modelId);

         }

     }

-    

+

     protected IModelReference newRef(String modelId) {

         return new ModelReference(getRuntime(), this, modelId);

     }

-  

+

 }

diff --git a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/api/impl/ContextualModelProject.java b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/api/impl/ContextualModelProject.java
index 43b9742..e9821be 100644
--- a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/api/impl/ContextualModelProject.java
+++ b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/api/impl/ContextualModelProject.java
@@ -310,5 +310,10 @@
     public IModelReference makeReference(String modelId) {
         return new ModelReference(getRuntime(), this, modelId);
     }
+    
+    @Override
+    public ITigerstripeModelProject getReferenceAsProject(String modelId) {
+        return makeReference(modelId).getResolvedModel();
+    }
 
 }
diff --git a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/project/TigerstripeModuleProject.java b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/project/TigerstripeModuleProject.java
index dbfcb65..04e9704 100644
--- a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/project/TigerstripeModuleProject.java
+++ b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/project/TigerstripeModuleProject.java
@@ -27,18 +27,19 @@
 
     @Override
     public synchronized ITigerstripeModelProject getTSProject() {
-        if (hostingProject != null) {
-            return hostingProject;
-        }
         if (installedModuleProject == null) {
             try {
                 IProjectDetails pd = getProjectDetails();
                 if (pd != null) {
                     String modelId = pd.getModelId();
                     if (modelId != null) {
-                        IInstalledModule module = runtime.getModule(modelId);
-                        if (module != null) {
-                            installedModuleProject = module.makeModuleProject();
+                        if (hostingProject != null) {
+                            installedModuleProject = hostingProject.getReferenceAsProject(modelId);
+                        } else {
+                            IInstalledModule module = runtime.getModule(modelId);
+                            if (module != null) {
+                                installedModuleProject = module.makeModuleProject();
+                            }
                         }
                     }
                 }