Bug 418061 - [e4] Editors on the right side are not bound to model elements in the tree navigator
diff --git a/extra/org.eclipse.emf.ecp.application.e4/Application.e4xmi b/extra/org.eclipse.emf.ecp.application.e4/Application.e4xmi
index e66d9cc..d4cf193 100644
--- a/extra/org.eclipse.emf.ecp.application.e4/Application.e4xmi
+++ b/extra/org.eclipse.emf.ecp.application.e4/Application.e4xmi
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <application:Application xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:application="http://www.eclipse.org/ui/2010/UIModel/application" xmlns:basic="http://www.eclipse.org/ui/2010/UIModel/application/ui/basic" xmlns:menu="http://www.eclipse.org/ui/2010/UIModel/application/ui/menu" xmi:id="_0JoRAJgREeKJoJcmiy2C6w" elementId="org.eclipse.e4.ide.application">
-  <children xsi:type="basic:TrimmedWindow" xmi:id="_0JoRAZgREeKJoJcmiy2C6w" label="org.eclipse.emf.ecp.application.e4" width="500" height="400">
+  <children xsi:type="basic:TrimmedWindow" xmi:id="_0JoRAZgREeKJoJcmiy2C6w" label="org.eclipse.emf.ecp.application.e4" width="800" height="600">
     <children xsi:type="basic:PartSashContainer" xmi:id="_AErb0JgUEeKJoJcmiy2C6w" elementId="org.eclipse.emf.ecp.e4.application.partsashcontainer.1" selectedElement="_lM0okJgTEeKJoJcmiy2C6w">
       <children xsi:type="basic:PartSashContainer" xmi:id="_lM0okJgTEeKJoJcmiy2C6w" elementId="org.eclipse.emf.ecp.e4.application.partsashcontainer.0" horizontal="true">
         <children xsi:type="basic:PartStack" xmi:id="_8GAbsJoUEeKJoJcmiy2C6w" elementId="org.eclipse.emf.ecp.e4.application.partstack.2">
@@ -35,7 +35,7 @@
   <handlers xmi:id="_q5IcYJgZEeKJoJcmiy2C6w" elementId="org.eclipse.emf.ecp.e4.application.handler.newProject" contributionURI="bundleclass://org.eclipse.emf.ecp.application.e4/org.eclipse.emf.ecp.e4.handlers.NewProjectHandler" command="_ogB3AJgZEeKJoJcmiy2C6w"/>
   <handlers xmi:id="_fb0kMJgcEeKJoJcmiy2C6w" elementId="org.eclipse.emf.ecp.e4.application.handler.checkoutProject" contributionURI="bundleclass://org.eclipse.emf.ecp.application.e4/org.eclipse.emf.ecp.e4.handlers.CheckoutProjectHandler" command="_eOWrgJgcEeKJoJcmiy2C6w"/>
   <handlers xmi:id="_XchBMJoSEeKJoJcmiy2C6w" elementId="org.eclipse.emf.ecp.e4.application.handler.project.newModelElement" contributionURI="bundleclass://org.eclipse.emf.ecp.application.e4/org.eclipse.emf.ecp.e4.handlers.NewModelElementHandler" command="_StwzEJoSEeKJoJcmiy2C6w"/>
-  <descriptors xmi:id="_5aBzoJgREeKJoJcmiy2C6w" elementId="org.eclipse.emf.ecp.e4.application.partdescriptor.editor" label="Editor" tooltip="Editor" allowMultiple="true" category="editorPart" closeable="true" contributionURI="bundleclass://org.eclipse.emf.ecp.application.e4/org.eclipse.emf.ecp.e4.editor.ECPE4Editor"/>
+  <descriptors xmi:id="_5aBzoJgREeKJoJcmiy2C6w" elementId="org.eclipse.emf.ecp.e4.application.partdescriptor.editor" label="" tooltip="" allowMultiple="true" category="editorPart" closeable="true" contributionURI="bundleclass://org.eclipse.emf.ecp.application.e4/org.eclipse.emf.ecp.e4.editor.ECPE4Editor"/>
   <commands xmi:id="_ogB3AJgZEeKJoJcmiy2C6w" elementId="org.eclipse.emf.ecp.e4.application.command.newProject" commandName="New Project"/>
   <commands xmi:id="_eOWrgJgcEeKJoJcmiy2C6w" elementId="org.eclipse.emf.ecp.e4.application.command.checkoutProject" commandName="Checkout"/>
   <commands xmi:id="_StwzEJoSEeKJoJcmiy2C6w" elementId="org.eclipse.emf.ecp.e4.application.command.project.newModelElement" commandName="New Model element"/>
diff --git a/extra/org.eclipse.emf.ecp.application.e4/META-INF/MANIFEST.MF b/extra/org.eclipse.emf.ecp.application.e4/META-INF/MANIFEST.MF
index 8b396ef..077d22a 100644
--- a/extra/org.eclipse.emf.ecp.application.e4/META-INF/MANIFEST.MF
+++ b/extra/org.eclipse.emf.ecp.application.e4/META-INF/MANIFEST.MF
@@ -25,7 +25,9 @@
  org.eclipse.e4.ui.bindings;bundle-version="0.10.1",
  org.eclipse.emf.ecp.editor;bundle-version="1.0.0",
  org.eclipse.emf.ecp.ui;bundle-version="1.0.0",
- org.eclipse.e4.ui.model.workbench;bundle-version="1.0.0"
+ org.eclipse.e4.ui.model.workbench;bundle-version="0.10.1",
+ org.eclipse.emf.edit.ui,
+ org.eclipse.emf.ecp.ui.view
 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 Import-Package: javax.annotation;version="1.0.0",
  org.eclipse.emf.ecp.explorereditorbridge.internal;version="1.0.0",
diff --git a/extra/org.eclipse.emf.ecp.application.e4/src/org/eclipse/emf/ecp/e4/application/Activator.java b/extra/org.eclipse.emf.ecp.application.e4/src/org/eclipse/emf/ecp/e4/application/Activator.java
index b3ccbf0..990742a 100644
--- a/extra/org.eclipse.emf.ecp.application.e4/src/org/eclipse/emf/ecp/e4/application/Activator.java
+++ b/extra/org.eclipse.emf.ecp.application.e4/src/org/eclipse/emf/ecp/e4/application/Activator.java
@@ -39,7 +39,7 @@
 		try {
 			ServiceReference<?> service=context.getServiceReferences(IContextFunction.class.getName(),"(service.context.key="+EPartService.class.getName()+")")[0];
 			//TODO a "bit" ugly
-			return (EPartService) ((IContextFunction)context.getService(service)).compute(E4Workbench.getServiceContext(),null);
+			return (EPartService) ((IContextFunction)context.getService(service)).compute(E4Workbench.getServiceContext());
 		} catch (InvalidSyntaxException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
diff --git a/extra/org.eclipse.emf.ecp.application.e4/src/org/eclipse/emf/ecp/e4/editor/E4ModelElementOpener.java b/extra/org.eclipse.emf.ecp.application.e4/src/org/eclipse/emf/ecp/e4/editor/E4ModelElementOpener.java
index 71957f4..11f1262 100644
--- a/extra/org.eclipse.emf.ecp.application.e4/src/org/eclipse/emf/ecp/e4/editor/E4ModelElementOpener.java
+++ b/extra/org.eclipse.emf.ecp.application.e4/src/org/eclipse/emf/ecp/e4/editor/E4ModelElementOpener.java
@@ -12,16 +12,35 @@
 import org.eclipse.emf.ecp.e4.application.Activator;
 import org.eclipse.emf.ecp.ui.util.ECPModelElementOpener;
 
-
 public class E4ModelElementOpener implements ECPModelElementOpener {
 
+	final String partId = "org.eclipse.emf.ecp.e4.application.partdescriptor.editor";
+
 	@Override
 	public void openModelElement(Object modelElement, ECPProject ecpProject) {
-		
-//		Activator.getEContextService();
-		EPartService partService=Activator.getEPartService();
-		MPart part=partService.createPart("org.eclipse.emf.ecp.e4.application.partdescriptor.editor");
+
+		EPartService partService = Activator.getEPartService();
+		for (MPart existingPart : partService.getParts()) {
+			if (!partId.equals(existingPart.getElementId())) {
+				continue;
+			}
+			
+			if (existingPart.getContext()==null) {
+				continue;
+			}
+
+			if (existingPart.getContext().get(ECPE4Editor.INPUT) == modelElement) {
+				if (!existingPart.isVisible() || !existingPart.isOnTop()) {
+					partService.showPart(existingPart, PartState.ACTIVATE);
+				}
+				return;
+			}
+		}
+
+		MPart part = partService.createPart(partId);
 		partService.showPart(part, PartState.ACTIVATE);
+		part.getContext().set(ECPProject.class, ecpProject);
+		part.getContext().set(ECPE4Editor.INPUT, modelElement);
 	}
 
 }
diff --git a/extra/org.eclipse.emf.ecp.application.e4/src/org/eclipse/emf/ecp/e4/editor/ECPE4Editor.java b/extra/org.eclipse.emf.ecp.application.e4/src/org/eclipse/emf/ecp/e4/editor/ECPE4Editor.java
index 50acbe6..eae07aa 100644
--- a/extra/org.eclipse.emf.ecp.application.e4/src/org/eclipse/emf/ecp/e4/editor/ECPE4Editor.java
+++ b/extra/org.eclipse.emf.ecp.application.e4/src/org/eclipse/emf/ecp/e4/editor/ECPE4Editor.java
@@ -1,41 +1,112 @@
 package org.eclipse.emf.ecp.e4.editor;
 
+import java.net.URL;
+
 import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
 import javax.inject.Named;
 
 import org.eclipse.e4.core.di.annotations.Optional;
+import org.eclipse.e4.ui.di.Focus;
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
 import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.emf.common.notify.Adapter;
+import org.eclipse.emf.common.notify.Notification;
+import org.eclipse.emf.common.notify.impl.AdapterImpl;
 import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecp.core.ECPProject;
 import org.eclipse.emf.ecp.core.util.ECPUtil;
 import org.eclipse.emf.ecp.edit.ECPEditorContext;
 import org.eclipse.emf.ecp.editor.EditorFactory;
 import org.eclipse.emf.ecp.editor.IEditorCompositeProvider;
 import org.eclipse.emf.ecp.explorereditorbridge.internal.EditorContext;
+import org.eclipse.emf.ecp.internal.ui.view.emf.AdapterFactoryLabelProvider;
+import org.eclipse.emf.ecp.spi.core.InternalProvider;
+import org.eclipse.emf.ecp.spi.ui.UIProvider;
+import org.eclipse.emf.edit.provider.IItemLabelProvider;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.ScrolledComposite;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
 
 public class ECPE4Editor {
 
-	public ECPE4Editor() {
+	public static final java.lang.String INPUT = "ecpEditorInput";
+	private MPart part;
+	private EObject modelElement;
+	private Adapter adapter;
+	private Composite composite;
+	private ScrolledComposite sc;
 
+	@Inject
+	public ECPE4Editor(Composite composite) {
+		this.composite = composite;
+		sc = new ScrolledComposite(composite, SWT.V_SCROLL
+				| SWT.H_SCROLL);
 	}
 
-	@PostConstruct
-	public void construct(Composite composite, @Named(IServiceConstants.ACTIVE_SELECTION) @Optional EObject modelElement) {
-		// IEditorCompositeProvider compositeProvider=EditorFactory.INSTANCE.getEditorComposite(modelElementContext);
-		// Composite editorComposite=compositeProvider.createUI(composite);
-		if (modelElement == null)
+	@Inject
+	public void setInput(@Optional
+			@Named(INPUT) EObject modelElement, @Optional ECPProject ecpProject, MPart part) {
+		if(modelElement==null||ecpProject==null){
 			return;
-		ECPEditorContext editorContext = new EditorContext(modelElement, ECPUtil.getECPProjectManager().getProject(
-			modelElement), composite.getShell());
-		IEditorCompositeProvider editorPageContent = EditorFactory.INSTANCE.getEditorComposite(editorContext);
-		ScrolledComposite sc = new ScrolledComposite(composite, SWT.V_SCROLL|SWT.H_SCROLL);
+		}
+		this.part=part;
+		this.modelElement=modelElement;
+		ECPEditorContext editorContext = new EditorContext(modelElement,
+				ecpProject, composite.getShell());
+		IEditorCompositeProvider editorPageContent = EditorFactory.INSTANCE
+				.getEditorComposite(editorContext);
+		
 
 		Composite createUI = editorPageContent.createUI(sc);
 		sc.setExpandHorizontal(true);
 		sc.setExpandVertical(true);
 		sc.setContent(createUI);
 		sc.setMinSize(createUI.computeSize(SWT.DEFAULT, SWT.DEFAULT));
+
+//		composite.layout();
+		
+		updateImageAndText();
+		adapter = new AdapterImpl(){
+
+			/* (non-Javadoc)
+			 * @see org.eclipse.emf.common.notify.impl.AdapterImpl#notifyChanged(org.eclipse.emf.common.notify.Notification)
+			 */
+			@Override
+			public void notifyChanged(Notification msg) {
+				Display.getDefault().asyncExec(new Runnable() {
+					public void run() {
+						updateImageAndText();
+					}
+				});
+			}
+			
+		};
+		modelElement.eAdapters().add(adapter); 		
+	}
+	
+	@PreDestroy
+	void dispose(){
+		modelElement.eAdapters().remove(adapter);
+	}
+
+	private void updateImageAndText() {
+		part.setLabel(UIProvider.EMF_LABEL_PROVIDER.getText(modelElement));
+	    part.setTooltip(UIProvider.EMF_LABEL_PROVIDER.getText(modelElement));
+
+	    AdapterFactoryLabelProvider provider = new AdapterFactoryLabelProvider(InternalProvider.EMF_ADAPTER_FACTORY);
+	    IItemLabelProvider itemLabelProvider = (IItemLabelProvider)provider.getAdapterFactory().adapt(modelElement, IItemLabelProvider.class);
+	    
+	    URL url = (URL) itemLabelProvider.getImage(modelElement);
+
+	}
+	
+	@Focus
+	void setFocus(){
+		if(sc!=null){
+			sc.setFocus();
+		}
 	}
 }