package eu.cloudscaleproject.env.toolchain.addons;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

import org.eclipse.core.resources.IProject;
import org.eclipse.e4.core.contexts.Active;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.annotations.Optional;

import eu.cloudscaleproject.env.common.CloudscaleContext;
import eu.cloudscaleproject.env.common.notification.IValidationStatusProvider;
import eu.cloudscaleproject.env.toolchain.IActiveResources;
import eu.cloudscaleproject.env.toolchain.resources.types.IEditorInputResource;

/**
 * 
 * Transfer objects from the active context to the CloudScale global context
 * 
 * @author vito
 *
 */
public class ToolchainAddon {
	
	private static class ActiveResources implements IActiveResources{

		private IProject project = null;
		private IEditorInputResource alternative = null;
		private IValidationStatusProvider statusProvider = null;
		
		public void setProject(IProject project){
			this.project = project;
		}
		
		public void setAlternative(IEditorInputResource alternative){
			this.alternative = alternative;
		}
		
		public void setStatusProvideer(IValidationStatusProvider statusProvider){
			this.statusProvider = statusProvider;
		}
		
		@Override
		public IProject getActiveProject() {
			return this.project;
		}

		@Override
		public IEditorInputResource getActiveAlternative() {
			return this.alternative;
		}

		@Override
		public IValidationStatusProvider getActiveStatusProvider() {
			return this.statusProvider;
		}
		
	}
	
	@PostConstruct
	public void postConstruct(IEclipseContext context){
		//put dummy object at initialization
		context.set(IActiveResources.class, new ActiveResources());
	}
	
	@Inject
	public void updateStatus(IEclipseContext context,
							 @Active @Optional @Named(CloudscaleContext.ACTIVE_ALTERNATIVE) IValidationStatusProvider alternative,
							 @Active @Optional IValidationStatusProvider statusProvider,
							 @Active @Optional IEditorInputResource editorInputResource,
							 @Active @Optional IProject project){
		
		ActiveResources activeResources = new ActiveResources();
		
		//handle validation status provider
		if(statusProvider == null){
			if(alternative instanceof IValidationStatusProvider){
				statusProvider = (IValidationStatusProvider)alternative;
			}
			if(editorInputResource != null){
				statusProvider = editorInputResource;
			}
		}
		activeResources.setStatusProvideer(statusProvider);
	
		//handle editor input resource
		if(editorInputResource == null){
			if(alternative instanceof IEditorInputResource){
				editorInputResource = (IEditorInputResource)alternative;
			}
			if(statusProvider instanceof IEditorInputResource){
				editorInputResource = (IEditorInputResource)statusProvider;
			}
		}
		activeResources.setAlternative(editorInputResource);

		//handle project
		IProject currentProject = CloudscaleContext.getGlobalContext().get(IProject.class);
		
		if(project != null){
			if(project.isAccessible()){
				activeResources.setProject(project);
			}
		}
		else{
			if(currentProject != null && !currentProject.isAccessible()){
				activeResources.setProject(project);
			}
		}
		
		context.set(IActiveResources.class, activeResources);
	}

}

@@ -2107,16 +2107,124 @@
 ct)%7B%0A%09%09%0A
+%09%09ActiveResources currentActiveResources = CloudscaleContext.getGlobalContext().get(ActiveResources.class);%0A
 %09%09Active
@@ -3003,95 +3003,8 @@
 ject
-%0A%09%09IProject currentProject = CloudscaleContext.getGlobalContext().get(IProject.class);%0A
 %09%09%0A%09
@@ -3116,24 +3116,75 @@
 else
-%7B%0A%09%09%09if(currentP
+ if(currentActiveResources != null)%7B%0A%09%09%09if(currentActiveResources.p
 roje
@@ -3201,17 +3201,32 @@
  && 
-!
 current
-P
+ActiveResources.p
 roje
@@ -3268,32 +3268,55 @@
 rces.setProject(
+currentActiveResources.
 project);%0A%09%09%09%7D%0A%09

Active IProject fix