Fix for Bug 370509 - New E4 Application wizard does not handle spaces in
project name
diff --git a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/project/E4NewProjectWizard.java b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/project/E4NewProjectWizard.java
index ea33626..24c84c3 100644
--- a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/project/E4NewProjectWizard.java
+++ b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/project/E4NewProjectWizard.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2006, 2010 Soyatec(http://www.soyatec.com) and others.
+ * Copyright (c) 2006, 2012 Soyatec(http://www.soyatec.com) 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
@@ -7,6 +7,7 @@
  *
  * Contributors:
  *     Soyatec - initial API and implementation
+ *     IBM Corporation - ongoing enhancements
  *******************************************************************************/
 package org.eclipse.e4.internal.tools.wizards.project;
 
@@ -129,7 +130,7 @@
 		fContentPage = new PluginContentPage(
 				"page2", fProjectProvider, fMainPage, fPluginData); //$NON-NLS-1$
 
-		fApplicationPage = new NewApplicationWizardPage(fProjectProvider);
+		fApplicationPage = new NewApplicationWizardPage(fProjectProvider, fPluginData);
 
 		addPage(fContentPage);
 		addPage(fApplicationPage);
@@ -347,7 +348,8 @@
 				|| map.get(NewApplicationWizardPage.PRODUCT_NAME) == null)
 			return;
 
-		String projectName = map.get(NewApplicationWizardPage.PRODUCT_NAME);
+		// If the project has invalid characters, the plug-in name would replace them with underscores, product name does the same
+		String pluginName = map.get(NewApplicationWizardPage.PRODUCT_NAME);
 		String xmiPath = map
 				.get(NewApplicationWizardPage.APPLICATION_XMI_PROPERTY);
 
@@ -451,23 +453,23 @@
 
 			// Create Quit command
 			MCommand quitCommand = createCommand("quitCommand", "QuitHandler",
-					"Ctrl+Q", projectName, fragment, application);
+					"Ctrl+Q", pluginName, fragment, application);
 
 			MCommand openCommand = createCommand("openCommand", "OpenHandler",
-					"Ctrl+O", projectName, fragment, application);
+					"Ctrl+O", pluginName, fragment, application);
 
 			MCommand saveCommand = createCommand("saveCommand", "SaveHandler",
-					"Ctrl+S", projectName, fragment, application);
+					"Ctrl+S", pluginName, fragment, application);
 
 			MCommand aboutCommand = createCommand("aboutCommand",
-					"AboutHandler", "Ctrl+A", projectName, fragment,
+					"AboutHandler", "Ctrl+A", pluginName, fragment,
 					application);
 
 			MTrimmedWindow mainWindow = MBasicFactory.INSTANCE
 					.createTrimmedWindow();
 			application.getChildren().add(mainWindow);
 			{
-				mainWindow.setLabel(projectName);
+				mainWindow.setLabel(pluginName);
 				mainWindow.setWidth(500);
 				mainWindow.setHeight(400);
 
@@ -486,7 +488,7 @@
 						fileMenuItem.getChildren().add(menuItemOpen);
 						menuItemOpen.setLabel("Open");
 						menuItemOpen.setIconURI("platform:/plugin/"
-								+ project.getName() + "/icons/sample.gif");
+								+ pluginName + "/icons/sample.gif");
 						menuItemOpen.setCommand(openCommand);
 
 						MHandledMenuItem menuItemSave = MMenuFactory.INSTANCE
@@ -494,7 +496,7 @@
 						fileMenuItem.getChildren().add(menuItemSave);
 						menuItemSave.setLabel("Save");
 						menuItemSave.setIconURI("platform:/plugin/"
-								+ project.getName() + "/icons/save_edit.gif");
+								+ pluginName + "/icons/save_edit.gif");
 						menuItemSave.setCommand(saveCommand);
 
 						MHandledMenuItem menuItemQuit = MMenuFactory.INSTANCE
@@ -555,14 +557,14 @@
 								.createHandledToolItem();
 						toolBar.getChildren().add(toolItemOpen);
 						toolItemOpen.setIconURI("platform:/plugin/"
-								+ project.getName() + "/icons/sample.gif");
+								+ pluginName + "/icons/sample.gif");
 						toolItemOpen.setCommand(openCommand);
 
 						MHandledToolItem toolItemSave = MMenuFactory.INSTANCE
 								.createHandledToolItem();
 						toolBar.getChildren().add(toolItemSave);
 						toolItemSave.setIconURI("platform:/plugin/"
-								+ project.getName() + "/icons/save_edit.gif");
+								+ pluginName + "/icons/save_edit.gif");
 						toolItemSave.setCommand(saveCommand);
 					}
 				}
@@ -616,7 +618,7 @@
 		binaryExtentions.add(".png");
 
 		Map<String, String> keys = new HashMap<String, String>();
-		keys.put("projectName", projectName);
+		keys.put("projectName", pluginName);
 		keys.put("packageName", fragment.getElementName() + ".handlers");
 
 		try {
diff --git a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/project/NewApplicationWizardPage.java b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/project/NewApplicationWizardPage.java
index 14bc00b..8573dac 100644
--- a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/project/NewApplicationWizardPage.java
+++ b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/project/NewApplicationWizardPage.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2006, 2010 Soyatec (http://www.soyatec.com) and others.
+ * Copyright (c) 2006, 2012 Soyatec (http://www.soyatec.com) 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
@@ -7,6 +7,7 @@
  *
  * Contributors:
  *     Soyatec - initial API and implementation
+ *     IBM Corporation - ongoing enhancements
  *******************************************************************************/
 package org.eclipse.e4.internal.tools.wizards.project;
 
@@ -19,6 +20,7 @@
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.wizard.WizardPage;
 import org.eclipse.pde.internal.ui.wizards.IProjectProvider;
+import org.eclipse.pde.internal.ui.wizards.plugin.AbstractFieldData;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.events.SelectionListener;
@@ -58,12 +60,14 @@
 	private Text proNameText;
 	private Text proApplicationText;
 	private Group propertyGroup;
+	private AbstractFieldData pluginData;
 
 	private PropertyData[] PROPERTIES;
 
-	protected NewApplicationWizardPage(IProjectProvider projectProvider) {
+	protected NewApplicationWizardPage(IProjectProvider projectProvider, AbstractFieldData pluginData) {
 		super("New e4 Application Wizard Page");
 		this.projectProvider = projectProvider;
+		this.pluginData = pluginData;
 		data = new HashMap<String, String>();
 		setTitle("e4 Application");
 		setMessage("Configure application with special values.");
@@ -386,7 +390,9 @@
 	@Override
 	public void setVisible(boolean visible) {
 		if (visible && PROPERTIES == null) {
-			proNameText.setText(projectProvider.getProjectName());
+			
+			// Use the plug-in name for the product name (not project name which can contain illegal characters)
+			proNameText.setText(pluginData.getId());
 
 			proApplicationText.setText(E4_APPLICATION);
 
@@ -406,8 +412,11 @@
 			for (PropertyData property : getPropertyData()) {
 				data.put(property.getName(), property.getValue());
 			}
-
-			data.put(PRODUCT_NAME, projectProvider.getProjectName());
+			
+			// Use the plug-in name for the product name (not project name which can contain illegal characters)
+			String productName = pluginData.getId();
+			
+			data.put(PRODUCT_NAME, productName);
 			data.put(APPLICATION, E4_APPLICATION);
 		}
 		Map<String, String> map = new HashMap<String, String>();
diff --git a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/project/PluginContentPage.java b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/project/PluginContentPage.java
index 8f0c5ae..74d65d9 100644
--- a/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/project/PluginContentPage.java
+++ b/bundles/org.eclipse.e4.tools/src/org/eclipse/e4/internal/tools/wizards/project/PluginContentPage.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2010 IBM Corporation and others.
+ * Copyright (c) 2000, 2012 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
@@ -50,6 +50,7 @@
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.dialogs.PreferencesUtil;
+import org.osgi.framework.Version;
 
 /**
  * Content wizard page for the New Plugin Project wizard (page 2)
@@ -360,4 +361,27 @@
 		}
 		return super.canFlipToNextPage();
 	}
+	
+	/* (non-Javadoc)
+	 * @see org.eclipse.pde.internal.ui.wizards.plugin.ContentPage#computeId()
+	 */
+	protected String computeId() {
+		String id = super.computeId();
+		// In addition to removed illegal characters, the xmi model does not recognize plug-in uris if they end in a version number
+		// See PlatformURLPluginConnection.parse()
+		int underScore = id.lastIndexOf('_');
+		Version version;
+		while (underScore >= 0) {
+			try {
+				version = Version.parseVersion(id.substring(underScore + 1));
+				// name cannot end with a valid version, remove it
+				id = id.substring(0, underScore);
+			} catch (IllegalArgumentException iae) {
+				// valid name so far, continue to next underscore
+			}
+			underScore = id.lastIndexOf('_', underScore - 1);
+			
+		}
+		return id;
+	}
 }