bug 377500 - Need selection commands in sections
https://bugs.eclipse.org/bugs/show_bug.cgi?id=377500
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 78bd9c7..18ebd3e 100644
--- a/bundles/org.eclipse.orion.client.core/web/orion/section.js
+++ b/bundles/org.eclipse.orion.client.core/web/orion/section.js
@@ -35,7 +35,8 @@
 		}

 		this.titleNode = dojo.create( "div", { id: options.id + "Title", "class":"sectionAnchor layoutLeft", innerHTML: options.title }, this.domNode );

 		this.progressNode = dojo.create( "div", { id: options.id + "Progress", "class": "sectionProgress layoutLeft", innerHTML: "..."}, this.domNode );

-		this.actionsNode = dojo.create( "div", { id: options.id + "ActionsArea", "class":"layoutRight sectionActions"}, this.domNode );

+		this.actionsNode = dojo.create( "div", { id: options.id + "ActionArea", "class":"layoutRight sectionActions"}, this.domNode );

+		this.selectionNode = dojo.create( "div", { id: options.id + "SelectionArea", "class":"layoutRight sectionActions"}, this.domNode );

 		if(options.slideout){

 			this.slideout = '<div id="' + options.id + 'slideContainer" class="layoutBlock slideParameters slideContainer">' +

 								'<span id="' + options.id + 'slideOut" class="slide">' +

@@ -102,12 +103,12 @@
 			},

 			

 			getSelection: function(){

-				if (!this._serviceRegistry)

-					return null;

-					

-				if (!this._selection)

-					this._selection = new mSelection.Selection(this._serviceRegistry, this.id);

-				return this._selection;

+//				if (!this._serviceRegistry)

+//					return null;

+//					

+//				if (!this._selection)

+//					this._selection = new mSelection.Selection(this._serviceRegistry, this.id);

+//				return this._selection;

 			},

 			

 			_setMonitorMessage: function(monitorId, message){

diff --git a/bundles/org.eclipse.orion.client.git/web/git/git-status2.js b/bundles/org.eclipse.orion.client.git/web/git/git-status2.js
index dcbce26..3dffb5a 100644
--- a/bundles/org.eclipse.orion.client.git/web/git/git-status2.js
+++ b/bundles/org.eclipse.orion.client.git/web/git/git-status2.js
@@ -47,7 +47,7 @@
 		// define commands
 		mGitCommands.createFileCommands(serviceRegistry, commandService, explorer, "pageActions", "selectionTools");
 		mGitCommands.createGitClonesCommands(serviceRegistry, commandService, explorer, "pageActions", "selectionTools", fileClient);
-		mGitCommands.createGitStatusCommands(serviceRegistry, commandService, explorer, "pageActions", "selectionTools", fileClient);
+		mGitCommands.createGitStatusCommands(serviceRegistry, commandService, explorer);
 
 		// define the command contributions - where things appear, first the groups
 		commandService.addCommandGroup("pageActions", "eclipse.gitGroup", 100);
diff --git a/bundles/org.eclipse.orion.client.git/web/orion/git/gitCommands.js b/bundles/org.eclipse.orion.client.git/web/orion/git/gitCommands.js
index fe39f8e..8abf570 100644
--- a/bundles/org.eclipse.orion.client.git/web/orion/git/gitCommands.js
+++ b/bundles/org.eclipse.orion.client.git/web/orion/git/gitCommands.js
@@ -2237,19 +2237,8 @@
 		});
 		commandService.addCommand(openCommitCommand);
 	};
-	
-	
-	
 
-
-	
-	
-	
-	
-	
-	
-	
-	exports.createGitStatusCommands = function(serviceRegistry, commandService, explorer, toolbarId, selectionTools, fileClient) {
+	exports.createGitStatusCommands = function(serviceRegistry, commandService, explorer) {
 		
 		function displayErrorOnStatus(error) {
 			if (error.status === 401 || error.status === 403)
@@ -2270,6 +2259,13 @@
 			serviceRegistry.getService("orion.page.message").setProgressResult(display);
 		}
 		
+		function forceArray(item) {
+			if (!dojo.isArray(item)) {
+				item = [item];
+			}
+			return item;
+		}
+		
 		var stageCommand = new mCommands.Command({
 			name: "Stage",
 			tooltip: "Stage the change",
@@ -2277,20 +2273,43 @@
 			spriteClass: "gitCommandSprite",
 			id: "eclipse.orion.git.stageCommand",
 			callback: function(data) {
-				var item = data.items;
+				var items = forceArray(data.items);
 				
 				var progressService = serviceRegistry.getService("orion.page.message");
 				
-				progressService.createProgressMonitor(
-					serviceRegistry.getService("orion.git.provider").stage(item.indexURI),
-					"Staging changes").deferred.then(
-					function(jsonData){
-						dojo.hitch(explorer, explorer.changedItem)(item);
-					}, displayErrorOnStatus
-				)
+				if (items.length === 1){				
+					progressService.createProgressMonitor(
+						serviceRegistry.getService("orion.git.provider").stage(items[0].indexURI),
+						"Staging changes").deferred.then(
+						function(jsonData){
+							dojo.hitch(explorer, explorer.changedItem)(items);
+						}, displayErrorOnStatus
+					);
+				} else {
+					var paths = [];
+					for ( var i = 0; i < items.length; i++) {
+						paths[i] = items[i].path;
+					}
+					
+					progressService.createProgressMonitor(
+						serviceRegistry.getService("orion.git.provider").stageMultipleFiles(data.userData.Clone.IndexLocation, paths),
+						"Staging changes").deferred.then(
+						function(jsonData){
+							dojo.hitch(explorer, explorer.changedItem)(items);
+						}, displayErrorOnStatus
+					);
+				}			
 			},
 			visibleWhen: function(item) {
-				return !mGitUtil.isStaged(item);
+				var items = forceArray(item);
+				if (items.length === 0)
+					return false;
+
+				for (var i = 0; i < items.length; i++) {
+					if (mGitUtil.isStaged(items[i]))
+						return false; 
+				}
+				return true;
 			}
 		});	
 		
@@ -2303,20 +2322,43 @@
 			spriteClass: "gitCommandSprite",
 			id: "eclipse.orion.git.unstageCommand",
 			callback: function(data) {
-				var item = data.items;
+				var items = forceArray(data.items);
 				
 				var progressService = serviceRegistry.getService("orion.page.message");
-				
-				progressService.createProgressMonitor(
-					serviceRegistry.getService("orion.git.provider").unstage(item.indexURI, item.name),
-					"Unstaging changes").deferred.then(
-					function(jsonData){
-						dojo.hitch(explorer, explorer.changedItem)(item);
-					}, displayErrorOnStatus
-				)
+
+				if (items.length === 1){				
+					progressService.createProgressMonitor(
+						serviceRegistry.getService("orion.git.provider").unstage(items[0].indexURI, items[0].name),
+						"Staging changes").deferred.then(
+						function(jsonData){
+							dojo.hitch(explorer, explorer.changedItem)(items);
+						}, displayErrorOnStatus
+					);
+				} else {
+					var paths = [];
+					for ( var i = 0; i < items.length; i++) {
+						paths[i] = items[i].path;
+					}
+					
+					progressService.createProgressMonitor(
+						serviceRegistry.getService("orion.git.provider").unstage(data.userData.Clone.IndexLocation, paths),
+						"Staging changes").deferred.then(
+						function(jsonData){
+							dojo.hitch(explorer, explorer.changedItem)(items);
+						}, displayErrorOnStatus
+					);
+				}
 			},
 			visibleWhen: function(item) {
-				return mGitUtil.isStaged(item);
+				var items = forceArray(item);
+				if (items.length === 0)
+					return false;
+
+				for (var i = 0; i < items.length; i++) {
+					if (!mGitUtil.isStaged(items[i]))
+						return false; 
+				}
+				return true;
 			}
 		});	
 		
@@ -2342,7 +2384,7 @@
 					function(jsonData){
 						dojo.hitch(explorer, explorer.changedItem)(item);
 					}, displayErrorOnStatus
-				)
+				);
 			},
 			visibleWhen: function(item) {
 				return true;
@@ -2374,7 +2416,7 @@
 							function(jsonData){
 								dojo.hitch(explorer, explorer.changedItem)(item);
 							}, displayErrorOnStatus
-						)						
+						);		
 					}
 				);
 			},
@@ -2394,7 +2436,7 @@
 			spriteClass: "gitCommandSprite",
 			id: "eclipse.orion.git.checkoutCommand",
 			callback: function(data) {				
-				var item = data.items;
+				var items = forceArray(data.items);
 				
 				var dialog = serviceRegistry.getService("orion.page.dialog");
 				dialog.confirm("Your changes to the selected files will be discarded and cannot be recovered.\n" +
@@ -2403,24 +2445,34 @@
 						if (!doit) {
 							return;
 						}
+						
 						var progressService = serviceRegistry.getService("orion.page.message");
+						
+						var paths = [];
+						for ( var i = 0; i < items.length; i++) {
+							paths[i] = items[i].path;
+						}
+						
 						progressService.createProgressMonitor(
-							serviceRegistry.getService("orion.git.provider").checkoutPath(item.CloneLocation, [item.path]),
+							serviceRegistry.getService("orion.git.provider").checkoutPath(data.userData.Clone.Location, paths),
 							"Resetting local changes").deferred.then(
 							function(jsonData){
-								dojo.hitch(explorer, explorer.changedItem)(item);
+								dojo.hitch(explorer, explorer.changedItem)(items);
 							}, displayErrorOnStatus
 						);				
 					}
 				);
-				
-				
 			},
 			visibleWhen: function(item) {
-//				var return_value = (item.type === "unstagedItems" && self.hasUnstaged && self._unstagedContentRenderer.getSelected().length > 0);
-//				return return_value;
-				
-				return !mGitUtil.isStaged(item);
+				var items = forceArray(item);
+				if (items.length === 0)
+					return false;
+
+				for (var i = 0; i < items.length; i++) {
+					if (mGitUtil.isStaged(items[i]))
+						return false; 
+				}
+				return true;
 			}
 		});
 
@@ -2432,21 +2484,26 @@
 			imageClass: "git-sprite-diff",
 			spriteClass: "gitCommandSprite",
 			id: "eclipse.orion.git.showPatchCommand",
-			hrefCallback : function() {
-//				var url = self._curClone.DiffLocation + "?parts=diff";
-//				var selectedItems = self._unstagedContentRenderer.getSelected();
-//				for (var i = 0; i < selectedItems.length; i++) {
-//					url += "&Path=";
-//					url += selectedItems[i].modelItem.path;
-//				}
-//				return url;
-				return "http://elo";
+			hrefCallback : function(data) {
+				var items = forceArray(data.items);
+				
+				var url = data.userData.Clone.DiffLocation + "?parts=diff";
+				for (var i = 0; i < items.length; i++) {
+					url += "&Path=";
+					url += items[i].path;
+				}
+				return url;
 			},
 			visibleWhen: function(item) {
-//				var return_value = (item.type === "unstagedItems" && self.hasUnstaged && self._unstagedContentRenderer.getSelected().length > 0);
-//				return return_value;
-				
-				return false;
+				var items = forceArray(item);
+				if (items.length === 0)
+					return false;
+
+				for (var i = 0; i < items.length; i++) {
+					if (mGitUtil.isStaged(items[i]))
+						return false; 
+				}
+				return true;
 			}
 		});
 		
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 41c3a56..38c04d5 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
@@ -191,8 +191,8 @@
 								

 								that.initTitleBar(status, repositories[0]);

 				

-								that.displayUnstaged(that._model);

-								that.displayStaged(status);

+								that.displayUnstaged(status, repositories[0]);

+								that.displayStaged(status, repositories[0]);

 								that.displayDiffs(status);

 								that.displayCommits(repositories[0]);

 								

@@ -284,7 +284,9 @@
 

 		// Git unstaged changes

 		

-		GitStatusExplorer.prototype.displayUnstaged = function(status){

+		

+		

+		GitStatusExplorer.prototype.displayUnstaged = function(status, repository){

 			

 			var that = this;

 

@@ -292,7 +294,7 @@
 			

 			var tableNode = dojo.byId( 'table' );

 			

-			var unstageSection = new mSection.Section(tableNode, {

+			var unstagedSection = new mSection.Section(tableNode, {

 				explorer: this,

 				id: "unstagedSection",

 				title: unstagedSortedChanges.length > 0 ? "Unstaged" : "No Unstaged Changes",

@@ -301,19 +303,29 @@
 				serviceRegistry: this.registry

 			});

 			

-//			unstageSection.registerCommandContribution("eclipse.orion.git.stageCommand", 100);

-//			unstageSection.registerCommandContribution("eclipse.orion.git.checkoutCommand", 200);

-//			unstageSection.registerCommandContribution("eclipse.orion.git.showPatchCommand", 300);

-//			unstageSection.renderCommands(status, "button");

+			this.commandService.registerCommandContribution(unstagedSection.selectionNode.id, "eclipse.orion.git.stageCommand", 100);

+			this.commandService.registerCommandContribution(unstagedSection.selectionNode.id, "eclipse.orion.git.checkoutCommand", 200);

+			this.commandService.registerCommandContribution(unstagedSection.selectionNode.id, "eclipse.orion.git.showPatchCommand", 300);

 			

-			var sectionItemActionScopeId = "unstagedSectionItem";

+			if (!this.unstagedOnce){

+				if (!this.unstagedSelection)

+					this.unstagedSelection = new mSelection.Selection(this.registry, "orion.unstagedSection.selection");

+				

+				this.registry.getService("orion.unstagedSection.selection").addEventListener("selectionChanged", function(singleSelection, selections) {

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

+					if (selectionTools) {

+						dojo.empty(selectionTools);

+						that.commandService.renderCommands(unstagedSection.selectionNode.id, selectionTools, selections, that, "button", {"Clone": repository});

+					}

+				});

+				this.unstagedOnce = true;

+			}

 			

-			var mySelectionService = new mSelection.Selection(this.registry);

+			var sectionItemActionScopeId = "unstagedSectionItemActionArea";

 			

-			var commandService = this.registry.getService("orion.page.command");

-			commandService.registerCommandContribution(sectionItemActionScopeId, "eclipse.orion.git.stageCommand", 100);

-			commandService.registerCommandContribution(sectionItemActionScopeId, "eclipse.orion.git.checkoutCommand", 200);		

-			commandService.registerSelectionService(sectionItemActionScopeId, mySelectionService);

+			this.commandService.registerCommandContribution(sectionItemActionScopeId, "eclipse.orion.git.stageCommand", 100);

+			this.commandService.registerCommandContribution(sectionItemActionScopeId, "eclipse.orion.git.checkoutCommand", 200);		

+			this.commandService.registerSelectionService(sectionItemActionScopeId, this.unstagedSelection);

 			

 			UnstagedModel = (function() {

 				function UnstagedModel() {

@@ -388,12 +400,12 @@
 				return UnstagedNavigator;

 			}());

 			

-			new UnstagedNavigator(this.registry, mySelectionService, "unstagedNode"/*tableNode.id*/, sectionItemActionScopeId);

+			new UnstagedNavigator(this.registry, this.unstagedSelection, "unstagedNode"/*tableNode.id*/, sectionItemActionScopeId);

 		};

 		

 		// Git staged changes

 		

-		GitStatusExplorer.prototype.displayStaged = function(status){

+		GitStatusExplorer.prototype.displayStaged = function(status, repository){

 			

 			var that = this;

 			

@@ -401,7 +413,7 @@
 			

 			var tableNode = dojo.byId( 'table' );

 			

-			var stageSection = new mSection.Section(tableNode, {

+			var stagedSection = new mSection.Section(tableNode, {

 				explorer: this,

 				id: "stagedSection",

 				title: stagedSortedChanges.length > 0 ? "Staged" : "No Staged Changes",

@@ -411,17 +423,29 @@
 				slideout: true

 			});

 			

-//			stageSection.registerCommandContribution("eclipse.orion.git.unstageCommand", 100);

-			stageSection.registerCommandContribution("eclipse.orion.git.commitCommand", 200);

-			stageSection.renderCommands(status, "button");

+			this.commandService.registerCommandContribution(stagedSection.actionsNode.id, "eclipse.orion.git.commitCommand", 100);

+			this.commandService.renderCommands(stagedSection.actionsNode.id, stagedSection.actionsNode.id, status, that, "button");

 			

-			var sectionItemActionScopeId = "stagedSectionItem";

+			this.commandService.registerCommandContribution(stagedSection.selectionNode.id, "eclipse.orion.git.unstageCommand", 100);

 			

-			var mySelectionService = new mSelection.Selection(this.registry);

+			if (!this.stagedOnce){

+				if (!this.stagedSelection)

+					this.stagedSelection = new mSelection.Selection(this.registry, "orion.stagedSection.selection");

+				

+				this.registry.getService("orion.stagedSection.selection").addEventListener("selectionChanged", function(singleSelection, selections) {

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

+					if (selectionTools) {

+						dojo.empty(selectionTools);

+						that.commandService.renderCommands(stagedSection.selectionNode.id, selectionTools, selections, that, "button", {"Clone": repository});

+					}

+				});

+				this.stagedOnce = true;

+			}

 			

-			var commandService = this.registry.getService("orion.page.command");

-			commandService.registerCommandContribution(sectionItemActionScopeId, "eclipse.orion.git.unstageCommand", 100);

-			commandService.registerSelectionService(sectionItemActionScopeId, mySelectionService);

+			var sectionItemActionScopeId = "stagedSectionItemActionArea";

+			

+			this.commandService.registerCommandContribution(sectionItemActionScopeId, "eclipse.orion.git.unstageCommand", 100);

+			this.commandService.registerSelectionService(sectionItemActionScopeId, this.stagedSelection);

 			

 			StagedModel = (function() {

 				function StagedModel() {

@@ -497,7 +521,7 @@
 				return StagedNavigator;

 			}());

 			

-			new StagedNavigator(this.registry, mySelectionService, "stagedNode"/*tableNode.id*/, sectionItemActionScopeId);

+			new StagedNavigator(this.registry, this.stagedSelection, "stagedNode"/*tableNode.id*/, sectionItemActionScopeId);

 		};

 				

 		// Git diffs