Skip to content

Commit

Permalink
[eclipse] Add the action "Convert to SARL Maven Project".
Browse files Browse the repository at this point in the history
This action enables to create a Maven-based project with SARL nature.

close #890

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Nov 29, 2018
1 parent 763a79a commit 8e7448a
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 1 deletion.
3 changes: 2 additions & 1 deletion main/coreplugins/io.sarl.m2e/OSGI-INF/l10n/bundle.properties
Expand Up @@ -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
wizard.description.importSarlMavenProject.short = Import an existing SARL Maven project
action.name.converttosarlmaven = Convert to SARL Maven Project
22 changes: 22 additions & 0 deletions main/coreplugins/io.sarl.m2e/plugin.xml
Expand Up @@ -80,4 +80,26 @@
icon="platform:/plugin/org.eclipse.ui.intro.universal/themes/solstice/graphics/icons/ctool/new-project.png">
</command>
</extension>

<extension point="org.eclipse.ui.popupMenus">
<objectContribution id="io.sarl.m2e.enableSarlMavenNatureAction"
objectClass="org.eclipse.core.resources.IProject"
adaptable="true">
<action id="io.sarl.m2e.enableSarlMavenNatureAction"
class="io.sarl.m2e.wizards.importproject.EnableSarlMavenNatureAction"
label="%action.name.converttosarlmaven"
style="push"
menubarPath="org.eclipse.ui.projectConfigure/additions"
enablesFor="+"/>
<visibility>
<and>
<objectState name="open" value="true"/>
<not>
<objectState name="nature" value="io.sarl.eclipse.natures.SARLProjectNature"/>
</not>
</and>
</visibility>
</objectContribution>
</extension>

</plugin>
@@ -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;
}
};
}

}
@@ -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;
}
@@ -0,0 +1 @@
EnableSarlMavenNatureAction_0=Converting to SARL Maven project

0 comments on commit 8e7448a

Please sign in to comment.