From 8e7448a81c884101bf87c2bdaaca1221a04c33d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Galland?= Date: Thu, 29 Nov 2018 22:39:21 +0100 Subject: [PATCH] [eclipse] Add the action "Convert to SARL Maven Project". MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This action enables to create a Maven-based project with SARL nature. close #890 Signed-off-by: Stéphane Galland --- .../OSGI-INF/l10n/bundle.properties | 3 +- main/coreplugins/io.sarl.m2e/plugin.xml | 22 ++ .../EnableSarlMavenNatureAction.java | 206 ++++++++++++++++++ .../m2e/wizards/importproject/Messages.java | 46 ++++ .../wizards/importproject/messages.properties | 1 + 5 files changed, 277 insertions(+), 1 deletion(-) create mode 100644 main/coreplugins/io.sarl.m2e/src/io/sarl/m2e/wizards/importproject/EnableSarlMavenNatureAction.java create mode 100644 main/coreplugins/io.sarl.m2e/src/io/sarl/m2e/wizards/importproject/Messages.java create mode 100644 main/coreplugins/io.sarl.m2e/src/io/sarl/m2e/wizards/importproject/messages.properties diff --git a/main/coreplugins/io.sarl.m2e/OSGI-INF/l10n/bundle.properties b/main/coreplugins/io.sarl.m2e/OSGI-INF/l10n/bundle.properties index b598c3fa19..2a793446e5 100644 --- a/main/coreplugins/io.sarl.m2e/OSGI-INF/l10n/bundle.properties +++ b/main/coreplugins/io.sarl.m2e/OSGI-INF/l10n/bundle.properties @@ -5,4 +5,5 @@ wizard.name.newSarlMavenProject = SARL Maven Project wizard.description.newSarlMavenProject.short = Create a new SARL Maven project wizard.description.newSarlMavenProject.long = Create a new SARL Maven project in the SARL ide. wizard.name.importSarlMavenProject = Existing SARL Maven Project -wizard.description.importSarlMavenProject.short = Import an existing SARL Maven project \ No newline at end of file +wizard.description.importSarlMavenProject.short = Import an existing SARL Maven project +action.name.converttosarlmaven = Convert to SARL Maven Project \ No newline at end of file diff --git a/main/coreplugins/io.sarl.m2e/plugin.xml b/main/coreplugins/io.sarl.m2e/plugin.xml index 3f458b4257..2d90d71590 100644 --- a/main/coreplugins/io.sarl.m2e/plugin.xml +++ b/main/coreplugins/io.sarl.m2e/plugin.xml @@ -80,4 +80,26 @@ icon="platform:/plugin/org.eclipse.ui.intro.universal/themes/solstice/graphics/icons/ctool/new-project.png"> + + + + + + + + + + + + + + + diff --git a/main/coreplugins/io.sarl.m2e/src/io/sarl/m2e/wizards/importproject/EnableSarlMavenNatureAction.java b/main/coreplugins/io.sarl.m2e/src/io/sarl/m2e/wizards/importproject/EnableSarlMavenNatureAction.java new file mode 100644 index 0000000000..70f4ed9065 --- /dev/null +++ b/main/coreplugins/io.sarl.m2e/src/io/sarl/m2e/wizards/importproject/EnableSarlMavenNatureAction.java @@ -0,0 +1,206 @@ +/* + * $Id$ + * + * SARL is an general-purpose agent programming language. + * More details on http://www.sarl.io + * + * Copyright (C) 2014-2018 the original authors or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.sarl.m2e.wizards.importproject; + +import java.io.File; +import java.util.Iterator; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.IExecutableExtension; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.SubMonitor; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.m2e.core.internal.IMavenConstants; +import org.eclipse.ui.IObjectActionDelegate; +import org.eclipse.ui.IWorkbenchPart; + +import io.sarl.eclipse.natures.SARLProjectConfigurator; +import io.sarl.m2e.SARLMavenEclipsePlugin; + +/** + * Action for enabling a maven-based SARL project. + * + * @author $Author: sgalland$ + * @version $FullVersion$ + * @mavengroupid $GroupId$ + * @mavenartifactid $ArtifactId$ + * @since 0.9 + */ +public class EnableSarlMavenNatureAction implements IObjectActionDelegate, IExecutableExtension { + + /** Identifier of the action. + */ + public static final String ID = "io.sarl.m2e.enableSarlMavenNatureAction"; //$NON-NLS-1$ + + private static final String FILENAME_CLASSPATH = ".classpath"; //$NON-NLS-1$ + + private ISelection selection; + + /** Constructor. + */ + public EnableSarlMavenNatureAction() { + } + + /** Constructor. + * + * @param option the options of the action. + */ + public EnableSarlMavenNatureAction(String option) { + setInitializationData(null, null, option); + } + + @Override + public void setInitializationData(IConfigurationElement config, String propertyName, Object data) { + // + } + + @Override + public void selectionChanged(IAction action, ISelection selection) { + this.selection = selection; + } + + @Override + public void setActivePart(IAction action, IWorkbenchPart targetPart) { + // + } + + @Override + public void run(IAction action) { + if (this.selection instanceof IStructuredSelection) { + final IStructuredSelection structuredSelection = (IStructuredSelection) this.selection; + final Iterator it = structuredSelection.iterator(); + while (it.hasNext()) { + final Object element = it.next(); + IProject project = null; + if (element instanceof IProject) { + project = (IProject) element; + } else if (element instanceof IAdaptable) { + project = ((IAdaptable) element).getAdapter(IProject.class); + } + if (project != null) { + enableNature(project); + } + } + } + } + + /** Enable the SARL Maven nature. + * + * @param project the project. + */ + protected void enableNature(IProject project) { + final IFile pom = project.getFile(IMavenConstants.POM_FILE_NAME); + final Job job; + if (pom.exists()) { + job = createJobForMavenProject(project); + } else { + job = createJobForJavaProject(project); + } + if (job != null) { + job.schedule(); + } + } + + /** Create the configuration job for a Maven project. + * + * @param project the project to configure. + * @return the job. + */ + @SuppressWarnings("static-method") + protected Job createJobForMavenProject(IProject project) { + return new Job(Messages.EnableSarlMavenNatureAction_0) { + + @Override + protected IStatus run(IProgressMonitor monitor) { + final SubMonitor mon = SubMonitor.convert(monitor, 3); + try { + // The project should be a Maven project. + final IPath descriptionFilename = project.getFile(new Path(IProjectDescription.DESCRIPTION_FILE_NAME)).getLocation(); + final File projectDescriptionFile = descriptionFilename.toFile(); + final IPath classpathFilename = project.getFile(new Path(FILENAME_CLASSPATH)).getLocation(); + final File classpathFile = classpathFilename.toFile(); + // Project was open by the super class. Close it because Maven fails when a project already exists. + project.close(mon.newChild(1)); + // Delete the Eclipse project and classpath definitions because Maven fails when a project already exists. + project.delete(false, true, mon.newChild(1)); + if (projectDescriptionFile.exists()) { + projectDescriptionFile.delete(); + } + if (classpathFile.exists()) { + classpathFile.delete(); + } + // Import + MavenImportUtils.importMavenProject( + project.getWorkspace().getRoot(), + project.getName(), + true, + mon.newChild(1)); + } catch (CoreException exception) { + SARLMavenEclipsePlugin.getDefault().log(exception); + } + return Status.OK_STATUS; + } + }; + } + + /** Create the configuration job for a Java project. + * + * @param project the project to configure. + * @return the job. + */ + @SuppressWarnings("static-method") + protected Job createJobForJavaProject(IProject project) { + return new Job(Messages.EnableSarlMavenNatureAction_0) { + + @Override + protected IStatus run(IProgressMonitor monitor) { + final SubMonitor mon = SubMonitor.convert(monitor, 3); + // Force the project configuration to SARL. + SARLProjectConfigurator.configureSARLProject( + // Project to configure + project, + // Add SARL natures + true, + // Force java configuration + true, + // Create folders + true, + // Monitor + mon.newChild(3)); + return Status.OK_STATUS; + } + }; + } + +} diff --git a/main/coreplugins/io.sarl.m2e/src/io/sarl/m2e/wizards/importproject/Messages.java b/main/coreplugins/io.sarl.m2e/src/io/sarl/m2e/wizards/importproject/Messages.java new file mode 100644 index 0000000000..b3b1fde8cb --- /dev/null +++ b/main/coreplugins/io.sarl.m2e/src/io/sarl/m2e/wizards/importproject/Messages.java @@ -0,0 +1,46 @@ +/* + * $Id$ + * + * SARL is an general-purpose agent programming language. + * More details on http://www.sarl.io + * + * Copyright (C) 2014-2018 the original authors or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.sarl.m2e.wizards.importproject; + +import org.eclipse.osgi.util.NLS; + +/** Localized Messages. + * + * @author $Author: sgalland$ + * @version $FullVersion$ + * @mavengroupid $GroupId$ + * @mavenartifactid $ArtifactId$ + * @ExcludeFromApidoc + */ +@SuppressWarnings("all") +public class Messages extends NLS { + private static final String BUNDLE_NAME = Messages.class.getPackage().getName() + ".messages"; //$NON-NLS-1$ + static { + // initialize resource bundle + NLS.initializeMessages(BUNDLE_NAME, Messages.class); + } + + private Messages() { + } + + public static String EnableSarlMavenNatureAction_0; +} diff --git a/main/coreplugins/io.sarl.m2e/src/io/sarl/m2e/wizards/importproject/messages.properties b/main/coreplugins/io.sarl.m2e/src/io/sarl/m2e/wizards/importproject/messages.properties new file mode 100644 index 0000000000..ec52932cef --- /dev/null +++ b/main/coreplugins/io.sarl.m2e/src/io/sarl/m2e/wizards/importproject/messages.properties @@ -0,0 +1 @@ +EnableSarlMavenNatureAction_0=Converting to SARL Maven project