Bug 526490 - Silent failure when trying to import a project of same name

Change-Id: Ic0f6bcae77d3a122783f6e28642fa79b29286a42
Signed-off-by: Lucas Bullen <lbullen@redhat.com>
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/wizards/datatransfer/DataTransferMessages.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/wizards/datatransfer/DataTransferMessages.java
index 674fed4..a3ca2f2 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/wizards/datatransfer/DataTransferMessages.java
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/wizards/datatransfer/DataTransferMessages.java
@@ -156,6 +156,7 @@
 	public static String SmartImportWizardPage_selectFolderOrArchiveToImport;
 	public static String SmartImportWizardPage_browseForFolder;
 	public static String SmartImportProposals_alreadyImportedAsProject_title;
+	public static String SmartImportProposals_anotherProjectWithSameNameExists_title;
 	public static String SmartImportProposals_anotherProjectWithSameNameExists_description;
 
 	public static String SmartImportWizardPage_importProjectsInFolderTitle;
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/wizards/datatransfer/SmartImportRootWizardPage.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/wizards/datatransfer/SmartImportRootWizardPage.java
index 4a8ffe9..c47c70e 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/wizards/datatransfer/SmartImportRootWizardPage.java
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/wizards/datatransfer/SmartImportRootWizardPage.java
@@ -11,6 +11,7 @@
  *     Lars Vogel <Lars.Vogel@vogella.com>
  *     Rüdiger Herrmann <ruediger.herrmann@gmx.de>
  *     Patrik Suzzi <psuzzi@gmail.com> - Bug 500836
+ *     Lucas Bullen (Red Hat Inc.) - Bug 526490
  ******************************************************************************/
 package org.eclipse.ui.internal.wizards.datatransfer;
 
@@ -188,7 +189,7 @@
 
 		@Override
 		public Color getForeground(Object o) {
-			if (isExistingProject((File) o)) {
+			if (isExistingProject((File) o) || isExistingProjectName((File) o)) {
 				return Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
 			}
 			return null;
@@ -209,6 +210,8 @@
 			File file = (File) o;
 			if (isExistingProject(file)) {
 				return DataTransferMessages.SmartImportProposals_alreadyImportedAsProject_title;
+			} else if (isExistingProjectName(file)) {
+				return DataTransferMessages.SmartImportProposals_anotherProjectWithSameNameExists_title;
 			}
 			List<ProjectConfigurator> configurators = SmartImportRootWizardPage.this.potentialProjects.get(file);
 			if (configurators.isEmpty()) {
@@ -224,7 +227,7 @@
 
 		@Override
 		public Color getForeground(Object o) {
-			if (isExistingProject((File) o)) {
+			if (isExistingProject((File) o) || isExistingProjectName((File) o)) {
 				return Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
 			}
 			return null;
@@ -580,7 +583,8 @@
 		tree.addCheckStateListener(new ICheckStateListener() {
 			@Override
 			public void checkStateChanged(CheckStateChangedEvent event) {
-				if (isExistingProject((File) event.getElement())) {
+				if (isExistingProject((File) event.getElement())
+						|| isExistingProjectName((File) event.getElement())) {
 					tree.setChecked(event.getElement(), false);
 					return;
 				}
@@ -622,7 +626,7 @@
 			public void widgetSelected(SelectionEvent e) {
 				for (TreeItem item : tree.getTree().getItems()) {
 					File dir = (File) item.getData();
-					if (isExistingProject(dir)) {
+					if (isExistingProject(dir) || isExistingProjectName(dir)) {
 						tree.setChecked(dir, false);
 					} else {
 						tree.setChecked(dir, true);
@@ -699,6 +703,10 @@
 		return false;
 	}
 
+	protected boolean isExistingProjectName(File element) {
+		return ResourcesPlugin.getWorkspace().getRoot().getProject(element.getName()).exists();
+	}
+
 	protected void validatePage() {
 		// reset error message
 		setErrorMessage(null);
@@ -722,7 +730,7 @@
 
 	@Override
 	public boolean isPageComplete() {
-		return sourceIsValid() && getWizard().getImportJob() != null && getWizard().getImportJob() != null
+		return sourceIsValid() && getWizard().getImportJob() != null
 				&& (getWizard().getImportJob().getDirectoriesToImport() == null
 						|| !getWizard().getImportJob().getDirectoriesToImport().isEmpty());
 	}
@@ -780,7 +788,9 @@
 			if (potentialProjects.size() == 1 && potentialProjects.values().iterator().next().isEmpty()) {
 				getWizard().getImportJob().setDirectoriesToImport(null);
 				getWizard().getImportJob().setExcludedDirectories(null);
-				selectionSummary.setText(NLS.bind(DataTransferMessages.SmartImportProposals_selectionSummary, 1, 1));
+
+				selectionSummary.setText(NLS.bind(DataTransferMessages.SmartImportProposals_selectionSummary,
+						directoriesToImport.size(), 1));
 			} else {
 				Set<File> excludedDirectories = new HashSet(((Map<File, ?>) this.tree.getInput()).keySet());
 				for (Object item : this.directoriesToImport) {
@@ -917,7 +927,7 @@
 		tree.setInput(potentialProjects);
 		this.directoriesToImport = new HashSet<>();
 		for (File dir : potentialProjects.keySet()) {
-			if (!isExistingProject(dir)) {
+			if (!(isExistingProject(dir) || isExistingProjectName(dir))) {
 				directoriesToImport.add(dir);
 			}
 		}
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/wizards/datatransfer/messages.properties b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/wizards/datatransfer/messages.properties
index ac4e1e7..88f9b51 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/wizards/datatransfer/messages.properties
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/wizards/datatransfer/messages.properties
@@ -157,6 +157,7 @@
 SmartImportWizardPage_selectFolderOrArchiveToImport=Select the folder to find projects to import
 SmartImportWizardPage_browseForFolder=Browse for Folder
 SmartImportProposals_alreadyImportedAsProject_title=Folder already imported as project
+SmartImportProposals_anotherProjectWithSameNameExists_title=Project with same name already imported
 SmartImportProposals_anotherProjectWithSameNameExists_description=Could not perform operation:\n\
 A project with name ''{0}'' already exists in workspace, and the folder you''re trying to import \
 contains a project description file (.project) using this same name.\n\
diff --git a/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/datatransfer/SmartImportTests.java b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/datatransfer/SmartImportTests.java
index 11225b7..a77b6d2 100644
--- a/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/datatransfer/SmartImportTests.java
+++ b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/datatransfer/SmartImportTests.java
@@ -7,6 +7,7 @@
  *
  * Contributors:
  * - Mickael Istria (Red Hat Inc.)
+ * - Lucas Bullen (Red Hat Inc.)
  *******************************************************************************/
 package org.eclipse.ui.tests.datatransfer;
 
@@ -28,6 +29,7 @@
 import org.eclipse.core.runtime.OperationCanceledException;
 import org.eclipse.jface.dialogs.IDialogConstants;
 import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.viewers.CheckboxTreeViewer;
 import org.eclipse.jface.wizard.ProgressMonitorPart;
 import org.eclipse.jface.wizard.WizardDialog;
 import org.eclipse.swt.SWT;
@@ -41,6 +43,7 @@
 import org.eclipse.ui.IWorkingSet;
 import org.eclipse.ui.IWorkingSetManager;
 import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.dialogs.FilteredTree;
 import org.eclipse.ui.internal.WorkbenchPlugin;
 import org.eclipse.ui.internal.wizards.datatransfer.SmartImportRootWizardPage;
 import org.eclipse.ui.internal.wizards.datatransfer.SmartImportWizard;
@@ -174,6 +177,58 @@
 	}
 
 	@Test
+	public void testImportProjectWithExistingName()
+			throws IOException, OperationCanceledException, InterruptedException {
+		URL url = FileLocator
+				.toFileURL(getClass()
+						.getResource("/data/org.eclipse.datatransferArchives/sameNameProject1/sameNameProject"));
+		File file = new File(url.getFile());
+		runSmartImport(file);
+
+		// Check expected project is there
+		IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
+		assertEquals(1, projects.length);
+
+		url = FileLocator
+				.toFileURL(getClass()
+						.getResource("/data/org.eclipse.datatransferArchives/sameNameProject2/sameNameProject"));
+		file = new File(url.getFile());
+
+		SmartImportWizard wizard = new SmartImportWizard();
+		wizard.setInitialImportSource(file);
+		this.dialog = new WizardDialog(getWorkbench().getActiveWorkbenchWindow().getShell(), wizard);
+		dialog.setBlockOnOpen(false);
+		dialog.open();
+		processEvents();
+		processEventsUntil(new Condition() {
+			@Override
+			public boolean compute() {
+				return !dialog.getErrorMessage().isEmpty();
+			}
+		}, -1);
+		SmartImportRootWizardPage page = (SmartImportRootWizardPage) dialog.getCurrentPage();
+		CheckboxTreeViewer treeViewer = getTreeViewer((Composite) page.getControl());
+		assertNotNull(treeViewer);
+		assertEquals(1, treeViewer.getTree().getItemCount());
+		assertEquals(0, treeViewer.getCheckedElements().length);
+		assertEquals("Project with same name already imported", treeViewer.getTree().getItems()[0].getText(1));
+	}
+
+	private CheckboxTreeViewer getTreeViewer(Composite parent) {
+		for (Control control : parent.getChildren()) {
+			if (control instanceof FilteredTree) {
+				return (CheckboxTreeViewer) ((FilteredTree) control).getViewer();
+			} else if (control instanceof Composite) {
+				CheckboxTreeViewer res = getTreeViewer((Composite) control);
+				if (res != null) {
+					return res;
+				}
+			}
+		}
+		return null;
+	}
+
+	@Test
 	public void testConfigurationIgnoreNestedProjects()
 			throws IOException, OperationCanceledException, InterruptedException {
 		URL url = FileLocator
diff --git a/tests/org.eclipse.ui.tests/data/org.eclipse.datatransferArchives/sameNameProject1/sameNameProject/importme b/tests/org.eclipse.ui.tests/data/org.eclipse.datatransferArchives/sameNameProject1/sameNameProject/importme
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/org.eclipse.ui.tests/data/org.eclipse.datatransferArchives/sameNameProject1/sameNameProject/importme
diff --git a/tests/org.eclipse.ui.tests/data/org.eclipse.datatransferArchives/sameNameProject2/sameNameProject/importme b/tests/org.eclipse.ui.tests/data/org.eclipse.datatransferArchives/sameNameProject2/sameNameProject/importme
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/org.eclipse.ui.tests/data/org.eclipse.datatransferArchives/sameNameProject2/sameNameProject/importme