Bug 380830 - Don't enable sectional selection commands when section hidden
diff --git a/bundles/org.eclipse.orion.client.core/web/orion/commands.js b/bundles/org.eclipse.orion.client.core/web/orion/commands.js
index bcc29a9..6ffa6d0 100644
--- a/bundles/org.eclipse.orion.client.core/web/orion/commands.js
+++ b/bundles/org.eclipse.orion.client.core/web/orion/commands.js
@@ -637,7 +637,7 @@
 			if (!this._contributionsByScopeId[scopeId]) {
 				this._contributionsByScopeId[scopeId] = {};
 			}
-			this._contributionsByScopeId.localSelectionService = selectionService;
+			this._contributionsByScopeId[scopeId].localSelectionService = selectionService;
 		},
 		
 		/**
diff --git a/bundles/org.eclipse.orion.client.core/web/orion/navoutliner.js b/bundles/org.eclipse.orion.client.core/web/orion/navoutliner.js
index e7c63f1..e11d1ce 100644
--- a/bundles/org.eclipse.orion.client.core/web/orion/navoutliner.js
+++ b/bundles/org.eclipse.orion.client.core/web/orion/navoutliner.js
@@ -157,7 +157,7 @@
 
 		render: function(favorites, serviceRegistry) {
 			var commandService = this.commandService;
-
+			var that = this;
 			if (serviceRegistry) {
 				var allReferences = serviceRegistry.getServiceReferences("orion.core.file"); //$NON-NLS-0$
 				// top level folder outline if there is more than one file service.  We never show this if there is only one file service,
@@ -171,7 +171,13 @@
 							preferenceService: serviceRegistry.getService("orion.core.preference"), //$NON-NLS-0$
 							canHide: true,
 							useAuxStyle: true,
-							slideout: true
+							slideout: true,
+							onExpandCollapse: function(isExpanded, section) {
+								dojo.empty(section.selectionNode);
+								if (isExpanded) {
+									commandService.renderCommands(section.selectionNode.id, section.selectionNode, null, that, "button"); //$NON-NLS-0$
+								}
+							}
 						});
 					}
 					this.explorer = new NavOutlineExplorer(serviceRegistry, this.fileSystemSelection);
@@ -193,7 +199,13 @@
 					preferenceService: serviceRegistry.getService("orion.core.preference"), //$NON-NLS-0$
 					canHide: true,
 					useAuxStyle: true,
-					slideout: true
+					slideout: true,
+					onExpandCollapse: function(isExpanded, section) {
+						dojo.empty(section.selectionNode);
+						if (isExpanded) {
+							commandService.renderCommands(section.selectionNode.id, section.selectionNode, null, that, "button"); //$NON-NLS-0$
+						}
+					}
 				});
 				this.favoritesSelection = new mSelection.Selection(serviceRegistry, "orion.favorites.selection"); //$NON-NLS-0$
 				// add commands to the fave section heading
@@ -205,7 +217,7 @@
 					var selectionTools = dojo.byId(selectionId);
 					if (selectionTools) {
 						dojo.empty(selectionTools);
-						commandService.renderCommands(selectionId, selectionTools, selections, this, "button"); //$NON-NLS-0$
+						commandService.renderCommands(selectionId, selectionTools, selections, that, "button"); //$NON-NLS-0$
 					}
 				});
 			}
diff --git a/bundles/org.eclipse.orion.client.core/web/orion/section.js b/bundles/org.eclipse.orion.client.core/web/orion/section.js
index c8a86b6..3efd693 100644
--- a/bundles/org.eclipse.orion.client.core/web/orion/section.js
+++ b/bundles/org.eclipse.orion.client.core/web/orion/section.js
@@ -23,6 +23,7 @@
 	 * @param options.canHide {boolean} [optional] if true section may be hidden

 	 * @param options.hidden {boolean} [optional] if true section will be hidden at first display

 	 * @param options.useAuxStyle {boolean} [optional] if true the section will be styled for an auxiliary pane

+	 * @param options.onExpandCollapse {function} [optional] a function that will be called when the expanded/collapsed state changes

 	 * @returns Section object

 	 */

 	function Section(parent, options) {

@@ -91,6 +92,9 @@
 		}

 		

 		this.hidden = options.hidden;

+		if (typeof(options.onExpandCollapse) === "function") { //$NON-NLS-0$

+			this._onExpandCollapse = options.onExpandCollapse;

+		}

 		this._preferenceService = options.preferenceService;

 		// initially style as hidden until we determine what needs to happen

 		dojo.style(this._contentParent, "display", "none"); //$NON-NLS-1$ //$NON-NLS-0$

@@ -204,6 +208,11 @@
 					prefs.put(id, isExpanded);

 				}); 

 			}

+			

+			// notify the client

+			if (this._onExpandCollapse) {

+				this._onExpandCollapse(isExpanded, this);

+			}

 		}

 	};

 	

diff --git a/bundles/org.eclipse.orion.client.git/web/orion/git/gitStatusExplorer.js b/bundles/org.eclipse.orion.client.git/web/orion/git/gitStatusExplorer.js
index 94eeb65..693c49f 100644
--- a/bundles/org.eclipse.orion.client.git/web/orion/git/gitStatusExplorer.js
+++ b/bundles/org.eclipse.orion.client.git/web/orion/git/gitStatusExplorer.js
@@ -300,7 +300,13 @@
 				id: "unstagedSection", //$NON-NLS-0$

 				title: unstagedSortedChanges.length > 0 ? messages['Unstaged'] : messages["No Unstaged Changes"],

 				content: '<div id="unstagedNode"></div>', //$NON-NLS-0$

-				canHide: true

+				canHide: true,

+				onExpandCollapse: function(isExpanded, section) {

+					dojo.empty(section.selectionNode);

+					if (isExpanded) {

+						that.commandService.renderCommands(section.selectionNode.id, section.selectionNode, null, that, "button", {"Clone": repository}); //$NON-NLS-1$ //$NON-NLS-0$

+					}

+				}

 			});

 			

 			this.commandService.registerCommandContribution(unstagedSection.actionsNode.id, "orion.explorer.expandAll", 200); //$NON-NLS-1$ //$NON-NLS-0$

@@ -311,8 +317,10 @@
 			this.commandService.registerCommandContribution(unstagedSection.selectionNode.id, "eclipse.orion.git.checkoutCommand", 300); //$NON-NLS-0$	

 

 			if (!this.unstagedOnce){

-				if (!this.unstagedSelection)

+				if (!this.unstagedSelection) {

 					this.unstagedSelection = new mSelection.Selection(this.registry, "orion.unstagedSection.selection"); //$NON-NLS-0$

+					this.commandService.registerSelectionService(unstagedSection.selectionNode.id, this.unstagedSelection);

+				}

 				

 				this.registry.getService("orion.unstagedSection.selection").addEventListener("selectionChanged", function(singleSelection, selections) { //$NON-NLS-1$ //$NON-NLS-0$

 					var selectionTools = dojo.byId(unstagedSection.selectionNode.id);

@@ -491,7 +499,13 @@
 				title: stagedSortedChanges.length > 0 ? messages['Staged'] : messages["No Staged Changes"],

 				content: '<div id="stagedNode"></div>', //$NON-NLS-0$

 				slideout: true,

-				canHide: true

+				canHide: true,

+				onExpandCollapse: function(isExpanded, section) {

+					dojo.empty(section.selectionNode);

+					if (isExpanded) {

+						that.commandService.renderCommands(section.selectionNode.id, section.selectionNode, null, that, "button", {"Clone": repository}); //$NON-NLS-0$ //$NON-NLS-1$

+					}

+				}

 			});

 			

 			this.commandService.registerCommandContribution(stagedSection.actionsNode.id, "eclipse.orion.git.commitCommand", 100); //$NON-NLS-0$

@@ -501,8 +515,10 @@
 			this.commandService.registerCommandContribution(stagedSection.selectionNode.id, "eclipse.orion.git.unstageCommand", 100); //$NON-NLS-0$

 			

 			if (!this.stagedOnce){

-				if (!this.stagedSelection)

+				if (!this.stagedSelection) {

 					this.stagedSelection = new mSelection.Selection(this.registry, "orion.stagedSection.selection"); //$NON-NLS-0$

+					this.commandService.registerSelectionService(stagedSection.selectionNode.id, this.stagedSelection);

+				}

 				

 				this.registry.getService("orion.stagedSection.selection").addEventListener("selectionChanged", function(singleSelection, selections) { //$NON-NLS-1$ //$NON-NLS-0$

 					var selectionTools = dojo.byId(stagedSection.selectionNode.id);