Fix for Bug 348215 - Reattaching a detached stack with multiple views will cause part disposal
diff --git a/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/PartRenderingEngine.java b/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/PartRenderingEngine.java
index 129dd98..e3ef320 100644
--- a/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/PartRenderingEngine.java
+++ b/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/PartRenderingEngine.java
@@ -543,6 +543,31 @@
 				}
 			}
 
+			// Reparent the context (or the kid's context)
+			if (element instanceof MContext) {
+				IEclipseContext ctxt = ((MContext) element).getContext();
+				if (ctxt != null)
+					ctxt.setParent(parentContext);
+			} else {
+				List<MContext> childContexts = modelService.findElements(
+						element, null, MContext.class, null);
+				for (MContext c : childContexts) {
+					// Ensure that we only reset the context of our direct
+					// children
+					MUIElement kid = (MUIElement) c;
+					MUIElement parent = kid.getParent();
+					if (parent == null && kid.getCurSharedRef() != null)
+						parent = kid.getCurSharedRef().getParent();
+					if (parent != element)
+						continue;
+
+					if (c.getContext() != null
+							&& c.getContext().getParent() != parentContext) {
+						c.getContext().setParent(parentContext);
+					}
+				}
+			}
+
 			// Now that we have a widget let the parent (if any) know
 			if (element.getParent() instanceof MUIElement) {
 				MElementContainer<MUIElement> parentElement = element
diff --git a/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/tests/workbench/PartRenderingEngineTests.java b/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/tests/workbench/PartRenderingEngineTests.java
index 9273849..247d866 100644
--- a/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/tests/workbench/PartRenderingEngineTests.java
+++ b/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/tests/workbench/PartRenderingEngineTests.java
@@ -2752,6 +2752,114 @@
 		assertTrue(window.getWindows().contains(detachedWindow));
 	}
 
+	public void testBug348215_PartOnlyContextReparent() {
+		MApplication application = ApplicationFactoryImpl.eINSTANCE
+				.createApplication();
+
+		MWindow window = BasicFactoryImpl.eINSTANCE.createWindow();
+		application.getChildren().add(window);
+		application.setSelectedElement(window);
+
+		MWindow detachedWindow = BasicFactoryImpl.eINSTANCE.createWindow();
+		window.getWindows().add(detachedWindow);
+
+		MPart part = BasicFactoryImpl.eINSTANCE.createPart();
+		part.setContributionURI("platform:/plugin/org.eclipse.e4.ui.tests/org.eclipse.e4.ui.tests.workbench.SampleView");
+		detachedWindow.getChildren().add(part);
+		detachedWindow.setSelectedElement(part);
+
+		application.setContext(appContext);
+		appContext.set(MApplication.class.getName(), application);
+
+		wb = new E4Workbench(application, appContext);
+		wb.createAndRunUI(window);
+
+		assertTrue(part.getContext() != null);
+		assertTrue(part.getContext().getParent() == detachedWindow.getContext());
+
+		detachedWindow.getChildren().remove(part);
+		window.getChildren().add(part);
+
+		assertTrue(part.getContext() != null);
+		assertTrue(part.getContext().getParent() == window.getContext());
+	}
+
+	public void testBug348215_PartContextReparent() {
+		MApplication application = ApplicationFactoryImpl.eINSTANCE
+				.createApplication();
+
+		MWindow window = BasicFactoryImpl.eINSTANCE.createWindow();
+		application.getChildren().add(window);
+		application.setSelectedElement(window);
+
+		MWindow detachedWindow = BasicFactoryImpl.eINSTANCE.createWindow();
+		window.getWindows().add(detachedWindow);
+
+		MPartStack stack = BasicFactoryImpl.eINSTANCE.createPartStack();
+		detachedWindow.getChildren().add(stack);
+		detachedWindow.setSelectedElement(stack);
+
+		MPart part = BasicFactoryImpl.eINSTANCE.createPart();
+		part.setContributionURI("platform:/plugin/org.eclipse.e4.ui.tests/org.eclipse.e4.ui.tests.workbench.SampleView");
+		stack.getChildren().add(part);
+		stack.setSelectedElement(part);
+
+		application.setContext(appContext);
+		appContext.set(MApplication.class.getName(), application);
+
+		wb = new E4Workbench(application, appContext);
+		wb.createAndRunUI(window);
+
+		assertTrue(part.getContext() != null);
+		assertTrue(part.getContext().getParent() == detachedWindow.getContext());
+
+		detachedWindow.getChildren().remove(stack);
+		window.getChildren().add(stack);
+
+		assertTrue(part.getContext() != null);
+		assertTrue(part.getContext().getParent() == window.getContext());
+	}
+
+	public void testBug348215_PartPlaceholderContextReparent() {
+		MApplication application = ApplicationFactoryImpl.eINSTANCE
+				.createApplication();
+
+		MWindow window = BasicFactoryImpl.eINSTANCE.createWindow();
+		application.getChildren().add(window);
+		application.setSelectedElement(window);
+
+		MPart part = BasicFactoryImpl.eINSTANCE.createPart();
+		part.setContributionURI("platform:/plugin/org.eclipse.e4.ui.tests/org.eclipse.e4.ui.tests.workbench.SampleView");
+		window.getSharedElements().add(part);
+
+		MWindow detachedWindow = BasicFactoryImpl.eINSTANCE.createWindow();
+		window.getWindows().add(detachedWindow);
+
+		MPartStack stack = BasicFactoryImpl.eINSTANCE.createPartStack();
+		detachedWindow.getChildren().add(stack);
+		detachedWindow.setSelectedElement(stack);
+
+		MPlaceholder ph = AdvancedFactoryImpl.eINSTANCE.createPlaceholder();
+		ph.setRef(part);
+		stack.getChildren().add(ph);
+		stack.setSelectedElement(ph);
+
+		application.setContext(appContext);
+		appContext.set(MApplication.class.getName(), application);
+
+		wb = new E4Workbench(application, appContext);
+		wb.createAndRunUI(window);
+
+		assertTrue(part.getContext() != null);
+		assertTrue(part.getContext().getParent() == detachedWindow.getContext());
+
+		detachedWindow.getChildren().remove(stack);
+		window.getChildren().add(stack);
+
+		assertTrue(part.getContext() != null);
+		assertTrue(part.getContext().getParent() == window.getContext());
+	}
+
 	public void testBug348069_DetachedPerspectiveWindow() {
 		MApplication application = ApplicationFactoryImpl.eINSTANCE
 				.createApplication();