Bug 470771 - Reparenting of context is not happening if eg items are
moved from window a to window b
diff --git a/bundles/runtime/org.eclipse.fx.ui.workbench.renderers.base/src/org/eclipse/fx/ui/workbench/renderers/base/BaseCompositePartRenderer.java b/bundles/runtime/org.eclipse.fx.ui.workbench.renderers.base/src/org/eclipse/fx/ui/workbench/renderers/base/BaseCompositePartRenderer.java
index fbb3df2..4c721a3 100644
--- a/bundles/runtime/org.eclipse.fx.ui.workbench.renderers.base/src/org/eclipse/fx/ui/workbench/renderers/base/BaseCompositePartRenderer.java
+++ b/bundles/runtime/org.eclipse.fx.ui.workbench.renderers.base/src/org/eclipse/fx/ui/workbench/renderers/base/BaseCompositePartRenderer.java
@@ -233,6 +233,7 @@
 				hideChild(parent, element);
 			}
 		}
+		checkSelectedElement(parent);
 	}
 
 	void handleSelectedElement(MCompositePart parent, MPartSashContainerElement oldElement, MPartSashContainerElement newElement) {
diff --git a/bundles/runtime/org.eclipse.fx.ui.workbench.renderers.base/src/org/eclipse/fx/ui/workbench/renderers/base/BaseRenderer.java b/bundles/runtime/org.eclipse.fx.ui.workbench.renderers.base/src/org/eclipse/fx/ui/workbench/renderers/base/BaseRenderer.java
index 75e6354..ebaeeab 100755
--- a/bundles/runtime/org.eclipse.fx.ui.workbench.renderers.base/src/org/eclipse/fx/ui/workbench/renderers/base/BaseRenderer.java
+++ b/bundles/runtime/org.eclipse.fx.ui.workbench.renderers.base/src/org/eclipse/fx/ui/workbench/renderers/base/BaseRenderer.java
@@ -18,6 +18,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Optional;
 
 import javax.inject.Inject;
 
@@ -31,6 +32,7 @@
 import org.eclipse.e4.ui.model.application.MApplicationElement;
 import org.eclipse.e4.ui.model.application.ui.MContext;
 import org.eclipse.e4.ui.model.application.ui.MCoreExpression;
+import org.eclipse.e4.ui.model.application.ui.MElementContainer;
 import org.eclipse.e4.ui.model.application.ui.MUIElement;
 import org.eclipse.e4.ui.model.application.ui.MUILabel;
 import org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder;
@@ -51,6 +53,8 @@
 import org.eclipse.fx.ui.services.Constants;
 import org.eclipse.fx.ui.workbench.base.rendering.ElementRenderer;
 import org.eclipse.fx.ui.workbench.renderers.base.widget.WPropertyChangeHandler.WPropertyChangeEvent;
+import org.eclipse.fx.ui.workbench.renderers.base.widget.WLayoutedWidget;
+import org.eclipse.fx.ui.workbench.renderers.base.widget.WPlaceholderWidget;
 import org.eclipse.fx.ui.workbench.renderers.base.widget.WWidget;
 import org.eclipse.fx.ui.workbench.renderers.base.widget.WWidget.WidgetState;
 import org.eclipse.fx.ui.workbench.services.EModelStylingService;
@@ -786,6 +790,94 @@
 	 */
 	protected abstract void doProcessContent(@NonNull M element);
 
+	/**
+	 * Check that the selected element is a valid one or if not set the
+	 * selection to the first item in the container or <code>null</code>
+	 * 
+	 * @param element
+	 *            the element
+	 */
+	@SuppressWarnings("null")
+	protected void checkSelectedElement(MUIElement element) {
+		if (element instanceof MElementContainer<?>) {
+			@SuppressWarnings("unchecked")
+			MElementContainer<MUIElement> parent = (MElementContainer<MUIElement>) element;
+			if (parent.getSelectedElement() != null) {
+				if (parent.getChildren().isEmpty()) {
+					parent.setSelectedElement(null);
+				} else {
+					Optional<MUIElement> first = parent.getChildren().stream().filter(c -> c == parent.getSelectedElement() && isChildRenderedAndVisible(c)).findFirst();
+					if (!first.isPresent()) {
+						first = parent.getChildren().stream().filter(c -> isChildRenderedAndVisible(c)).findFirst();
+						if (first.isPresent()) {
+							parent.setSelectedElement(first.get());
+						} else {
+							parent.setSelectedElement(null);
+						}
+					}
+				}
+			}
+		}
+	}
+
+	/**
+	 * Fix the context hierarchy
+	 * 
+	 * @param elements
+	 *            the elements
+	 */
+	protected void fixContextHierarchy(@NonNull Collection<@NonNull ? extends MUIElement> elements) {
+		elements.stream().forEach(this::fixContextHierarchy);
+	}
+
+	/**
+	 * Fix the context hierarchy for given element
+	 * 
+	 * @param element
+	 *            the element
+	 */
+	protected void fixContextHierarchy(@NonNull MUIElement element) {
+		MUIElement tmp = element;
+		if (!tmp.isToBeRendered()) {
+			return;
+		}
+
+		if (tmp instanceof MPlaceholder && tmp.getWidget() != null) {
+			MPlaceholder ph = (MPlaceholder) tmp;
+			MUIElement ref = ph.getRef();
+
+			if (ref.getCurSharedRef() != ph) {
+				ref.setCurSharedRef(ph);
+				WPlaceholderWidget placeholder = (WPlaceholderWidget) ph.getWidget();
+				@SuppressWarnings("unchecked")
+				WLayoutedWidget<MUIElement> content = (WLayoutedWidget<MUIElement>) ref.getWidget();
+				placeholder.setContent(content);
+			}
+
+			tmp = ref;
+		}
+
+		if (tmp instanceof MContext) {
+			IEclipseContext context = ((MContext) tmp).getContext();
+			if (context != null) {
+				IEclipseContext newParentContext = this.modelService.getContainingContext(tmp);
+				if (context.getParent() != newParentContext) {
+					Util.setParentContext(context, newParentContext);
+				}
+			}
+		}
+
+		// Currently not supported in the model but will very likely be in
+		// future
+		if (tmp instanceof MElementContainer<?>) {
+			MElementContainer<?> container = (MElementContainer<?>) tmp;
+			List<MUIElement> kids = new ArrayList<MUIElement>(container.getChildren());
+			for (MUIElement childElement : kids) {
+				fixContextHierarchy(childElement);
+			}
+		}
+	}
+
 	@Override
 	public void focus(@NonNull MUIElement element) {
 		if (element.getWidget() instanceof WWidget) {
diff --git a/bundles/runtime/org.eclipse.fx.ui.workbench.renderers.base/src/org/eclipse/fx/ui/workbench/renderers/base/BaseSashRenderer.java b/bundles/runtime/org.eclipse.fx.ui.workbench.renderers.base/src/org/eclipse/fx/ui/workbench/renderers/base/BaseSashRenderer.java
index a0df323..4bf19ed 100755
--- a/bundles/runtime/org.eclipse.fx.ui.workbench.renderers.base/src/org/eclipse/fx/ui/workbench/renderers/base/BaseSashRenderer.java
+++ b/bundles/runtime/org.eclipse.fx.ui.workbench.renderers.base/src/org/eclipse/fx/ui/workbench/renderers/base/BaseSashRenderer.java
@@ -187,6 +187,7 @@
 				hideChild(parent, element);
 			}
 		}
+		checkSelectedElement(parent);
 	}
 
 	void handleSelectedElement(MPartSashContainer parent, MPartSashContainerElement oldElement, MPartSashContainerElement newElement) {
diff --git a/bundles/runtime/org.eclipse.fx.ui.workbench.renderers.base/src/org/eclipse/fx/ui/workbench/renderers/base/BaseStackRenderer.java b/bundles/runtime/org.eclipse.fx.ui.workbench.renderers.base/src/org/eclipse/fx/ui/workbench/renderers/base/BaseStackRenderer.java
index 9636516..6ea8c1c 100755
--- a/bundles/runtime/org.eclipse.fx.ui.workbench.renderers.base/src/org/eclipse/fx/ui/workbench/renderers/base/BaseStackRenderer.java
+++ b/bundles/runtime/org.eclipse.fx.ui.workbench.renderers.base/src/org/eclipse/fx/ui/workbench/renderers/base/BaseStackRenderer.java
@@ -15,7 +15,6 @@
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Optional;
 
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
@@ -24,7 +23,6 @@
 import org.eclipse.e4.core.contexts.IEclipseContext;
 import org.eclipse.e4.core.services.events.IEventBroker;
 import org.eclipse.e4.ui.model.application.MApplication;
-import org.eclipse.e4.ui.model.application.ui.MContext;
 import org.eclipse.e4.ui.model.application.ui.MElementContainer;
 import org.eclipse.e4.ui.model.application.ui.MUIElement;
 import org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder;
@@ -38,7 +36,6 @@
 import org.eclipse.fx.ui.workbench.base.rendering.RendererFactory;
 import org.eclipse.fx.ui.workbench.renderers.base.widget.WCallback;
 import org.eclipse.fx.ui.workbench.renderers.base.widget.WLayoutedWidget;
-import org.eclipse.fx.ui.workbench.renderers.base.widget.WPlaceholderWidget;
 import org.eclipse.fx.ui.workbench.renderers.base.widget.WStack;
 import org.eclipse.fx.ui.workbench.renderers.base.widget.WStack.WStackItem;
 import org.eclipse.fx.ui.workbench.services.ELifecycleService;
@@ -334,7 +331,10 @@
 				}
 			}
 		}
-
+		
+		//TODO THIS NEEDS TO BE MOVED TO THE CHILD ADDITION HANDLER!!!!
+		fixContextHierarchy(elements);
+		
 		// Ensure an element is selected see 436659
 		if( parent.getSelectedElement() == null ) {
 			if( ! widget.getItems().isEmpty() ) {
@@ -376,21 +376,7 @@
 			parent.getChildren().removeAll(removeOnHideList);
 		}
 		
-		if( parent.getSelectedElement() != null ) {
-			if( parent.getChildren().isEmpty() ) {
-				parent.setSelectedElement(null);
-			} else {
-				Optional<MStackElement> first = parent.getChildren().stream().filter( c -> c == parent.getSelectedElement() && c.isVisible()).findFirst();
-				if( ! first.isPresent()) {
-					first = parent.getChildren().stream().filter( c -> c.isVisible()).findFirst();
-					if( first.isPresent() ) {
-						parent.setSelectedElement(first.get());
-					} else {
-						parent.setSelectedElement(null);
-					}
-				}
-			}
-		}
+		checkSelectedElement(parent);
 	}
 
 	@NonNull
@@ -422,7 +408,7 @@
 		for (WStackItem<I, IC> i : stack.getItems()) {
 			if (i.getDomElement() == newElement) {
 				stack.selectItem(idx);
-				showElementRecursive(newElement);
+				fixContextHierarchy(newElement);
 				return;
 			}
 			idx++;
@@ -433,7 +419,7 @@
 		childRendered(parent, newElement);
 		stack.selectItem(parent.getChildren().indexOf(newElement));
 		// TODO Should we do the traversal before???
-		showElementRecursive(newElement);
+		fixContextHierarchy(newElement);
 	}
 
 	boolean handleStackItemClose(@NonNull MStackElement e, @NonNull WStackItem<I, IC> item) {
@@ -531,47 +517,4 @@
 			container.getChildren().remove(changedObj);
 		}
 	}
-
-	@SuppressWarnings("null")
-	private void showElementRecursive(@NonNull MUIElement tmp) {
-		MUIElement element = tmp;
-		if (!element.isToBeRendered()) {
-			return;
-		}
-
-		if (element instanceof MPlaceholder && element.getWidget() != null) {
-			MPlaceholder ph = (MPlaceholder) element;
-			MUIElement ref = ph.getRef();
-
-			if (ref.getCurSharedRef() != ph) {
-				ref.setCurSharedRef(ph);
-				WPlaceholderWidget placeholder = (WPlaceholderWidget) ph.getWidget();
-				@SuppressWarnings("unchecked")
-				WLayoutedWidget<MUIElement> content = (WLayoutedWidget<MUIElement>) ref.getWidget();
-				placeholder.setContent(content);
-			}
-
-			element = ref;
-		}
-
-		if (element instanceof MContext) {
-			IEclipseContext context = ((MContext) element).getContext();
-			if (context != null) {
-				IEclipseContext newParentContext = this.modelService.getContainingContext(element);
-				if (context.getParent() != newParentContext) {
-					Util.setParentContext(context, newParentContext);
-				}
-			}
-		}
-
-		// Currently not supported in the model but will very likely be in
-		// future
-		if (element instanceof MElementContainer<?>) {
-			MElementContainer<?> container = (MElementContainer<?>) element;
-			List<MUIElement> kids = new ArrayList<MUIElement>(container.getChildren());
-			for (MUIElement childElement : kids) {
-				showElementRecursive(childElement);
-			}
-		}
-	}
 }
\ No newline at end of file