[r3.6.1 maintenance] Bug 325797 - views automatically opened based on debug context are hidden/closed on perspective switch
diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/views/ViewContextService.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/views/ViewContextService.java
index 74ba6ef..8a62fa8 100644
--- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/views/ViewContextService.java
+++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/views/ViewContextService.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- *  Copyright (c) 2006, 2009 IBM Corporation and others.
+ *  Copyright (c) 2006, 2010 IBM Corporation and others.
  *  All rights reserved. This program and the accompanying materials
  *  are made available under the terms of the Eclipse Public License v1.0
  *  which accompanies this distribution, and is available at
@@ -16,9 +16,9 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
 import java.util.StringTokenizer;
-import java.util.Map.Entry;
 
 import org.eclipse.core.commands.common.NotDefinedException;
 import org.eclipse.core.commands.contexts.Context;
@@ -103,6 +103,12 @@
 	
 	private IDebugContextService fDebugContextService;
 	
+	/**
+	 * Perspective that is currently being de-activated.  Used to determine
+	 * when to ignore active context changes.  
+	 */
+	private IPerspectiveDescriptor fActivePerspective;
+	
 	// base debug context
 	public static final String DEBUG_CONTEXT= "org.eclipse.debug.ui.debugging"; //$NON-NLS-1$
 	
@@ -219,9 +225,9 @@
     	 * Activates the views in this context hierarchy. Views are activated top down, allowing
     	 * sub-contexts to override settings in a parent context.
     	 */
-    	public void activateChain(IWorkbenchPage page) {
+    	public void activateChain(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
     		initializeChain();
-    		doActivation(page, fAllViewBindingIds, fAllConetxtIds);
+    		doActivation(page, perspective, fAllViewBindingIds, fAllConetxtIds);
     	}	
     	
     	/**
@@ -232,18 +238,18 @@
     	 * @param viewIds id's of views to activate
     	 * @param contextIds associated contexts that are activated
     	 */
-    	private void doActivation(IWorkbenchPage page, String[] viewIds, String[] contextIds) {
+    	private void doActivation(IWorkbenchPage page, IPerspectiveDescriptor perspective, String[] viewIds, String[] contextIds) {
     		// note activation of all the relevant contexts
     		for (int i = 0; i < contextIds.length; i++) {
 				addActivated(contextIds[i]);
 			}
     		// set the active context to be this
-    		setActive(page.getPerspective(), getId());
+    		setActive(perspective, getId());
     		// activate the view bindings
     		for (int i = 0; i < viewIds.length; i++) {
 				String viewId = viewIds[i];
 				ViewBinding binding = (ViewBinding) fAllViewIdToBindings.get(viewId);
-				binding.activated(page);
+				binding.activated(page, perspective);
 			}
     		// bring most relevant views to top
     		for (int i = 0; i < viewIds.length; i++) {
@@ -293,7 +299,7 @@
     	 * 
     	 * @param page workbench page
     	 */
-    	public void deactivate(IWorkbenchPage page) {
+    	public void deactivate(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
 			removeActivated(getId());
 			if (isActiveContext(getId())) {
 				setActive(page.getPerspective(), null);
@@ -301,7 +307,7 @@
 			for (int i = 0; i < fViewBindingIds.length; i++) {
 				String viewId = fViewBindingIds[i];
 				ViewBinding binding = (ViewBinding) fAllViewIdToBindings.get(viewId);
-				binding.deactivated(page);
+				binding.deactivated(page, perspective);
 			}
     	}
     	
@@ -401,15 +407,15 @@
          * Returns whether this view was opened by the user in the active perspective.
          * @return
          */
-        public boolean isUserOpened() {
-            return fUserOpened.contains(getActivePerspective().getId());
+        public boolean isUserOpened(IPerspectiveDescriptor perspective) {
+            return fUserOpened.contains(perspective.getId());
         }
         
         /**
          * Returns whether this view was closed by the user in the active perspective
          * @return
          */
-        public boolean isUserClosed() {
+        public boolean isUserClosed(IPerspectiveDescriptor perspective) {
             return fUserClosed.contains(getActivePerspective().getId());
         }
         
@@ -421,8 +427,8 @@
          * 
          * @return
          */
-        public boolean isDefault() {
-            String id = getActivePerspective().getId();
+        public boolean isDefault(IPerspectiveDescriptor perspective) {
+            String id = perspective.getId();
             if (IDebugUIConstants.ID_DEBUG_PERSPECTIVE.equals(id)) {
                 return fgBaseDebugViewIds.contains(getViewId());
             }
@@ -461,8 +467,8 @@
          * 
          * @param page
          */
-        public void activated(IWorkbenchPage page) {
-            if (!isUserClosed()) {
+        public void activated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
+            if (!isUserClosed(perspective)) {
                 if (isAutoOpen()) {
                     try {
                         fIgnoreChanges = true;
@@ -511,9 +517,9 @@
          * 
          * @param page
          */
-        public void deactivated(IWorkbenchPage page) {
-            if (!isUserOpened()) {
-                if (isAutoClose() && !isDefault()) {
+        public void deactivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
+            if (!isUserOpened(perspective)) {
+                if (isAutoClose() && !isDefault(perspective)) {
                     IViewReference reference = page.findViewReference(getViewId());
                     if (reference != null) {
                         try {
@@ -598,6 +604,10 @@
 		getDebugContextService().addDebugContextListener(this);
 		DebugUIPlugin.getDefault().getPluginPreferences().addPropertyChangeListener(this);
 		fContextService.addContextManagerListener(this);
+	    IWorkbenchPage page = fWindow.getActivePage();
+	    if (page != null) {
+	        fActivePerspective = page.getPerspective();
+	    }
 	}
 	
 	public void dispose() {
@@ -605,6 +615,7 @@
 		getDebugContextService().removeDebugContextListener(this);
 		DebugUIPlugin.getDefault().getPluginPreferences().removePropertyChangeListener(this);
 		fContextService.removeContextManagerListener(this);
+        fActivePerspective = null;
 	}
 	
 	/**
@@ -715,11 +726,7 @@
 	 * @return active perspective or <code>null</code>
 	 */
 	private IPerspectiveDescriptor getActivePerspective() {
-		IWorkbenchPage activePage = fWindow.getActivePage();
-		if (activePage != null) {
-			return activePage.getPerspective();
-		}
-		return null;
+        return fActivePerspective;
 	}
 	
 	/**
@@ -788,12 +795,12 @@
 										while (contexts.hasNext()) {
 											String contextId = (String)contexts.next();
 											if (!isActivated(contextId)) {
-												activateChain(contextId);
+												activateChain(contextId, getActivePerspective());
 											}
 											// ensure last context gets top priority
 											if (!contexts.hasNext()) {
 												if (!isActiveContext(contextId)) {
-													activateChain(contextId);
+													activateChain(contextId, getActivePerspective());
 												}
 											}
 										}
@@ -901,7 +908,7 @@
 			Iterator iterator = contexts.iterator();
 			while (iterator.hasNext()) {
 				String id = (String) iterator.next();
-				deactivate(id);
+				deactivate(id, perspective);
 			}
 		}
 	}
@@ -934,6 +941,7 @@
 	 */
 	public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
 		if (page.getWorkbenchWindow().equals(fWindow)) {
+		    fActivePerspective = perspective;
 			ISelection activeContext = getDebugContextService().getActiveContext();
 			if (activeContext != null) {
 				contextActivated(activeContext);
@@ -952,12 +960,12 @@
 	 * 
 	 * @param contextId
 	 */
-	private void activateChain(String contextId) {
+	private void activateChain(String contextId, IPerspectiveDescriptor perspective) {
 		IWorkbenchPage page = fWindow.getActivePage();
 		if (page != null) {
 			DebugContextViewBindings bindings= (DebugContextViewBindings) fContextIdsToBindings.get(contextId);
 			if (bindings != null) {
-				bindings.activateChain(page);
+				bindings.activateChain(page, perspective);
 			}
 		}
 	}
@@ -1001,7 +1009,7 @@
 	 * @see org.eclipse.core.commands.contexts.IContextManagerListener#contextManagerChanged(org.eclipse.core.commands.contexts.ContextManagerEvent)
 	 */
 	public void contextManagerChanged(ContextManagerEvent event) {
-		if (event.isActiveContextsChanged()) {
+		if (event.isActiveContextsChanged() && getActivePerspective() != null) {
 			Set disabledContexts = getDisabledContexts(event);
 			if (!disabledContexts.isEmpty()) {
 				Iterator contexts = disabledContexts.iterator();
@@ -1009,7 +1017,7 @@
 					String contextId = (String)contexts.next();
 					if (isViewConetxt(contextId)) {
 						if (isActivated(contextId)) {
-							deactivate(contextId);
+							deactivate(contextId, getActivePerspective());
 						}
 					}
 				}
@@ -1019,12 +1027,12 @@
 		}
 	}
 	
-	private void deactivate(String contextId) {
+	private void deactivate(String contextId, IPerspectiveDescriptor perspective) {
 		IWorkbenchPage page = fWindow.getActivePage();
 		if (page != null) {
 			DebugContextViewBindings bindings = (DebugContextViewBindings) fContextIdsToBindings.get(contextId);
 			if (bindings != null) {
-				bindings.deactivate(page);
+				bindings.deactivate(page, perspective);
 			}
 		}		
 	}
@@ -1118,6 +1126,7 @@
 	 */
 	public void perspectivePreDeactivate(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
 		if (page.getWorkbenchWindow().equals(fWindow)) {
+            fActivePerspective = null;
 			clean(perspective);
 		}
 	}