Skip to content

Commit

Permalink
[examples] Create a Maven project when the example folder contains a …
Browse files Browse the repository at this point in the history
…pom file.

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Aug 22, 2018
1 parent fd21488 commit 28f946c
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ Bundle-Name: %Bundle-Name
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Require-Bundle: io.sarl.eclipse;bundle-version="0.8.0",
io.sarl.m2e;bundle-version="0.8.0",
org.eclipse.ui;bundle-version="3.109.100",
org.eclipse.core.runtime;bundle-version="3.14.0",
org.eclipse.emf.common.ui;bundle-version="2.13.0",
org.eclipse.ui.ide;bundle-version="3.14.0",
org.eclipse.debug.ui;bundle-version="3.13.0",
org.eclipse.core.resources;bundle-version="3.13.0"
Export-Package: io.sarl.examples.wizard
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* $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.examples.wizard;

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$
public static String SarlExampleInstallerWizard_0;
public static String SarlExampleInstallerWizard_1;
public static String SarlExampleInstallerWizard_2;
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
}

private Messages() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,29 @@

package io.sarl.examples.wizard;

import org.eclipse.core.runtime.CoreException;
import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.debug.internal.ui.SWTFactory;
import org.eclipse.emf.common.ui.wizard.ExampleInstallerWizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.intro.IIntroManager;
import org.eclipse.ui.intro.IIntroPart;
import org.eclipse.ui.wizards.datatransfer.ImportOperation;

import io.sarl.eclipse.natures.SARLProjectConfigurator;
import io.sarl.m2e.utils.M2EUtilities;

/** Wizard for importing SARL samples.
*
Expand All @@ -43,19 +57,66 @@
*/
public class SarlExampleInstallerWizard extends ExampleInstallerWizard {

private ConfigurationPage configurationPage;

@Override
public void addPages() {
// Override the default "addPages" in order to create a configuration page.
super.addPages();
this.configurationPage = new ConfigurationPage("configurationPage", Messages.SarlExampleInstallerWizard_0); //$NON-NLS-1$
this.configurationPage.setDescription(Messages.SarlExampleInstallerWizard_1);
addPage(this.configurationPage);
}

@Override
public void dispose() {
this.configurationPage.dispose();
this.configurationPage = null;
super.dispose();
}

@Override
protected void createProject(ProjectDescriptor projectDescriptor, IProgressMonitor monitor) throws CoreException {
final SubMonitor mon = SubMonitor.convert(monitor, 2);
super.createProject(projectDescriptor, mon.newChild(1));
SARLProjectConfigurator.configureSARLProject(
// Project to configure
projectDescriptor.getProject(),
// Force java configuration
true,
// Create folders
true,
// Monitor
mon.newChild(1));
protected void installProject(ProjectDescriptor projectDescriptor, ImportOperation importOperation,
IProgressMonitor progressMonitor) throws Exception {
final SubMonitor mon = SubMonitor.convert(progressMonitor, 5);

// Standard creation
super.installProject(projectDescriptor, importOperation, progressMonitor);

final IProject project = projectDescriptor.getProject();

final IFile pomFile = project.getFile(Path.fromOSString("pom.xml")); //$NON-NLS-1$
if (this.configurationPage.isMavenNatureEnabled() && pomFile.exists()) {
// The project should be a Maven project.
final IPath descriptionFilename = project.getFile(new Path(IProjectDescription.DESCRIPTION_FILE_NAME)).getLocation();
final File projectDescriptionFile = descriptionFilename.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 definition because Maven fails when a project already exists.
project.delete(false, true, mon.newChild(1));
if (projectDescriptionFile.exists()) {
projectDescriptionFile.delete();
}
// Import
M2EUtilities.importMavenProject(
project.getWorkspace().getRoot(),
projectDescriptor.getName(),
true,
mon.newChild(1));
} else {
// 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));
}
mon.done();
}

Expand All @@ -66,6 +127,9 @@ public boolean performFinish() {
closeWelcomePage();
return true;
}
if (this.configurationPage != null && !this.configurationPage.getControl().isDisposed()) {
this.configurationPage.refresh();
}
return false;
}

Expand All @@ -81,4 +145,67 @@ protected static void closeWelcomePage() {
}
}

/** Configuration page.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.8
*/
public class ConfigurationPage extends WizardPage {

/** Indicates if the maven nature must be enabled by default.
*/
public static final boolean DEFAULT_MAVEN_NATURE = true;

private Button mavenProjectButton;

/** Constructor.
*
* @param pageName the name of the page.
* @param title the title.
*/
public ConfigurationPage(String pageName, String title) {
super(pageName, title, null);
}

@Override
public void createControl(Composite parent) {
final Composite composite = new Composite(parent, SWT.NONE);
final GridLayout layout = new GridLayout(2, false);
final int margin = -5;
final int spacing = 3;
layout.marginTop = margin;
layout.marginLeft = margin;
layout.marginRight = margin;
layout.marginBottom = margin;
layout.horizontalSpacing = spacing;
layout.verticalSpacing = spacing;
composite.setLayout(layout);

this.mavenProjectButton = SWTFactory.createCheckButton(composite,
Messages.SarlExampleInstallerWizard_2, null, DEFAULT_MAVEN_NATURE, 2);

refresh();
setControl(composite);
}

/** Refresh the configuration page.
*/
public void refresh() {
setErrorMessage(null);
setPageComplete(true);
}

/** Replies if the maven nature must be enabled.
*
* @return {@code true} for enabling the maven nature.
*/
public boolean isMavenNatureEnabled() {
return this.mavenProjectButton.getSelection();
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SarlExampleInstallerWizard_0=Configuration
SarlExampleInstallerWizard_1=Configure the example's project.
SarlExampleInstallerWizard_2=Enable the Maven nature of the project if a file 'pom.xml' exists

0 comments on commit 28f946c

Please sign in to comment.