Index: src/org/jboss/tools/maven/project/examples/AbstractImportMavenExample.java =================================================================== --- src/org/jboss/tools/maven/project/examples/AbstractImportMavenExample.java (revision 0) +++ src/org/jboss/tools/maven/project/examples/AbstractImportMavenExample.java (working copy) @@ -0,0 +1,39 @@ +/************************************************************************************* + * Copyright (c) 2008-2011 Red Hat, Inc. 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * JBoss by Red Hat - Initial implementation. + ************************************************************************************/ + +package org.jboss.tools.maven.project.examples; + + +import java.io.File; +import java.util.Map; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.jboss.tools.project.examples.model.AbstractImportProjectExample; +import org.jboss.tools.project.examples.model.ProjectExample; + +/** + * @author Fred Bricon + * + */ +public abstract class AbstractImportMavenExample extends AbstractImportProjectExample { + + protected abstract AbstractImportMavenProjectDelegate getDelegate(); + + @Override + public boolean importProject(ProjectExample projectDescription, File file, + Map propertiesMap, IProgressMonitor monitor) throws Exception { + + AbstractImportMavenProjectDelegate delegate = getDelegate(); + delegate.setLocation(getLocation()); + return delegate.importProject(projectDescription, file, propertiesMap, monitor); + } + +} Index: src/org/jboss/tools/maven/project/examples/AbstractImportMavenProjectDelegate.java =================================================================== --- src/org/jboss/tools/maven/project/examples/AbstractImportMavenProjectDelegate.java (revision 0) +++ src/org/jboss/tools/maven/project/examples/AbstractImportMavenProjectDelegate.java (working copy) @@ -0,0 +1,35 @@ +/************************************************************************************* + * Copyright (c) 2008-2011 Red Hat, Inc. 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * JBoss by Red Hat - Initial implementation. + ************************************************************************************/ +package org.jboss.tools.maven.project.examples; + +import java.io.File; +import java.util.Map; + +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.jboss.tools.project.examples.model.ProjectExample; + +abstract class AbstractImportMavenProjectDelegate { + + private IPath location; + + protected abstract boolean importProject(ProjectExample projectDescription, File file, + Map propertiesMap, IProgressMonitor monitor) throws Exception; + + protected void setLocation(IPath location) { + this.location = location; + } + + protected IPath getLocation() { + return location; + } + +} Index: src/org/jboss/tools/maven/project/examples/ImportMavenArchetypeProjectExample.java =================================================================== --- src/org/jboss/tools/maven/project/examples/ImportMavenArchetypeProjectExample.java (revision 39078) +++ src/org/jboss/tools/maven/project/examples/ImportMavenArchetypeProjectExample.java (working copy) @@ -12,104 +12,14 @@ package org.jboss.tools.maven.project.examples; -import java.io.File; -import java.util.ArrayList; -import java.util.Collection; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.m2e.core.MavenPlugin; -import org.eclipse.m2e.core.embedder.MavenModelManager; -import org.eclipse.m2e.core.project.LocalProjectScanner; -import org.eclipse.m2e.core.project.MavenProjectInfo; -import org.eclipse.m2e.core.project.ProjectImportConfiguration; -import org.eclipse.swt.widgets.Display; -import org.eclipse.swt.widgets.Shell; -import org.jboss.tools.project.examples.ProjectExamplesActivator; -import org.jboss.tools.project.examples.model.AbstractImportProjectExample; -import org.jboss.tools.project.examples.model.ProjectExample; - /** - * @author snjeza + * @author Fred Bricon * */ -public class ImportMavenArchetypeProjectExample extends - AbstractImportProjectExample { +public class ImportMavenArchetypeProjectExample extends AbstractImportMavenExample { - @Override - public boolean importProject(final ProjectExample projectDescription, File file, - Map propertiesMap, final IProgressMonitor monitor) throws Exception { - List projects = new ArrayList(); - projects.add(projectDescription); -// ArchetypeExamplesWizard wizard = new ArchetypeExamplesWizard(projectDescription); -// WizardDialog wizardDialog = new WizardDialog(getActiveShell(), wizard); -// int ok = wizardDialog.open(); -// if (ok != Window.OK) { -// return false; -// } - List includedProjects = projectDescription.getIncludedProjects(); - if (includedProjects == null) { - includedProjects = new ArrayList(); - projectDescription.setIncludedProjects(includedProjects); - } - projectDescription.getIncludedProjects().clear(); - String projectName = (String) propertiesMap.get(ProjectExamplesActivator.PROPERTY_PROJECT_NAME); - includedProjects.add(projectName); - String artifactId = (String) propertiesMap.get(ProjectExamplesActivator.PROPERTY_ARTIFACT_ID); - IPath location = (IPath) propertiesMap.get(ProjectExamplesActivator.PROPERTY_LOCATION_PATH); - String projectFolder = location.append(artifactId).toFile() - .getAbsolutePath(); - MavenModelManager mavenModelManager = MavenPlugin - .getMavenModelManager(); - LocalProjectScanner scanner = new LocalProjectScanner(location.toFile(), - projectFolder, true, mavenModelManager); - try { - scanner.run(monitor); - } catch (InterruptedException e1) { - return false; - } - - Set projectSet = collectProjects(scanner.getProjects()); - ProjectImportConfiguration importConfiguration = new ProjectImportConfiguration(); - - for (MavenProjectInfo info : projectSet) { - try { - projectName = MavenProjectExamplesActivator.getProjectName(info, importConfiguration); - if (!includedProjects.contains(projectName)) { - includedProjects.add(projectName); - } - } catch (CoreException e) { - MavenProjectExamplesActivator.log(e); - return false; - } - } - MavenProjectExamplesActivator.updateMavenConfiguration(projectName, includedProjects, monitor); - return true; + protected AbstractImportMavenProjectDelegate getDelegate() { + return new ImportMavenArchetypeProjectExampleDelegate(); } - - public Set collectProjects( - Collection projects) { - return new LinkedHashSet() { - private static final long serialVersionUID = 1L; - - public Set collectProjects( - Collection projects) { - for (MavenProjectInfo projectInfo : projects) { - add(projectInfo); - collectProjects(projectInfo.getProjects()); - } - return this; - } - }.collectProjects(projects); - } - - private static Shell getActiveShell() { - return Display.getDefault().getActiveShell(); - } - } Index: src/org/jboss/tools/maven/project/examples/ImportMavenArchetypeProjectExampleDelegate.java =================================================================== --- src/org/jboss/tools/maven/project/examples/ImportMavenArchetypeProjectExampleDelegate.java (revision 0) +++ src/org/jboss/tools/maven/project/examples/ImportMavenArchetypeProjectExampleDelegate.java (working copy) @@ -0,0 +1,113 @@ +/************************************************************************************* + * Copyright (c) 2008-2011 Red Hat, Inc. 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * JBoss by Red Hat - Initial implementation. + ************************************************************************************/ + +package org.jboss.tools.maven.project.examples; + + +import java.io.File; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.m2e.core.MavenPlugin; +import org.eclipse.m2e.core.embedder.MavenModelManager; +import org.eclipse.m2e.core.project.LocalProjectScanner; +import org.eclipse.m2e.core.project.MavenProjectInfo; +import org.eclipse.m2e.core.project.ProjectImportConfiguration; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.jboss.tools.project.examples.ProjectExamplesActivator; +import org.jboss.tools.project.examples.model.ProjectExample; + +/** + * @author snjeza + * + */ +public class ImportMavenArchetypeProjectExampleDelegate extends AbstractImportMavenProjectDelegate { + + @Override + public boolean importProject(final ProjectExample projectDescription, File file, + Map propertiesMap, final IProgressMonitor monitor) throws Exception { + List projects = new ArrayList(); + projects.add(projectDescription); +// ArchetypeExamplesWizard wizard = new ArchetypeExamplesWizard(projectDescription); +// WizardDialog wizardDialog = new WizardDialog(getActiveShell(), wizard); +// int ok = wizardDialog.open(); +// if (ok != Window.OK) { +// return false; +// } + List includedProjects = projectDescription.getIncludedProjects(); + if (includedProjects == null) { + includedProjects = new ArrayList(); + projectDescription.setIncludedProjects(includedProjects); + } + projectDescription.getIncludedProjects().clear(); + String projectName = (String) propertiesMap.get(ProjectExamplesActivator.PROPERTY_PROJECT_NAME); + includedProjects.add(projectName); + String artifactId = (String) propertiesMap.get(ProjectExamplesActivator.PROPERTY_ARTIFACT_ID); + IPath location = (IPath) propertiesMap.get(ProjectExamplesActivator.PROPERTY_LOCATION_PATH); + String projectFolder = location.append(artifactId).toFile() + .getAbsolutePath(); + MavenModelManager mavenModelManager = MavenPlugin + .getMavenModelManager(); + LocalProjectScanner scanner = new LocalProjectScanner(location.toFile(), + projectFolder, true, mavenModelManager); + try { + scanner.run(monitor); + } catch (InterruptedException e1) { + return false; + } + + Set projectSet = collectProjects(scanner.getProjects()); + ProjectImportConfiguration importConfiguration = new ProjectImportConfiguration(); + + for (MavenProjectInfo info : projectSet) { + try { + projectName = MavenProjectExamplesActivator.getProjectName(info, importConfiguration); + if (!includedProjects.contains(projectName)) { + includedProjects.add(projectName); + } + } catch (CoreException e) { + MavenProjectExamplesActivator.log(e); + return false; + } + } + MavenProjectExamplesActivator.updateMavenConfiguration(projectName, includedProjects, monitor); + return true; + } + + public Set collectProjects( + Collection projects) { + return new LinkedHashSet() { + private static final long serialVersionUID = 1L; + + public Set collectProjects( + Collection projects) { + for (MavenProjectInfo projectInfo : projects) { + add(projectInfo); + collectProjects(projectInfo.getProjects()); + } + return this; + } + }.collectProjects(projects); + } + + private static Shell getActiveShell() { + return Display.getDefault().getActiveShell(); + } + +} Index: src/org/jboss/tools/maven/project/examples/ImportMavenProjectExample.java =================================================================== --- src/org/jboss/tools/maven/project/examples/ImportMavenProjectExample.java (revision 39078) +++ src/org/jboss/tools/maven/project/examples/ImportMavenProjectExample.java (working copy) @@ -10,341 +10,15 @@ ************************************************************************************/ package org.jboss.tools.maven.project.examples; -import java.io.File; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import org.apache.maven.model.Model; -import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.jface.dialogs.MessageDialog; -import org.eclipse.jface.preference.IPreferenceStore; -import org.eclipse.m2e.core.MavenPlugin; -import org.eclipse.m2e.core.embedder.MavenModelManager; -import org.eclipse.m2e.core.project.AbstractProjectScanner; -import org.eclipse.m2e.core.project.LocalProjectScanner; -import org.eclipse.m2e.core.project.MavenProjectInfo; -import org.eclipse.m2e.core.project.ProjectImportConfiguration; -import org.eclipse.m2e.core.ui.internal.actions.OpenMavenConsoleAction; -import org.eclipse.swt.widgets.Display; -import org.eclipse.swt.widgets.Shell; -import org.jboss.tools.maven.ui.Activator; -import org.jboss.tools.project.examples.ProjectExamplesActivator; -import org.jboss.tools.project.examples.model.AbstractImportProjectExample; -import org.jboss.tools.project.examples.model.ProjectExample; - /** - * @author snjeza + * @author Fred Bricon * */ -public class ImportMavenProjectExample extends AbstractImportProjectExample { +public class ImportMavenProjectExample extends AbstractImportMavenExample { - private static final String UNNAMED_PROJECTS = "UnnamedProjects"; //$NON-NLS-1$ - - private boolean confirm; - @Override - public boolean importProject(ProjectExample projectDescription, File file, - Map propertiesMap, IProgressMonitor monitor) throws Exception { - List projects = new ArrayList(); - projects.add(projectDescription); - IPath rootPath = getLocation(); - IPath mavenProjectsRoot = rootPath; - String projectName = projectDescription.getName(); - if (projectName == null || projectName.isEmpty()) { - projectName = UNNAMED_PROJECTS; - } - IPath path = mavenProjectsRoot.append(projectName); - final File destination = new File(path.toOSString()); - if (destination.exists()) { - final List existingProjects = getExistingProjects(destination); - if (existingProjects.size() > 0) { - Display.getDefault().syncExec(new Runnable() { - public void run() { - String title = "Overwrite"; - String msg = getMessage(destination, existingProjects); - confirm = MessageDialog.openQuestion(getActiveShell(), - title, msg); - } - }); - if (confirm) { - monitor.setTaskName("Deleting ..."); - for (IProject project:existingProjects) { - monitor.setTaskName("Deleting " + project.getName()); - project.delete(false, true, monitor); - } - } else { - return false; - } - } - boolean deleted = deleteDirectory(destination, monitor); - if (monitor.isCanceled()) { - return false; - } - if (!deleted) { - Display.getDefault().syncExec(new Runnable() { - - @Override - public void run() { - MessageDialog.openError(getActiveShell(), - "Error", "Cannot delete the '" + destination + "' file."); - } - }); - return false; - } - } - boolean ok = ProjectExamplesActivator.extractZipFile(file, destination, monitor); - monitor.setTaskName(""); - if (monitor.isCanceled()) { - return false; - } - if (!ok) { - Display.getDefault().syncExec(new Runnable() { - - @Override - public void run() { - MessageDialog.openError(getActiveShell(), - "Error", - "Cannot extract the archive."); - } - }); - return false; - } - IPreferenceStore store = Activator.getDefault().getPreferenceStore(); - boolean configureSeam = store.getBoolean(Activator.CONFIGURE_SEAM); - boolean configureJSF = store.getBoolean(Activator.CONFIGURE_JSF); - boolean configurePortlet = store.getBoolean(Activator.CONFIGURE_PORTLET); - boolean configureJSFPortlet = store.getBoolean(Activator.CONFIGURE_JSFPORTLET); - boolean configureSeamPortlet = store.getBoolean(Activator.CONFIGURE_SEAMPORTLET); - boolean configureCDI = store.getBoolean(Activator.CONFIGURE_CDI); - boolean configureHibernate = store.getBoolean(Activator.CONFIGURE_HIBERNATE); - boolean configureJaxRs = store.getBoolean(Activator.CONFIGURE_JAXRS); - List projectNames; - try { - store.setValue(Activator.CONFIGURE_SEAM, false); - store.setValue(Activator.CONFIGURE_JSF, false); - store.setValue(Activator.CONFIGURE_PORTLET, false); - store.setValue(Activator.CONFIGURE_JSFPORTLET, false); - store.setValue(Activator.CONFIGURE_SEAMPORTLET, false); - store.setValue(Activator.CONFIGURE_CDI, false); - store.setValue(Activator.CONFIGURE_HIBERNATE, false); - store.setValue(Activator.CONFIGURE_JAXRS, false); - projectNames = importMavenProjects(destination, projectDescription, monitor); - } finally { - store.setValue(Activator.CONFIGURE_SEAM, configureSeam); - store.setValue(Activator.CONFIGURE_JSF, configureJSF); - store.setValue(Activator.CONFIGURE_PORTLET, configurePortlet); - store.setValue(Activator.CONFIGURE_JSFPORTLET, configureJSFPortlet); - store.setValue(Activator.CONFIGURE_SEAMPORTLET, configureSeamPortlet); - store.setValue(Activator.CONFIGURE_CDI, configureCDI); - store.setValue(Activator.CONFIGURE_HIBERNATE, configureHibernate); - store.setValue(Activator.CONFIGURE_JAXRS, configureJaxRs); - } - new OpenMavenConsoleAction().run(); - List includedProjects = projectDescription.getIncludedProjects(); - includedProjects.clear(); - projectDescription.getIncludedProjects().addAll(projectNames); - MavenProjectExamplesActivator.updateMavenConfiguration(projectName, includedProjects, monitor); - return true; + protected AbstractImportMavenProjectDelegate getDelegate() { + return new ImportMavenProjectExampleDelegate(); } - - private List importMavenProjects(final File destination, - final ProjectExample projectDescription, IProgressMonitor monitor) { - List projectNames = new ArrayList(); - MavenPlugin plugin = MavenPlugin.getDefault(); - try { - AbstractProjectScanner projectScanner = getProjectScanner(destination); - projectScanner.run(monitor); - List mavenProjects = projectScanner.getProjects(); - List infos = new ArrayList(); - infos.addAll(mavenProjects); - addMavenProjects(infos, mavenProjects); - final List existingProjects = new ArrayList(); - ProjectImportConfiguration importConfiguration = new ProjectImportConfiguration(); - String profiles = projectDescription.getDefaultProfiles(); - if (profiles != null && profiles.trim().length() > 0) { - importConfiguration.getResolverConfiguration() - .setActiveProfiles(profiles); - } - for (MavenProjectInfo info : infos) { - String projectName = MavenProjectExamplesActivator.getProjectName(info, importConfiguration); - IProject project = ResourcesPlugin.getWorkspace().getRoot() - .getProject(projectName); - if (project != null && project.exists()) { - existingProjects.add(project); - } - } - if (existingProjects.size() > 0) { - Display.getDefault().syncExec(new Runnable() { - - @Override - public void run() { - String message = getWorkspaceMessage(existingProjects); - - confirm = MessageDialog.openConfirm(getActiveShell(), - "Confirmation", message); - } - }); - if (confirm) { - for (IProject project : existingProjects) { - try { - project.refreshLocal(IResource.DEPTH_INFINITE, - monitor); - } catch (Exception e) { - // ignore - } - project.delete(true, true, monitor); - } - } else { - return projectNames; - } - } - List includedProjects = projectDescription - .getIncludedProjects(); - if (includedProjects != null && includedProjects.size() > 0) { - List newInfos = new ArrayList(); - for (MavenProjectInfo info : infos) { - Model model = info.getModel(); - if (model != null && model.getArtifactId() != null - && model.getArtifactId().trim().length() > 0) { - for (String includedProject : includedProjects) { - if (model.getArtifactId().equals(includedProject)) { - newInfos.add(info); - } - } - } - } - infos = newInfos; - } - MavenPlugin.getProjectConfigurationManager().importProjects(infos, - importConfiguration, monitor); - for (MavenProjectInfo info : infos) { - Model model = info.getModel(); - if (model != null && model.getArtifactId() != null - && model.getArtifactId().trim().length() > 0) { - projectNames.add(model.getArtifactId()); - } - } - } catch (CoreException ex) { - MavenProjectExamplesActivator.log(ex, - "Projects imported with errors"); - return projectNames; - } catch (InterruptedException e) { - MavenProjectExamplesActivator.log(e, - "Projects imported with errors"); - return projectNames; - } - return projectNames; - } - - private List addMavenProjects(List infos, List mavenProjects) { - if (mavenProjects == null || mavenProjects.isEmpty()) { - return mavenProjects; - } - for (MavenProjectInfo projectInfo:mavenProjects) { - Collection projects = projectInfo.getProjects(); - if (projects != null && !projects.isEmpty()) { - for(MavenProjectInfo info:projects) { - infos.add(info); - } - List childProjects = new ArrayList(); - childProjects.addAll(projects); - addMavenProjects(infos, childProjects); - } - } - return mavenProjects; - } - - private AbstractProjectScanner getProjectScanner( - File folder) { - File root = ResourcesPlugin.getWorkspace().getRoot().getLocation() - .toFile(); - MavenPlugin mavenPlugin = MavenPlugin.getDefault(); - MavenModelManager modelManager = mavenPlugin.getMavenModelManager(); - return new LocalProjectScanner(root, folder.getAbsolutePath(), false, - modelManager); - } - - private static Shell getActiveShell() { - return Display.getDefault().getActiveShell(); - } - - private static boolean deleteDirectory(File path, IProgressMonitor monitor) { - if (path.exists()) { - File[] files = path.listFiles(); - for (File file : files) { - if (monitor.isCanceled()) { - return false; - } - monitor.setTaskName("Deleting " + file); - if (file.isDirectory()) { - deleteDirectory(file, monitor); - } else { - file.delete(); - } - } - } - return (path.delete()); - } - - private List getExistingProjects(final File destination) { - List existingProjects = new ArrayList(); - IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); - for (IProject project:projects) { - if (project != null && project.exists()) { - File projectFile = project.getLocation().toFile(); - if (projectFile.getAbsolutePath().startsWith(destination.getAbsolutePath())) { - existingProjects.add(project); - } - } - } - return existingProjects; - } - - private String getMessage(final File destination, List projects) { - if (projects.size() > 0) { - StringBuilder builder = new StringBuilder(); - if (projects.size() == 1) { - builder.append("\nThere is the '" + projects.get(0).getName() + - "' project on the destination location:\n\n"); - builder.append("Would you like to overwrite it?"); - } else { - builder.append("\nThere are the following projects on the destination location:\n\n"); - for (IProject project : projects) { - builder.append(project.getName()); - builder.append("\n"); //$NON-NLS-1$ - } - builder.append("\n"); //$NON-NLS-1$ - builder.append("Would you like to overwrite them?"); - } - return builder.toString(); - } - return null; - } - - private String getWorkspaceMessage( - final List existingProjects) { - StringBuilder builder = new StringBuilder(); - if (existingProjects.size() == 1) { - builder.append("There is the '" + existingProjects.get(0).getName() + - "' project in the workspace.\n\n"); - builder.append("Would you like to delete it?"); - } else { - builder.append("There are the following projects in the workspace:\n\n"); - for (IProject project:existingProjects) { - builder.append(project.getName()); - builder.append("\n"); //$NON-NLS-1$ - } - builder.append("\n"); //$NON-NLS-1$ - builder.append("Would you like to delete them?"); - } - return builder.toString(); - } - } Index: src/org/jboss/tools/maven/project/examples/ImportMavenProjectExampleDelegate.java =================================================================== --- src/org/jboss/tools/maven/project/examples/ImportMavenProjectExampleDelegate.java (revision 0) +++ src/org/jboss/tools/maven/project/examples/ImportMavenProjectExampleDelegate.java (working copy) @@ -0,0 +1,349 @@ +/************************************************************************************* + * Copyright (c) 2008-2011 Red Hat, Inc. 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * JBoss by Red Hat - Initial implementation. + ************************************************************************************/ +package org.jboss.tools.maven.project.examples; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import org.apache.maven.model.Model; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.m2e.core.MavenPlugin; +import org.eclipse.m2e.core.embedder.MavenModelManager; +import org.eclipse.m2e.core.project.AbstractProjectScanner; +import org.eclipse.m2e.core.project.LocalProjectScanner; +import org.eclipse.m2e.core.project.MavenProjectInfo; +import org.eclipse.m2e.core.project.ProjectImportConfiguration; +import org.eclipse.m2e.core.ui.internal.actions.OpenMavenConsoleAction; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; +import org.jboss.tools.maven.ui.Activator; +import org.jboss.tools.project.examples.ProjectExamplesActivator; +import org.jboss.tools.project.examples.model.ProjectExample; + +/** + * @author snjeza + * + */ +public class ImportMavenProjectExampleDelegate extends AbstractImportMavenProjectDelegate { + + private static final String UNNAMED_PROJECTS = "UnnamedProjects"; //$NON-NLS-1$ + + private boolean confirm; + + @Override + public boolean importProject(ProjectExample projectDescription, File file, + Map propertiesMap, IProgressMonitor monitor) throws Exception { + List projects = new ArrayList(); + projects.add(projectDescription); + IPath rootPath = getLocation(); + IPath mavenProjectsRoot = rootPath; + String projectName = projectDescription.getName(); + if (projectName == null || projectName.isEmpty()) { + projectName = UNNAMED_PROJECTS; + } + IPath path = mavenProjectsRoot.append(projectName); + final File destination = new File(path.toOSString()); + if (destination.exists()) { + final List existingProjects = getExistingProjects(destination); + if (existingProjects.size() > 0) { + Display.getDefault().syncExec(new Runnable() { + public void run() { + String title = "Overwrite"; + String msg = getMessage(destination, existingProjects); + confirm = MessageDialog.openQuestion(getActiveShell(), + title, msg); + } + }); + if (confirm) { + monitor.setTaskName("Deleting ..."); + for (IProject project:existingProjects) { + monitor.setTaskName("Deleting " + project.getName()); + project.delete(false, true, monitor); + } + } else { + return false; + } + } + boolean deleted = deleteDirectory(destination, monitor); + if (monitor.isCanceled()) { + return false; + } + if (!deleted) { + Display.getDefault().syncExec(new Runnable() { + + @Override + public void run() { + MessageDialog.openError(getActiveShell(), + "Error", "Cannot delete the '" + destination + "' file."); + } + }); + return false; + } + } + boolean ok = ProjectExamplesActivator.extractZipFile(file, destination, monitor); + monitor.setTaskName(""); + if (monitor.isCanceled()) { + return false; + } + if (!ok) { + Display.getDefault().syncExec(new Runnable() { + + @Override + public void run() { + MessageDialog.openError(getActiveShell(), + "Error", + "Cannot extract the archive."); + } + }); + return false; + } + IPreferenceStore store = Activator.getDefault().getPreferenceStore(); + boolean configureSeam = store.getBoolean(Activator.CONFIGURE_SEAM); + boolean configureJSF = store.getBoolean(Activator.CONFIGURE_JSF); + boolean configurePortlet = store.getBoolean(Activator.CONFIGURE_PORTLET); + boolean configureJSFPortlet = store.getBoolean(Activator.CONFIGURE_JSFPORTLET); + boolean configureSeamPortlet = store.getBoolean(Activator.CONFIGURE_SEAMPORTLET); + boolean configureCDI = store.getBoolean(Activator.CONFIGURE_CDI); + boolean configureHibernate = store.getBoolean(Activator.CONFIGURE_HIBERNATE); + boolean configureJaxRs = store.getBoolean(Activator.CONFIGURE_JAXRS); + List projectNames; + try { + store.setValue(Activator.CONFIGURE_SEAM, false); + store.setValue(Activator.CONFIGURE_JSF, false); + store.setValue(Activator.CONFIGURE_PORTLET, false); + store.setValue(Activator.CONFIGURE_JSFPORTLET, false); + store.setValue(Activator.CONFIGURE_SEAMPORTLET, false); + store.setValue(Activator.CONFIGURE_CDI, false); + store.setValue(Activator.CONFIGURE_HIBERNATE, false); + store.setValue(Activator.CONFIGURE_JAXRS, false); + projectNames = importMavenProjects(destination, projectDescription, monitor); + } finally { + store.setValue(Activator.CONFIGURE_SEAM, configureSeam); + store.setValue(Activator.CONFIGURE_JSF, configureJSF); + store.setValue(Activator.CONFIGURE_PORTLET, configurePortlet); + store.setValue(Activator.CONFIGURE_JSFPORTLET, configureJSFPortlet); + store.setValue(Activator.CONFIGURE_SEAMPORTLET, configureSeamPortlet); + store.setValue(Activator.CONFIGURE_CDI, configureCDI); + store.setValue(Activator.CONFIGURE_HIBERNATE, configureHibernate); + store.setValue(Activator.CONFIGURE_JAXRS, configureJaxRs); + } + new OpenMavenConsoleAction().run(); + List includedProjects = projectDescription.getIncludedProjects(); + includedProjects.clear(); + projectDescription.getIncludedProjects().addAll(projectNames); + MavenProjectExamplesActivator.updateMavenConfiguration(projectName, includedProjects, monitor); + return true; + } + + private List importMavenProjects(final File destination, + final ProjectExample projectDescription, IProgressMonitor monitor) { + List projectNames = new ArrayList(); + MavenPlugin plugin = MavenPlugin.getDefault(); + try { + AbstractProjectScanner projectScanner = getProjectScanner(destination); + projectScanner.run(monitor); + List mavenProjects = projectScanner.getProjects(); + List infos = new ArrayList(); + infos.addAll(mavenProjects); + addMavenProjects(infos, mavenProjects); + final List existingProjects = new ArrayList(); + ProjectImportConfiguration importConfiguration = new ProjectImportConfiguration(); + String profiles = projectDescription.getDefaultProfiles(); + if (profiles != null && profiles.trim().length() > 0) { + importConfiguration.getResolverConfiguration() + .setActiveProfiles(profiles); + } + for (MavenProjectInfo info : infos) { + String projectName = MavenProjectExamplesActivator.getProjectName(info, importConfiguration); + IProject project = ResourcesPlugin.getWorkspace().getRoot() + .getProject(projectName); + if (project != null && project.exists()) { + existingProjects.add(project); + } + } + if (existingProjects.size() > 0) { + Display.getDefault().syncExec(new Runnable() { + + @Override + public void run() { + String message = getWorkspaceMessage(existingProjects); + + confirm = MessageDialog.openConfirm(getActiveShell(), + "Confirmation", message); + } + }); + if (confirm) { + for (IProject project : existingProjects) { + try { + project.refreshLocal(IResource.DEPTH_INFINITE, + monitor); + } catch (Exception e) { + // ignore + } + project.delete(true, true, monitor); + } + } else { + return projectNames; + } + } + List includedProjects = projectDescription + .getIncludedProjects(); + if (includedProjects != null && includedProjects.size() > 0) { + List newInfos = new ArrayList(); + for (MavenProjectInfo info : infos) { + Model model = info.getModel(); + if (model != null && model.getArtifactId() != null + && model.getArtifactId().trim().length() > 0) { + for (String includedProject : includedProjects) { + if (model.getArtifactId().equals(includedProject)) { + newInfos.add(info); + } + } + } + } + infos = newInfos; + } + MavenPlugin.getProjectConfigurationManager().importProjects(infos, + importConfiguration, monitor); + for (MavenProjectInfo info : infos) { + Model model = info.getModel(); + if (model != null && model.getArtifactId() != null + && model.getArtifactId().trim().length() > 0) { + projectNames.add(model.getArtifactId()); + } + } + } catch (CoreException ex) { + MavenProjectExamplesActivator.log(ex, + "Projects imported with errors"); + return projectNames; + } catch (InterruptedException e) { + MavenProjectExamplesActivator.log(e, + "Projects imported with errors"); + return projectNames; + } + return projectNames; + } + + private List addMavenProjects(List infos, List mavenProjects) { + if (mavenProjects == null || mavenProjects.isEmpty()) { + return mavenProjects; + } + for (MavenProjectInfo projectInfo:mavenProjects) { + Collection projects = projectInfo.getProjects(); + if (projects != null && !projects.isEmpty()) { + for(MavenProjectInfo info:projects) { + infos.add(info); + } + List childProjects = new ArrayList(); + childProjects.addAll(projects); + addMavenProjects(infos, childProjects); + } + } + return mavenProjects; + } + + private AbstractProjectScanner getProjectScanner( + File folder) { + File root = ResourcesPlugin.getWorkspace().getRoot().getLocation() + .toFile(); + MavenPlugin mavenPlugin = MavenPlugin.getDefault(); + MavenModelManager modelManager = mavenPlugin.getMavenModelManager(); + return new LocalProjectScanner(root, folder.getAbsolutePath(), false, + modelManager); + } + + private static Shell getActiveShell() { + return Display.getDefault().getActiveShell(); + } + + private static boolean deleteDirectory(File path, IProgressMonitor monitor) { + if (path.exists()) { + File[] files = path.listFiles(); + for (File file : files) { + if (monitor.isCanceled()) { + return false; + } + monitor.setTaskName("Deleting " + file); + if (file.isDirectory()) { + deleteDirectory(file, monitor); + } else { + file.delete(); + } + } + } + return (path.delete()); + } + + private List getExistingProjects(final File destination) { + List existingProjects = new ArrayList(); + IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); + for (IProject project:projects) { + if (project != null && project.exists()) { + File projectFile = project.getLocation().toFile(); + if (projectFile.getAbsolutePath().startsWith(destination.getAbsolutePath())) { + existingProjects.add(project); + } + } + } + return existingProjects; + } + + private String getMessage(final File destination, List projects) { + if (projects.size() > 0) { + StringBuilder builder = new StringBuilder(); + if (projects.size() == 1) { + builder.append("\nThere is the '" + projects.get(0).getName() + + "' project on the destination location:\n\n"); + builder.append("Would you like to overwrite it?"); + } else { + builder.append("\nThere are the following projects on the destination location:\n\n"); + for (IProject project : projects) { + builder.append(project.getName()); + builder.append("\n"); //$NON-NLS-1$ + } + builder.append("\n"); //$NON-NLS-1$ + builder.append("Would you like to overwrite them?"); + } + return builder.toString(); + } + return null; + } + + private String getWorkspaceMessage( + final List existingProjects) { + StringBuilder builder = new StringBuilder(); + if (existingProjects.size() == 1) { + builder.append("There is the '" + existingProjects.get(0).getName() + + "' project in the workspace.\n\n"); + builder.append("Would you like to delete it?"); + } else { + builder.append("There are the following projects in the workspace:\n\n"); + for (IProject project:existingProjects) { + builder.append(project.getName()); + builder.append("\n"); //$NON-NLS-1$ + } + builder.append("\n"); //$NON-NLS-1$ + builder.append("Would you like to delete them?"); + } + return builder.toString(); + } + +}