[Importer] Suggest to discard new root project when it's not configured

Signed-off-by: Mickael Istria <mistria@redhat.com>
Change-Id: Idd6c3ac62f5ef307f613aeb5f7ac0ce39b55c099
diff --git a/bundles/org.eclipse.e4.ui.importer/src/org/eclipse/ui/internal/wizards/datatransfer/EasymportJob.java b/bundles/org.eclipse.e4.ui.importer/src/org/eclipse/ui/internal/wizards/datatransfer/EasymportJob.java
index 3b3ae44..2269f83 100644
--- a/bundles/org.eclipse.e4.ui.importer/src/org/eclipse/ui/internal/wizards/datatransfer/EasymportJob.java
+++ b/bundles/org.eclipse.e4.ui.importer/src/org/eclipse/ui/internal/wizards/datatransfer/EasymportJob.java
@@ -35,7 +35,9 @@
 import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.osgi.util.NLS;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.IWorkingSet;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.wizards.datatransfer.ProjectConfigurator;
@@ -49,8 +51,11 @@
 	private IWorkspaceRoot workspaceRoot;
 	private ProjectConfiguratorExtensionManager configurationManager;
 	private RecursiveImportListener listener;
+
 	private Map<IProject, List<ProjectConfigurator>> report;
-	
+	private boolean isRootANewProject;
+	private boolean discardRootProject;
+
 	public EasymportJob(File rootDirectory, Set<IWorkingSet> workingSets, boolean recuriveConfigure) {
 		super(rootDirectory.getAbsolutePath());
 		this.workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
@@ -63,18 +68,19 @@
 		this.recursiveConfigure = recuriveConfigure;
 		this.report = new HashMap<>();
 	}
-	
+
 	public void setListener(RecursiveImportListener listener) {
 		this.listener = listener;
 	}
-	
+
 	@Override
 	protected IStatus run(IProgressMonitor monitor) {
 		try {
+			this.isRootANewProject = projectAlreadyExistsInWorkspace(this.rootDirectory) == null;
 			this.rootProject = toExistingOrNewProject(
 					this.rootDirectory,
 					monitor);
-			
+
 			if (this.recursiveConfigure) {
 		        IWorkspace workspace = ResourcesPlugin.getWorkspace();
 		        IWorkspaceDescription description = workspace.getDescription();
@@ -83,13 +89,30 @@
 		        	description.setAutoBuilding(false);
 		        	workspace.setDescription(description);
 		        }
-		        
+
 				importProjectAndChildrenRecursively(this.rootProject, true, monitor);
-				
+
 				if (isAutoBuilding) {
 					description.setAutoBuilding(true);
 		        	workspace.setDescription(description);
 				}
+
+				if (this.isRootANewProject && this.report.size() > 1 &&
+						(this.report.get(this.rootProject).isEmpty()
+						|| (this.report.get(this.rootProject).size() == 1 && this.report.get(this.rootProject).get(0) instanceof EclipseProjectConfigurator))) {
+					Display.getDefault().syncExec(new Runnable() {
+						@Override
+						public void run() {
+							discardRootProject = MessageDialog.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
+								Messages.discardRootProject_title,
+								Messages.discardRootProject_description);
+						}
+					});
+					if (this.discardRootProject) {
+						this.rootProject.delete(false, true, monitor);
+						this.report.remove(this.rootProject);
+					}
+				}
 			}
 		} catch (Exception ex) {
 			return new Status(IStatus.ERROR, Activator.PLUGIN_ID, ex.getMessage(), ex);
@@ -97,7 +120,7 @@
 		return Status.OK_STATUS;
 	}
 
-	
+
 	private final class CrawlFolderJob extends Job {
 		private final IFolder childFolder;
 		private final Set<IProject> res;
@@ -139,7 +162,7 @@
 			}
 		}
 		// TODO parallelize here
-		
+
 		for (final IFolder childFolder : childrenToProcess) {
 			CrawlFolderJob crawlerJob = new CrawlFolderJob("Crawling " + childFolder.getLocation().toString(), childFolder, res);
 			crawlerJob.run(progressMonitor);
@@ -151,7 +174,7 @@
 	 * @param folder
 	 * @param workingSets
 	 * @param progressMonitor
-	 * @param listener 
+	 * @param listener
 	 * @return
 	 * @throws Exception
 	 */
@@ -183,29 +206,29 @@
 					}
 					// use directly project for next analysis
 					container = project;
-					projectFromCurrentContainer.add(project);					
+					projectFromCurrentContainer.add(project);
 				}
 			} else {
 				secondaryConfigurators.add(configurator);
 			}
 			progressMonitor.worked(1);
 		}
-		
+
 
 		if (!mainProjectConfigurators.isEmpty()) {
 			for (ProjectConfigurator configurator : mainProjectConfigurators) {
 				configurator.configure(project, excludedPaths, progressMonitor);
 				this.report.get(project).add(configurator);
-				if (this.listener != null) { 
+				if (this.listener != null) {
 					listener.projectConfigured(project, configurator);
 				}
 				excludedPaths.addAll(toPathSet(configurator.getDirectoriesToIgnore(project, progressMonitor)));
 			}
 		}
-		
+
 		Set<IProject> allNestedProjects = searchAndImportChildrenProjectsRecursively(container, excludedPaths, progressMonitor);
 		excludedPaths.addAll(toPathSet(allNestedProjects));
-		
+
 		if (allNestedProjects.isEmpty() && isRootProject) {
 			// No sub-project found, so apply available configurators anyway
 			progressMonitor.beginTask("Configuring 'leaf' of project at " + container.getLocation().toFile().getAbsolutePath(), activeConfigurators.size());
@@ -215,7 +238,7 @@
 			}
 			projectFromCurrentContainer.add(project);
 		}
-		
+
 		if (project != null) {
 			// Apply secondary configurators
 			progressMonitor.beginTask("Continue configuration of project at " + container.getLocation().toFile().getAbsolutePath(), secondaryConfigurators.size());
@@ -237,7 +260,7 @@
 
 	private Set<IPath> toPathSet(Set<? extends IContainer> resources) {
 		if (resources == null || resources.isEmpty()) {
-			return (Set<IPath>)Collections.EMPTY_SET;
+			return Collections.EMPTY_SET;
 		}
 		Set<IPath> res = new HashSet<IPath>();
 		for (IContainer container : resources) {
@@ -308,11 +331,11 @@
 		PlatformUI.getWorkbench().getWorkingSetManager().addToWorkingSets(res, this.workingSets);
 		return res;
 	}
-	
+
 	public IProject getRootProject() {
 		return this.rootProject;
 	}
-	
+
 	public Map<IProject, List<ProjectConfigurator>> getConfiguredProjects() {
 		return report;
 	}
diff --git a/bundles/org.eclipse.e4.ui.importer/src/org/eclipse/ui/internal/wizards/datatransfer/EasymportJobReportDialog.java b/bundles/org.eclipse.e4.ui.importer/src/org/eclipse/ui/internal/wizards/datatransfer/EasymportJobReportDialog.java
index abf76ea..ddb31ed 100644
--- a/bundles/org.eclipse.e4.ui.importer/src/org/eclipse/ui/internal/wizards/datatransfer/EasymportJobReportDialog.java
+++ b/bundles/org.eclipse.e4.ui.importer/src/org/eclipse/ui/internal/wizards/datatransfer/EasymportJobReportDialog.java
@@ -39,50 +39,50 @@
 import org.eclipse.ui.wizards.datatransfer.ProjectConfigurator;
 
 public class EasymportJobReportDialog extends Dialog {
-	
+
 	private EasymportJob job;
 	private IJobChangeListener jobChangeListener;
-	
+
 	public EasymportJobReportDialog(Shell shell, EasymportJob job) {
 		super(shell);
-		setShellStyle(SWT.RESIZE);
+		setShellStyle(SWT.RESIZE | SWT.MIN);
 		this.job = job;
 		jobChangeListener = new IJobChangeListener() {
 			@Override
 			public void sleeping(IJobChangeEvent arg0) {
 			}
-			
+
 			@Override
 			public void scheduled(IJobChangeEvent arg0) {
 			}
-			
+
 			@Override
 			public void running(IJobChangeEvent arg0) {
 			}
-			
+
 			@Override
 			public void done(IJobChangeEvent job) {
 				if (job.getJob() == EasymportJobReportDialog.this.job && getShell() != null) {
 					getShell().getDisplay().asyncExec(new Runnable() {
 						@Override
 						public void run() {
-							updateButtons();		
+							updateButtons();
 						}
 					});
 				}
 			}
-			
+
 			@Override
 			public void awake(IJobChangeEvent arg0) {
 			}
-			
+
 			@Override
 			public void aboutToRun(IJobChangeEvent arg0) {
 			}
 		};
 		Job.getJobManager().addJobChangeListener(jobChangeListener);
 	}
-	
+
 	@Override
 	public Composite createDialogArea(Composite parent) {
 		getShell().setText(Messages.EasymportWizardPage_nestedProjects);
@@ -91,18 +91,18 @@
 		Composite res = new Composite(parent, SWT.NONE);
 		res.setLayout(new GridLayout(1, false));
 		res.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
-		
+
 		new Label(res, SWT.NONE).setText(Messages.EasymportWizardPage_importedProjects);
 		final TableViewer nestedProjectsTable = new TableViewer(res);
 		nestedProjectsTable.setContentProvider(new IStructuredContentProvider() {
 			@Override
 			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
 			}
-			
+
 			@Override
 			public void dispose() {
 			}
-			
+
 			@Override
 			public Object[] getElements(Object root) {
 				return ((Map<IProject, List<IContentProvider>>)root).entrySet().toArray();
@@ -127,7 +127,7 @@
 		GridData tableLayoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
 		tableLayoutData.heightHint = 400;
 		nestedProjectsTable.getControl().setLayoutData(tableLayoutData);
-		
+
 		TableViewerColumn projectColumn = new TableViewerColumn(nestedProjectsTable, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
 		projectColumn.getColumn().setWidth(200);
 		projectColumn.getColumn().setText(Messages.EasymportWizardPage_project);
@@ -137,7 +137,7 @@
 				return ((Entry<IProject, List<ProjectConfigurator>>)element).getKey().getName();
 			}
 		});
-		
+
 		TableViewerColumn configuratorsColumn = new TableViewerColumn(nestedProjectsTable, SWT.NONE);
 		configuratorsColumn.getColumn().setWidth(300);
 		configuratorsColumn.getColumn().setText(Messages.EasymportWizardPage_natures);
@@ -155,7 +155,7 @@
 				return builder.toString();
 			}
 		});
-		
+
 		TableViewerColumn relativePathColumn = new TableViewerColumn(nestedProjectsTable, SWT.LEFT);
 		relativePathColumn.getColumn().setText(Messages.EasymportWizardPage_relativePath);
 		relativePathColumn.getColumn().setWidth(300);
@@ -178,7 +178,7 @@
 					}
 				});
 			}
-			
+
 			@Override
 			public void projectConfigured(IProject project, ProjectConfigurator configurator) {
 				nestedProjectsTable.getControl().getDisplay().asyncExec(new Runnable() {
@@ -192,12 +192,12 @@
 			}
 		};
 		this.job.setListener(tableReportFiller);
-		
+
 		ProgressMonitorPart progressMonitorPart = new ProgressMonitorPart(res, null);
-		
+
 		return res;
 	}
-	
+
 	@Override
 	protected void createButtonsForButtonBar(Composite parent) {
 		createButton(parent, CANCEL, "Abort", false).addSelectionListener(new SelectionAdapter() {
@@ -215,7 +215,7 @@
 		});
 		updateButtons();
 	}
-	
+
 	private void updateButtons() {
 		getButton(OK).setEnabled(this.job.getResult() != null);
 		getButton(CANCEL).setEnabled(this.job.getResult() == null);
diff --git a/bundles/org.eclipse.e4.ui.importer/src/org/eclipse/ui/internal/wizards/datatransfer/Messages.java b/bundles/org.eclipse.e4.ui.importer/src/org/eclipse/ui/internal/wizards/datatransfer/Messages.java
index 6c9443e..74ae36f 100644
--- a/bundles/org.eclipse.e4.ui.importer/src/org/eclipse/ui/internal/wizards/datatransfer/Messages.java
+++ b/bundles/org.eclipse.e4.ui.importer/src/org/eclipse/ui/internal/wizards/datatransfer/Messages.java
@@ -41,6 +41,9 @@
 	public static String EasymportWizardPage_relativePath;
 	public static String EasymportWizardPage_project;
 	public static String EasymportWizardPage_natures;
+	
+	public static String discardRootProject_title;
+	public static String discardRootProject_description;
 
 
 }
diff --git a/bundles/org.eclipse.e4.ui.importer/src/org/eclipse/ui/internal/wizards/datatransfer/Messages.properties b/bundles/org.eclipse.e4.ui.importer/src/org/eclipse/ui/internal/wizards/datatransfer/Messages.properties
index ce782b3..4001f79 100644
--- a/bundles/org.eclipse.e4.ui.importer/src/org/eclipse/ui/internal/wizards/datatransfer/Messages.properties
+++ b/bundles/org.eclipse.e4.ui.importer/src/org/eclipse/ui/internal/wizards/datatransfer/Messages.properties
@@ -33,4 +33,7 @@
 EasymportWizardPage_importedProjects=Imported nested projects
 EasymportWizardPage_relativePath=Relative Path
 EasymportWizardPage_project=Project
-EasymportWizardPage_natures=Natures 
\ No newline at end of file
+EasymportWizardPage_natures=Natures
+discardRootProject_title=Discard root project?
+discardRootProject_description=It seems like the root directory you specified doesn't contain much development data. Do you want to discard it?\n\
+While keeping the root project may be helpful for navigation purpose (especially if you use nested view of projects), discarding it may improve the overall performances of your IDE.
\ No newline at end of file