Initial fix for Bug 341010 - "Reset Perspective" will not restore view that was dragged into editor area
diff --git a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/WBWRenderer.java b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/WBWRenderer.java
index cc73135..75d4e94 100644
--- a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/WBWRenderer.java
+++ b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/WBWRenderer.java
@@ -23,7 +23,9 @@
 import org.eclipse.e4.core.di.annotations.Optional;
 import org.eclipse.e4.core.services.events.IEventBroker;
 import org.eclipse.e4.core.services.log.Logger;
+import org.eclipse.e4.ui.internal.workbench.Activator;
 import org.eclipse.e4.ui.internal.workbench.E4Workbench;
+import org.eclipse.e4.ui.internal.workbench.Policy;
 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;
@@ -109,6 +111,7 @@
 	private EventHandler shellUpdater;
 	private EventHandler visibilityHandler;
 	private EventHandler sizeHandler;
+	private EventHandler childHandler;
 
 	public WBWRenderer() {
 		super();
@@ -131,9 +134,7 @@
 				parent = ph.getParent();
 			}
 			if (parent instanceof MPartStack) {
-				parent.getTags().remove("active"); //$NON-NLS-1$
-				if (parent.getWidget() != null)
-					setCSSInfo(parent, parent.getWidget());
+				styleStack((MPartStack) parent, false);
 			} else {
 				if (activePart.getWidget() != null)
 					setCSSInfo(activePart, activePart.getWidget());
@@ -150,14 +151,23 @@
 				parent = ph.getParent();
 			}
 			if (parent instanceof MPartStack && parent.getWidget() != null) {
-				parent.getTags().add("active"); //$NON-NLS-1$
-				setCSSInfo(parent, parent.getWidget());
+				styleStack((MPartStack) parent, true);
 			} else if (activePart.getWidget() != null) {
 				setCSSInfo(activePart, activePart.getWidget());
 			}
 		}
 	}
 
+	private void styleStack(MPartStack stack, boolean active) {
+		if (!active)
+			stack.getTags().remove("active"); //$NON-NLS-1$
+		else
+			stack.getTags().add("active"); //$NON-NLS-1$
+
+		if (stack.getWidget() != null)
+			setCSSInfo(stack, stack.getWidget());
+	}
+
 	private void closeDetachedWindow(MWindow window) {
 		EPartService partService = (EPartService) window.getContext().get(
 				EPartService.class.getName());
@@ -297,6 +307,40 @@
 
 		eventBroker.subscribe(UIEvents.buildTopic(UIEvents.Window.TOPIC),
 				sizeHandler);
+
+		childHandler = new EventHandler() {
+			public void handleEvent(Event event) {
+				// Track additions/removals of the active part and keep its
+				// stack styled correctly
+				Object changedObj = event
+						.getProperty(UIEvents.EventTags.ELEMENT);
+				if (!(changedObj instanceof MPartStack))
+					return;
+				MPartStack stack = (MPartStack) changedObj;
+
+				String eventType = (String) event
+						.getProperty(UIEvents.EventTags.TYPE);
+				if (UIEvents.EventTypes.ADD.equals(eventType)) {
+					MUIElement added = (MUIElement) event
+							.getProperty(UIEvents.EventTags.NEW_VALUE);
+					if (added == activePart) {
+						styleStack(stack, true);
+					}
+				} else if (UIEvents.EventTypes.REMOVE.equals(eventType)) {
+					Activator.trace(Policy.DEBUG_RENDERER,
+							"Child Removed", null); //$NON-NLS-1$
+					MUIElement removed = (MUIElement) event
+							.getProperty(UIEvents.EventTags.OLD_VALUE);
+					if (removed == activePart) {
+						styleStack(stack, false);
+					}
+				}
+			}
+		};
+
+		eventBroker.subscribe(UIEvents.buildTopic(
+				UIEvents.ElementContainer.TOPIC,
+				UIEvents.ElementContainer.CHILDREN), childHandler);
 	}
 
 	@PreDestroy
@@ -304,6 +348,7 @@
 		eventBroker.unsubscribe(shellUpdater);
 		eventBroker.unsubscribe(visibilityHandler);
 		eventBroker.unsubscribe(sizeHandler);
+		eventBroker.unsubscribe(childHandler);
 	}
 
 	public Object createWidget(MUIElement element, Object parent) {
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 bf287db..d639473 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
@@ -269,7 +269,7 @@
 				if (newLocation == EModelService.IN_SHARED_AREA
 						|| newLocation == EModelService.OUTSIDE_PERSPECTIVE) {
 					MWindow topWin = modelService.getTopLevelWindowFor(added);
-					modelService.removeLocalPlaceholders(topWin, null);
+					modelService.hideLocalPlaceholders(topWin, null);
 				}
 			} else if (UIEvents.EventTypes.REMOVE.equals(eventType)) {
 				Activator.trace(Policy.DEBUG_RENDERER, "Child Removed", null); //$NON-NLS-1$
diff --git a/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/ModelServiceImpl.java b/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/ModelServiceImpl.java
index 4fa841a..e61d786 100644
--- a/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/ModelServiceImpl.java
+++ b/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/ModelServiceImpl.java
@@ -42,6 +42,7 @@
 import org.eclipse.e4.ui.model.application.ui.menu.MToolControl;
 import org.eclipse.e4.ui.model.internal.ModelUtils;
 import org.eclipse.e4.ui.workbench.modeling.EModelService;
+import org.eclipse.e4.ui.workbench.modeling.EPartService;
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.emf.ecore.util.EcoreUtil;
 
@@ -368,11 +369,27 @@
 	 */
 	public MPlaceholder findPlaceholderFor(MWindow window, MUIElement element) {
 		List<MPlaceholder> phList = findPerspectiveElements(window, null, MPlaceholder.class, null);
+		List<MPlaceholder> elementRefs = new ArrayList<MPlaceholder>();
 		for (MPlaceholder ph : phList) {
 			if (ph.getRef() == element)
-				return ph;
+				elementRefs.add(ph);
 		}
-		return null;
+
+		if (elementRefs.size() == 0)
+			return null;
+
+		if (elementRefs.size() == 1)
+			return elementRefs.get(0);
+
+		// If there is more than one placeholder then return the one in the shared area
+		for (MPlaceholder refPh : elementRefs) {
+			int loc = getElementLocation(refPh);
+			if ((loc & IN_SHARED_AREA) != 0)
+				return refPh;
+		}
+
+		// Just return the first one
+		return elementRefs.get(0);
 	}
 
 	/*
@@ -717,6 +734,19 @@
 		}
 		persp.getWindows().clear();
 
+		// Remove any views (Placeholders) from the shared area
+		EPartService ps = window.getContext().get(EPartService.class);
+		List<MArea> areas = findElements(window, null, MArea.class, null);
+		if (areas.size() == 1) {
+			MArea area = areas.get(0);
+			List<MPlaceholder> phList = findElements(area, null, MPlaceholder.class, null);
+			for (MPlaceholder ph : phList) {
+				ps.hidePart((MPart) ph.getRef());
+
+				ph.getParent().getChildren().remove(ph);
+			}
+		}
+
 		// Remove any minimized stacks for this perspective
 		List<MTrimBar> bars = findElements(window, null, MTrimBar.class, null);
 		List<MToolControl> toRemove = new ArrayList<MToolControl>();
@@ -875,7 +905,7 @@
 	 * .ui.model.application.ui.basic.MWindow,
 	 * org.eclipse.e4.ui.model.application.ui.advanced.MPerspective)
 	 */
-	public void removeLocalPlaceholders(MWindow window, MPerspective perspective) {
+	public void hideLocalPlaceholders(MWindow window, MPerspective perspective) {
 		List<MPlaceholder> globals = findElements(window, null, MPlaceholder.class, null,
 				OUTSIDE_PERSPECTIVE | IN_SHARED_AREA);
 
@@ -892,8 +922,8 @@
 			for (MPlaceholder local : locals) {
 				for (MPlaceholder global : globals) {
 					if (global.getRef() == local.getRef()) {
+						local.setToBeRendered(false);
 						MElementContainer<MUIElement> localParent = local.getParent();
-						local.getParent().getChildren().remove(local);
 						setStackVisibility(localParent);
 					}
 				}
diff --git a/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/workbench/modeling/EModelService.java b/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/workbench/modeling/EModelService.java
index a7ef3c2..8b25baf 100644
--- a/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/workbench/modeling/EModelService.java
+++ b/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/workbench/modeling/EModelService.java
@@ -405,12 +405,16 @@
 
 	/**
 	 * This method ensures that there will never be two placeholders for the same referenced element
-	 * visible in the presentation at the same time. It does this by removing placeholders which are
+	 * visible in the presentation at the same time. It does this by hiding placeholders which are
 	 * contained in any MPerspective if there is a placeholder for the element in any 'shared' area
-	 * (i.e. visible regardless of which perspective is visible.
+	 * (i.e. visible regardless of which perspective is visible) by setting its 'toBeRendered' state
+	 * to <code>false</code>.
 	 * 
 	 * @param window
+	 *            The window to modify
 	 * @param perspective
+	 *            if non-null specifies the specific perspective to modify, otherwise all
+	 *            perspectives in the window are checked
 	 */
-	public void removeLocalPlaceholders(MWindow window, MPerspective perspective);
+	public void hideLocalPlaceholders(MWindow window, MPerspective perspective);
 }