Skip to content

Commit

Permalink
[eclipse] Add contributions to the configure menus.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Sep 2, 2016
1 parent 1484d56 commit 2c6f6fc
Show file tree
Hide file tree
Showing 8 changed files with 434 additions and 9 deletions.
Expand Up @@ -48,7 +48,8 @@ the Eclipse Foundation http://www.eclipse.org, the Xtext library https://eclipse
the Guava Libraries https://code.google.com/p/guava-libraries, and \
the Apache Software Foundation https://www.apache.org.
wizard.name.newSarlProject = SARL Project
wizard.description.newSarlProject = Create a SARL project
wizard.description.newSarlProject.short = Create a new SARL project
wizard.description.newSarlProject.long = Create a new SARL project in the SARL ide.
wizard.name.newSarlScript = SARL File
wizard.description.newSarlScript = Create a SARL script
wizard.name.newAgent = SARL Agent
Expand All @@ -71,3 +72,5 @@ wizard.name.newAnnotation = SARL Annotation
wizard.description.newAnnotation = Create a SARL annotation
projectconfigurator.name = SARL Project
welcome.message = Welcome to the Eclipse IDE for SARL Developers
action.addSarlNature = Add SARL Nature
action.removeSarlNature = Remove SARL Nature
59 changes: 54 additions & 5 deletions eclipse-sarl/plugins/io.sarl.eclipse/plugin.xml
Expand Up @@ -15,7 +15,7 @@
finalPerspective="io.sarl.eclipse.perspective.devel"
preferredPerspectives="io.sarl.eclipse.perspective.devel,org.eclipse.jdt.ui.JavaPerspective,org.eclipse.jdt.ui.JavaBrowsingPerspective"
project="true">
<description>%wizard.description.newSarlProject"</description>
<description>%wizard.description.newSarlProject.short"</description>
</wizard>
<wizard
category="io.sarl.eclipse.category.wizards"
Expand Down Expand Up @@ -213,7 +213,7 @@
<keywordReference id="org.eclipse.jdt.ui.buildpath"/>
</page>
<page category="io.sarl.lang.SARL"
class="io.sarl.lang.ui.SARLExecutableExtensionFactory:org.eclipse.xtext.ui.validation.ValidatorPreferencePage"
class="io.sarl.eclipse.SARLEclipseExecutableExtensionFactory:org.eclipse.xtext.ui.validation.ValidatorPreferencePage"
id="io.sarl.eclipse.preferences.Validator"
name="%preference.errorWarning">
<keywordReference id="io.sarl.eclipse.keyword_SARL"/>
Expand All @@ -239,7 +239,7 @@
</page>
<page
category="io.sarl.lang.SARL"
class="io.sarl.lang.ui.SARLExecutableExtensionFactory:org.eclipse.xtext.ui.validation.ValidatorPreferencePage"
class="io.sarl.eclipse.SARLEclipseExecutableExtensionFactory:org.eclipse.xtext.ui.validation.ValidatorPreferencePage"
id="io.sarl.eclipse.properties.Validator"
name="%preference.errorWarning">
<keywordReference id="io.sarl.eclipse.keyword_SARL"/>
Expand Down Expand Up @@ -360,6 +360,55 @@
label="%projectconfigurator.name">
</projectConfigurator>
</extension>
<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="popup:org.eclipse.ui.projectConfigure">
<command commandId="io.sarl.eclipse.natures.addSarlNature"
label="%action.addSarlNature"
style="push">
<visibleWhen checkEnabled="false">
<with variable="selection">
<iterate ifEmpty="false" operator="or">
<adapt type="org.eclipse.core.resources.IProject">
<not>
<test property="org.eclipse.core.resources.projectNature"
value="io.sarl.eclipse.SARLProjectNature"/>
</not>
</adapt>
</iterate>
</with>
</visibleWhen>
</command>
<command commandId="io.sarl.eclipse.natures.removeSarlNature"
label="%action.removeSarlNature"
style="push">
<visibleWhen checkEnabled="false">
<with variable="selection">
<iterate ifEmpty="false" operator="or">
<adapt type="org.eclipse.core.resources.IProject">
<test property="org.eclipse.core.resources.projectNature"
value="io.sarl.eclipse.SARLProjectNature"/>
</adapt>
</iterate>
</with>
</visibleWhen>
</command>
</menuContribution>
</extension>
<extension point="org.eclipse.ui.commands">
<command id="io.sarl.eclipse.natures.addSarlNature"
name="%action.addSarlNature" />
<command id="io.sarl.eclipse.natures.removeSarlNature"
name="%action.removeSarlNature" />
</extension>
<extension point="org.eclipse.ui.handlers">
<handler
class="io.sarl.eclipse.SARLEclipseExecutableExtensionFactory:io.sarl.eclipse.natures.AddSarlNatureHandler"
commandId="io.sarl.eclipse.natures.addSarlNature" />
<handler
class="io.sarl.eclipse.SARLEclipseExecutableExtensionFactory:io.sarl.eclipse.natures.RemoveSarlNatureHandler"
commandId="io.sarl.eclipse.natures.removeSarlNature" />
</extension>
<!-- Welcome pages -->
<extension
point="org.eclipse.ui.intro">
<introProductBinding
Expand All @@ -378,8 +427,8 @@
point="org.eclipse.ui.intro.quicklinks">
<command
id="org.eclipse.ui.newWizard(newWizardId=io.sarl.eclipse.wizard.newSarlProject)"
description="Create a new SARL Eclipse project"
label="Create a new SARL project"
description="%wizard.description.newSarlProject.long"
label="%wizard.description.newSarlProject.short"
icon="platform:/plugin/org.eclipse.ui.intro.universal/themes/solstice/graphics/icons/ctool/new-project.png">
</command>
</extension>
Expand Down
@@ -0,0 +1,125 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2016 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.eclipse.natures;

import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.inject.Inject;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;

/**
* Action for converting the nature of a project to SARL.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
public class AddSarlNatureHandler extends AbstractHandler {

@Inject
private SARLProjectConfigurator configurator;

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
if (currentSelection instanceof IStructuredSelection) {
final IStructuredSelection structuredSelection = (IStructuredSelection) currentSelection;
final List<IProject> projects = new ArrayList<>();
final Iterator<?> iterator = structuredSelection.iterator();
while (iterator.hasNext()) {
final Object obj = iterator.next();
if (obj instanceof IJavaProject) {
projects.add(((IJavaProject) obj).getProject());
} else if (obj instanceof IProject) {
projects.add((IProject) obj);
}
}
final Shell activeShell = HandlerUtil.getActiveShell(event);
convertAllWithProgress(activeShell, projects);
}
return null;
}

private boolean convertAllWithProgress(Shell shell, List<IProject> projects) throws ExecutionException {
final IRunnableWithProgress op = new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.beginTask(Messages.RemoveSarlNatureHandler_0, projects.size());
final SubMonitor mon = SubMonitor.convert(monitor);
try {
for (final IProject project : projects) {
doConvert(project, mon.newChild(1));
}
} catch (ExecutionException e) {
throw new InvocationTargetException(e);
} finally {
monitor.done();
}
}
};
try {
new ProgressMonitorDialog(shell).run(true, true, op);
} catch (InvocationTargetException exception) {
throw new ExecutionException(
MessageFormat.format(Messages.RemoveSarlNatureHandler_1, exception.getLocalizedMessage()), exception);
} catch (InterruptedException exception) {
//interrupted by user
return true;
}
return false;
}

/** Convert the given project.
*
* @param project the project to convert..
* @param monitor the progress monitor.
* @throws ExecutionException if something going wrong.
*/
protected void doConvert(IProject project, IProgressMonitor monitor) throws ExecutionException {
monitor.setTaskName(MessageFormat.format(Messages.AddSarlNatureHandler_2, project.getName()));
final SubMonitor mon = SubMonitor.convert(monitor, 2);
if (this.configurator.canConfigure(project, Collections.emptySet(), mon.newChild(1))) {
this.configurator.configure(project, Collections.emptySet(), mon.newChild(1));
}
monitor.done();
}

}
@@ -0,0 +1,56 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2016 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.eclipse.natures;

import com.google.inject.ImplementedBy;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

/**
* Configurator for a SARL project.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
@ImplementedBy(SARLProjectConfigurator.class)
public interface IProjectUnconfigurator {

/** Replies if the project can be unconfigured.
*
* @param project the project.
* @param monitor the progress monitor.
* @return <code>true</code> if the project could be unconfigured.
*/
boolean canUnconfigure(IProject project, IProgressMonitor monitor);

/** Unconfigure the given project.
*
* @param project the project.
* @param monitor the progress monitor.
* @throws CoreException if the project cannot be unconfigured.
*/
void unconfigure(IProject project, IProgressMonitor monitor) throws CoreException;

}
Expand Up @@ -33,6 +33,10 @@
@SuppressWarnings("all")
public class Messages extends NLS {
private static final String BUNDLE_NAME = "io.sarl.eclipse.natures.messages"; //$NON-NLS-1$
public static String AddSarlNatureHandler_2;
public static String RemoveSarlNatureHandler_0;
public static String RemoveSarlNatureHandler_1;
public static String RemoveSarlNatureHandler_2;
public static String SARLProjectConfigurator_0;
static {
// initialize resource bundle
Expand Down

0 comments on commit 2c6f6fc

Please sign in to comment.