[jbosstools-commits] JBoss Tools SVN: r44098 - in trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/application/importoperation: project and 1 other directory.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Thu Sep 27 11:51:25 EDT 2012


Author: adietish
Date: 2012-09-27 11:51:25 -0400 (Thu, 27 Sep 2012)
New Revision: 44098

Added:
   trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/application/importoperation/project/
   trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/application/importoperation/project/AbstractProjectImportOperation.java
   trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/application/importoperation/project/GeneralProjectImportOperation.java
   trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/application/importoperation/project/MavenProjectImportOperation.java
Log:
split wizard package into distinct parts

Added: trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/application/importoperation/project/AbstractProjectImportOperation.java
===================================================================
--- trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/application/importoperation/project/AbstractProjectImportOperation.java	                        (rev 0)
+++ trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/application/importoperation/project/AbstractProjectImportOperation.java	2012-09-27 15:51:25 UTC (rev 44098)
@@ -0,0 +1,35 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ *     Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.openshift.express.internal.ui.wizard.application.importoperation.project;
+
+import java.io.File;
+
+/**
+ * @author André Dietisheim <adietish at redhat.com>
+ */
+public class AbstractProjectImportOperation {
+
+	private File projectFolder;
+
+	public AbstractProjectImportOperation(File projectDirectory) {
+		this.projectFolder = projectDirectory;
+	}
+
+	protected File getProjectDirectory() {
+		return projectFolder;
+	}
+
+	protected boolean isReadable(File destination) {
+		return destination != null
+				&& destination.exists()
+				&& destination.canRead();
+	}
+}
\ No newline at end of file

Added: trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/application/importoperation/project/GeneralProjectImportOperation.java
===================================================================
--- trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/application/importoperation/project/GeneralProjectImportOperation.java	                        (rev 0)
+++ trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/application/importoperation/project/GeneralProjectImportOperation.java	2012-09-27 15:51:25 UTC (rev 44098)
@@ -0,0 +1,87 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ *     Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.openshift.express.internal.ui.wizard.application.importoperation.project;
+
+import java.io.File;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IWorkspace;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.PlatformUI;
+
+/**
+ * @author Andre Dietisheim <adietish at redhat.com>
+ * 
+ */
+public class GeneralProjectImportOperation extends AbstractProjectImportOperation {
+
+	public GeneralProjectImportOperation(File projectDirectory) {
+		super(projectDirectory);
+	}
+
+	public List<IProject> importToWorkspace(IProgressMonitor monitor)
+			throws CoreException, InterruptedException {
+
+		IWorkspace workspace = ResourcesPlugin.getWorkspace();
+		IProject project = workspace.getRoot().getProject(getProjectDirectory().getName());
+		overwriteExistingProject(project, workspace, monitor);
+		importToWorkspace(getProjectDirectory(), workspace, monitor);
+		return Collections.singletonList(project);
+	}
+
+	private void importToWorkspace(File projectDirectory, IWorkspace workspace, IProgressMonitor monitor)
+			throws CoreException {
+		String projectName = projectDirectory.getName();
+		IProjectDescription description = workspace.newProjectDescription(projectName);
+		description.setLocation(Path.fromOSString(projectDirectory.getAbsolutePath()));
+		IProject project = workspace.getRoot().getProject(projectName);
+		project.create(description, monitor);
+		project.open(IResource.BACKGROUND_REFRESH, monitor);
+	}
+
+	private void overwriteExistingProject(final IProject project, IWorkspace workspace, IProgressMonitor monitor)
+			throws CoreException {
+		if (project == null
+				|| !project.exists()) {
+			return;
+		}
+
+		final boolean[] overwrite = new boolean[1];
+		Display.getDefault().syncExec(new Runnable() {
+
+			public void run() {
+				overwrite[0] = MessageDialog.openQuestion(
+						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
+						"Overwrite project?",
+						NLS.bind(
+								"A project \"{0}\" already exists in the workspace.\n"
+										+ "If you want to import the OpenShift \"{0}\", the project in your workspace will "
+										+ "get overwritten and may not be recovered.\n\n"
+										+ "Are you sure that you want to overwrite the project \"{0}\" in your workspace?",
+								project.getName()));
+			}
+
+		});
+		if (overwrite[0]) {
+			project.delete(true, true, monitor);
+		}
+	}
+}

Added: trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/application/importoperation/project/MavenProjectImportOperation.java
===================================================================
--- trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/application/importoperation/project/MavenProjectImportOperation.java	                        (rev 0)
+++ trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/application/importoperation/project/MavenProjectImportOperation.java	2012-09-27 15:51:25 UTC (rev 44098)
@@ -0,0 +1,98 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ *     Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.openshift.express.internal.ui.wizard.application.importoperation.project;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.m2e.core.embedder.MavenModelManager;
+import org.eclipse.m2e.core.internal.MavenPluginActivator;
+import org.eclipse.m2e.core.project.IMavenProjectImportResult;
+import org.eclipse.m2e.core.project.IProjectConfigurationManager;
+import org.eclipse.m2e.core.project.LocalProjectScanner;
+import org.eclipse.m2e.core.project.MavenProjectInfo;
+import org.eclipse.m2e.core.project.ProjectImportConfiguration;
+
+/**
+ * @author Andre Dietisheim <adietish at redhat.com>
+ * 
+ */
+public class MavenProjectImportOperation extends AbstractProjectImportOperation {
+
+	private static final String POM_FILE = "pom.xml";
+
+	public MavenProjectImportOperation(File projectFolder) {
+		super(projectFolder);
+	}
+
+	public List<IProject> importToWorkspace(IProgressMonitor monitor)
+			throws CoreException, InterruptedException {
+		MavenPluginActivator mavenPlugin = MavenPluginActivator.getDefault();
+		IProjectConfigurationManager configurationManager = mavenPlugin.getProjectConfigurationManager();
+		MavenModelManager modelManager = mavenPlugin.getMavenModelManager();
+		Set<MavenProjectInfo> projectInfos = getMavenProjects(getProjectDirectory(), modelManager, monitor);
+		ProjectImportConfiguration projectImportConfiguration = new ProjectImportConfiguration();
+		List<IMavenProjectImportResult> importResults =
+				configurationManager.importProjects(projectInfos, projectImportConfiguration, monitor);
+		return toProjects(importResults);
+	}
+
+	private List<IProject> toProjects(List<IMavenProjectImportResult> importResults) {
+		List<IProject> projects = new ArrayList<IProject>();
+		for (IMavenProjectImportResult importResult : importResults) {
+			IProject project = importResult.getProject();
+			if (project != null) {
+				projects.add(importResult.getProject());
+			}
+		}
+		return projects;
+	}
+
+	private Set<MavenProjectInfo> getMavenProjects(File directory, MavenModelManager modelManager,
+			IProgressMonitor monitor) throws InterruptedException {
+		LocalProjectScanner scanner = new LocalProjectScanner(directory.getParentFile(), directory.toString(), false,
+				modelManager);
+		scanner.run(monitor);
+		return collectProjects(scanner.getProjects());
+	}
+
+	public boolean isMavenProject() {
+		if (!isReadable(getProjectDirectory())
+				|| !getProjectDirectory().isDirectory()) {
+			return false;
+		}
+
+		return isReadable(new File(getProjectDirectory(), POM_FILE));
+	}
+	
+	public Set<MavenProjectInfo> collectProjects(
+			Collection<MavenProjectInfo> projects) {
+		return new LinkedHashSet<MavenProjectInfo>() {
+			private static final long serialVersionUID = 1L;
+
+			public Set<MavenProjectInfo> collectProjects(
+					Collection<MavenProjectInfo> projects) {
+				for (MavenProjectInfo projectInfo : projects) {
+					add(projectInfo);
+					collectProjects(projectInfo.getProjects());
+				}
+				return this;
+			}
+		}.collectProjects(projects);
+	}
+}



More information about the jbosstools-commits mailing list