Bug 432369 - Get rid of Orion ImportDialog
- Initial implementation which removes the use of ImportDialog from the "File->Import->File System" and "File->New->Project->File System" commands.
- Fixed error reporting in projectClient.js->createProject
diff --git a/bundles/org.eclipse.orion.client.core/web/orion/projectClient.js b/bundles/org.eclipse.orion.client.core/web/orion/projectClient.js
index f89253f..bda7ae6 100644
--- a/bundles/org.eclipse.orion.client.core/web/orion/projectClient.js
+++ b/bundles/org.eclipse.orion.client.core/web/orion/projectClient.js
@@ -167,13 +167,22 @@
 		},
 		
 		createProject: function(workspaceLocation, projectMetadata){
-				return this.fileClient.createProject(workspaceLocation, projectMetadata.Name, projectMetadata.ContentLocation, true).then(function(fileMetadata){
+			var deferred = new Deferred();
+			
+			this.fileClient.createProject(workspaceLocation, projectMetadata.Name, projectMetadata.ContentLocation, true).then(
+				function(fileMetadata){
 					delete projectMetadata.Name;
-					return this.initProject(fileMetadata.ContentLocation, projectMetadata);
+					deferred.resolve(this.initProject(fileMetadata.ContentLocation, projectMetadata));
 				}.bind(this), 
-				function(error){return error;},
-				function(progress){return progress;}
+				function(error){
+					deferred.reject(error);
+				},
+				function(progress){
+					deferred.progress(progress);
+				}
 			);
+			
+			return deferred;
 		},
 		
 		getDependencyFileMetadata : function(dependency, workspaceLocation){
diff --git a/bundles/org.eclipse.orion.client.ui/web/edit/edit.html b/bundles/org.eclipse.orion.client.ui/web/edit/edit.html
index 719910e..14d1462 100644
--- a/bundles/org.eclipse.orion.client.ui/web/edit/edit.html
+++ b/bundles/org.eclipse.orion.client.ui/web/edit/edit.html
@@ -23,6 +23,8 @@
     </head>
 
 	<body id="orion-editor" spellcheck="false" class="orionPage">
+		<!-- file input doesn't function properly on all web browsers if it is dynamically generated, we don't want the input to be visible -->
+		<input id="fileSelectorInput" type="file" multiple style="position:fixed; top: -100px;"/>
 		<div id="sideMenu" class="sideMenu"></div>
 		<div id="pageContent" class="content-fixedHeight">
 			<div id="sideMenuToggle" class="sideMenuToggle"></div>
diff --git a/bundles/org.eclipse.orion.client.ui/web/orion/fileCommands.js b/bundles/org.eclipse.orion.client.ui/web/orion/fileCommands.js
index 6043784..dc1d63c 100644
--- a/bundles/org.eclipse.orion.client.ui/web/orion/fileCommands.js
+++ b/bundles/org.eclipse.orion.client.ui/web/orion/fileCommands.js
@@ -14,9 +14,9 @@
 
 define(['i18n!orion/navigate/nls/messages', 'require', 'orion/webui/littlelib', 'orion/i18nUtil', 'orion/uiUtils', 'orion/fileUtils', 'orion/commands', 
 	'orion/commandRegistry', 'orion/extensionCommands', 'orion/contentTypes', 'orion/compare/compareUtils', 
-	'orion/Deferred', 'orion/webui/dialogs/DirectoryPrompterDialog', 'orion/webui/dialogs/SFTPConnectionDialog', 'orion/webui/dialogs/ImportDialog',
+	'orion/Deferred', 'orion/webui/dialogs/DirectoryPrompterDialog', 'orion/webui/dialogs/SFTPConnectionDialog',
 	'orion/EventTarget'],
-	function(messages, require, lib, i18nUtil, mUIUtils, mFileUtils, mCommands, mCommandRegistry, mExtensionCommands, mContentTypes, mCompareUtils, Deferred, DirPrompter, SFTPDialog, ImportDialog, EventTarget){
+	function(messages, require, lib, i18nUtil, mUIUtils, mFileUtils, mCommands, mCommandRegistry, mExtensionCommands, mContentTypes, mCompareUtils, Deferred, DirPrompter, SFTPDialog, EventTarget){
 
 	/**
 	 * Utility methods
@@ -1067,13 +1067,25 @@
 			id: "orion.import", //$NON-NLS-0$
 			callback : function(data) {
 				var item = getTargetFolder(data.items);
-				var dialog = new ImportDialog.ImportDialog({
-					importLocation: item.ImportLocation,
-					func: function() {
-						dispatchModelEvent({ type: "import", target: item }); //$NON-NLS-0$
+				var fileInput = lib.node("fileSelectorInput");
+				var cloneInput = fileInput.cloneNode(); // clone file input before its value is changed
+
+				var changeListener = function(){ //$NON-NLS-0$
+					if (fileInput.files && fileInput.files.length > 0) {
+						for (var i = 0; i < fileInput.files.length; i++) {
+							explorer._uploadFile(item, fileInput.files.item(i), true);
+						}
 					}
-				});
-				dialog.show();
+					
+					fileInput.removeEventListener("change", changeListener);
+				};
+				
+				fileInput.addEventListener("change", changeListener);
+				fileInput.click();
+				
+				//replace original fileInput so that "change" event always fires
+				fileInput.parentNode.replaceChild(cloneInput, fileInput);
+				
 			},
 			visibleWhen: checkFolderSelection
 		});
diff --git a/bundles/org.eclipse.orion.client.ui/web/orion/navigate/nls/root/messages.js b/bundles/org.eclipse.orion.client.ui/web/orion/navigate/nls/root/messages.js
index afcea52..2788ea6 100644
--- a/bundles/org.eclipse.orion.client.ui/web/orion/navigate/nls/root/messages.js
+++ b/bundles/org.eclipse.orion.client.ui/web/orion/navigate/nls/root/messages.js
@@ -111,5 +111,7 @@
 	"Uploading ": "Uploading ",
 	"Cancel upload": "Cancel upload",
 	"Uploading the following file failed: ": "Uploading the following file failed: ",
+	"Enter project name:": "Enter project name:",
+	"Creating project ": "Creating project ",
 	"NoFile": "Use the ${0} menu to create new files and folders. Click a file to start coding." //$NON-NLS-1$ //$NON-NLS-0$
 });
diff --git a/bundles/org.eclipse.orion.client.ui/web/orion/projectCommands.js b/bundles/org.eclipse.orion.client.ui/web/orion/projectCommands.js
index 3e31d15..5c7f732 100644
--- a/bundles/org.eclipse.orion.client.ui/web/orion/projectCommands.js
+++ b/bundles/org.eclipse.orion.client.ui/web/orion/projectCommands.js
@@ -9,13 +9,13 @@
  * Contributors:
  *     IBM Corporation - initial API and implementation
  *******************************************************************************/
-/*global window define orion XMLHttpRequest confirm*/
+/*global window define orion XMLHttpRequest confirm document*/
 /*jslint sub:true*/
 define(['require', 'i18n!orion/navigate/nls/messages', 'orion/webui/littlelib', 'orion/commands', 'orion/Deferred', 'orion/webui/dialogs/DirectoryPrompterDialog',
- 'orion/commandRegistry', 'orion/i18nUtil', 'orion/webui/dialogs/ImportDialog', 'orion/widgets/projects/ProjectOptionalParametersDialog', 
+ 'orion/commandRegistry', 'orion/i18nUtil', 'orion/webui/dialogs/PromptDialog', 'orion/widgets/projects/ProjectOptionalParametersDialog', 
  'orion/fileCommands', 'orion/editorCommands', 'orion/EventTarget',
  'orion/URITemplate', 'orion/PageLinks', 'orion/objects', 'orion/preferences'],
-	function(require, messages, lib, mCommands, Deferred, DirectoryPrompterDialog, mCommandRegistry, i18nUtil, ImportDialog, ProjectOptionalParametersDialog, FileCommands, mEditorCommands, EventTarget,
+	function(require, messages, lib, mCommands, Deferred, DirectoryPrompterDialog, mCommandRegistry, i18nUtil, PromptDialog, ProjectOptionalParametersDialog, FileCommands, mEditorCommands, EventTarget,
 		URITemplate, PageLinks, objects, mPreferences){
 		var projectCommandUtils = {};
 		
@@ -641,6 +641,7 @@
 	};
 	
 	var explorer;
+	
 	projectCommandUtils.setExplorer = function(theExplorer) {
 		explorer = theExplorer;
 	};
@@ -1018,7 +1019,7 @@
 					}
 					var item = forceSingleItem(data.items);
 					fileClient.loadWorkspace(fileClient.fileServiceRootURL(item.Location)).then(function(workspace) {
-						progress.progress(projectClient.createProject(workspace.ChildrenLocation, {Name: name}), "Creating project " + name).then(function(project){
+						progress.progress(projectClient.createProject(workspace.ChildrenLocation, {Name: name}), messages["Creating project "] + name).then(function(project){
 							dispatchNewProject(workspace, project);
 						});
 					});
@@ -1036,30 +1037,68 @@
 			commandService.addCommand(createBasicProjectCommand);
 				
 			var createZipProjectCommand = new mCommands.Command({
-			name: "File System",
-			tooltip: "Create project from a local zipped file.",
-			id: "orion.project.create.fromfile",
-			parameters : new mCommandRegistry.ParametersDescription([new mCommandRegistry.CommandParameter("name", "text", "Name: ")]),
+			name: "File System", //$NON-NLS-0$
+			tooltip: "Create project from a local zipped file.", //$NON-NLS-0$
+			id: "orion.project.create.fromfile", //$NON-NLS-0$
 			callback: function(data){
-					var name = data.parameters.valueFor("name");
-					if(!name){
-						return;
-					}
 					var item = forceSingleItem(data.items);
+
+					var fileInput = lib.node("fileSelectorInput"); //$NON-NLS-0$
+					var cloneInput = fileInput.cloneNode(); // clone file input before its value is changed
 					
-					fileClient.loadWorkspace(fileClient.fileServiceRootURL(item.Location)).then(function(workspace) {
-						progress.progress(projectClient.createProject(workspace.ChildrenLocation, {Name: name}), "Creating project " + name).then(function(projectInfo){
-							progress.progress(fileClient.read(projectInfo.ContentLocation, true)).then(function(projectMetadata){
-								var dialog = new ImportDialog.ImportDialog({
-									importLocation: projectMetadata.ImportLocation,
-									func: function() {
-										dispatchNewProject(workspace, projectInfo);
-									}
-								});
-								dialog.show();
-							});
-						});
+					var projectNameDialog = new PromptDialog.PromptDialog({
+						title: messages["Enter project name:"] //$NON-NLS-0$
 					});
+					
+					// add listener which uses project name entered by user to create a new project
+					projectNameDialog.addEventListener("ok", function(event) { //$NON-NLS-0$
+						var projectName = event.value;
+						fileClient.loadWorkspace(fileClient.fileServiceRootURL(item.Location)).then(function(workspace) {
+							progress.progress(projectClient.createProject(workspace.ChildrenLocation, {Name: projectName}), messages["Creating project "] + projectName).then(function(projectInfo){ //$NON-NLS-0$
+								progress.progress(fileClient.read(projectInfo.ContentLocation, true)).then(function(projectMetadata){
+									
+									explorer.changedItem(workspace, true).then(function(newWorkspace){ //force workspace reload
+										var children = newWorkspace.Children;
+										// find the item that represents the newly created project
+										var projectItem = null;
+										children.some(function(child){
+											if (child.Location === projectMetadata.Location) {
+												projectItem = child;
+												return true;
+											}
+											return false;
+										});
+										// upload files selected by user into newly created project
+										if (projectItem) {
+											if (fileInput.files && fileInput.files.length > 0) {
+												for (var i = 0; i < fileInput.files.length; i++) {
+													explorer._uploadFile(projectItem, fileInput.files.item(i), true);
+												}
+											}
+										}
+									});
+									
+								}, errorHandler);
+							}, errorHandler);
+						}, errorHandler);
+					});
+					
+					var changeListener = function(){
+						if (fileInput.files && fileInput.files.length > 0) {
+							projectNameDialog.show();	// ask user for project name
+						}
+
+						fileInput.removeEventListener("change", changeListener);	
+					};
+					fileInput.addEventListener("change", changeListener);
+					
+					// Launch file picker. Note that at the time when this code was written, web browser 
+					// restrictions made it so that fileInput.click() cannot be called asynchronously.
+					// e.g. cannot be called from event handler after the user enters the project name
+					fileInput.click();
+					
+					//replace original fileInput so that change event always fires
+					fileInput.parentNode.replaceChild(cloneInput, fileInput);
 				},
 			visibleWhen: function(item) {
 					if (!explorer || !explorer.isCommandsVisible()) {
@@ -1090,7 +1129,7 @@
 						}
 						var item = forceSingleItem(data.items);
 						fileClient.loadWorkspace(fileClient.fileServiceRootURL(item.Location)).then(function(workspace) {
-							progress.progress(projectClient.createProject(workspace.ChildrenLocation, {Name: name, ContentLocation: url}), "Creating project " + name).then(function(project){
+							progress.progress(projectClient.createProject(workspace.ChildrenLocation, {Name: name, ContentLocation: url}), messages["Creating project "] + name).then(function(project){
 								dispatchNewProject(workspace, project);
 							});
 						});