Skip to content

Commit

Permalink
Apply language-specific customizations when creating a Workflow project
Browse files Browse the repository at this point in the history
The NewWorkflowProjectWizard allows the user to select a set of ScriptingLanguage.
Once the project is created, all the Customization defined for these languages are
applied on the project.
  • Loading branch information
echebbi committed Mar 5, 2019
1 parent c264958 commit 21fa776
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
******************************************************************************/
package fr.kazejiyu.ekumi.ide.ui;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

Expand All @@ -18,17 +20,11 @@
public class Activator extends AbstractUIPlugin {

// The plug-in ID
public static final String PLUGIN_ID = "fr.kazejiyu.ekumi.ide.ui"; //$NON-NLS-1$
public static final String ID = "fr.kazejiyu.ekumi.ide.ui"; //$NON-NLS-1$

// The shared instance
private static Activator plugin;

/**
* The constructor
*/
public Activator() {
}

@Override
public void start(BundleContext context) throws Exception {
super.start(context);
Expand All @@ -49,5 +45,59 @@ public void stop(BundleContext context) throws Exception {
public static Activator getDefault() {
return plugin;
}

/**
* Logs a message for info or debugging purposes.
*
* @param message
* The message to log.
*/
public static void debug(String message) {
getDefault().getLog().log(new Status(IStatus.INFO, ID, message));
}

/**
* Logs a message for warning the user.
*
* @param message
* The message to log.
*/
public static void warn(String message) {
getDefault().getLog().log(new Status(IStatus.WARNING, ID, message));
}

/**
* Logs a message for warning the user.
*
* @param t
* The throwable that causes the warning.
* @param message
* The message to log.
*/
public static void warn(Throwable t, String message) {
getDefault().getLog().log(new Status(IStatus.WARNING, ID, message, t));
}

/**
* Logs an error.
*
* @param message
* The error message.
*/
public static void error(String message) {
getDefault().getLog().log(new Status(IStatus.ERROR, ID, message));
}

/**
* Logs an Exception.
*
* @param e
* The exception to log.
* @param message
* The error message.
*/
public static void error(Exception e, String message) {
getDefault().getLog().log(new Status(IStatus.ERROR, ID, message, e));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
Expand All @@ -25,6 +26,7 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
Expand All @@ -39,6 +41,7 @@

import fr.kazejiyu.ekumi.ide.nature.WorkflowProject;
import fr.kazejiyu.ekumi.ide.nature.WorkflowProjectNature;
import fr.kazejiyu.ekumi.ide.project.customization.Customization;
import fr.kazejiyu.ekumi.model.spec.Activity;
import fr.kazejiyu.ekumi.model.spec.SpecFactory;
import fr.kazejiyu.ekumi.workflow.editor.design.api.EKumiViewpoints;
Expand Down Expand Up @@ -71,18 +74,17 @@ public WorkspaceWorkflowProject(IWorkspace workspace, ModelingProjectManager mod
}

@Override
public IProject create(String name, IPath path, String activityName, IProgressMonitor monitor) throws CoreException {
public IProject create(String name, IPath path, String activityName, Set<Customization> customizations, IProgressMonitor monitor) throws CoreException {
IWorkspaceRunnable createProject = projectMonitor -> {
try {
IProject project = createProject(name, path, projectMonitor);
SubMonitor subMonitor = SubMonitor.convert(monitor, 3 + customizations.size());

IProject project = createProject(name, path, subMonitor.split(1));
Activity workflow = createProjectModel(activityName, project, subMonitor.split(1));
createRepresentation(workflow, project, subMonitor.split(1));

Activity workflow = createProjectModel(activityName, project);

createRepresentation(workflow, project, projectMonitor);
}
finally {
monitor.done();
}
for (Customization customization : customizations) {
customization.customize(workspace, project, subMonitor.split(1));
}
};
workspace.run(createProject, monitor);
return workspace.getRoot().getProject(name);
Expand All @@ -103,6 +105,9 @@ public IProject create(String name, IPath path, String activityName, IProgressMo
* @throws CoreException if an error occurs while creating the project
*/
private IProject createProject(String name, IPath path, IProgressMonitor monitor) throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor);
subMonitor.setTaskName("Creating the project");

// First add the Modeling nature to the project
IProject project = modeling.createNewModelingProject(name, path, true, monitor);

Expand All @@ -129,12 +134,17 @@ private IProject createProject(String name, IPath path, IProgressMonitor monitor
* The name of the model to create.
* @param project
* The project in which the model is persisted.
* @param monitor
* The
*
* @return the new model
*
* @throws CoreException if the new model cannot be persisted
*/
private static Activity createProjectModel(String activityName, IProject project) throws CoreException {
private static Activity createProjectModel(String activityName, IProject project, IProgressMonitor monitor) throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor);
subMonitor.setTaskName("Creating the workflow model");

// Create the Activity representing the workflow
Activity workflow = SpecFactory.eINSTANCE.createActivity();
workflow.setId(project.getName() + "." + activityName);
Expand Down Expand Up @@ -169,6 +179,9 @@ private static Activity createProjectModel(String activityName, IProject project
* @throws CoreException
*/
private static void createRepresentation(Activity model, IProject project, IProgressMonitor monitor) throws CoreException {
SubMonitor subMonitor = SubMonitor.convert(monitor);
subMonitor.setTaskName("Creating the diagram");

// Create a new Sirius session to handle the representation
Session session = ModelingProject.asModelingProject(project).get().getSession();

Expand All @@ -177,7 +190,8 @@ private static void createRepresentation(Activity model, IProject project, IProg
.execute(new RecordingCommand(session.getTransactionalEditingDomain()) {
@Override
protected void doExecute() {
session.addSemanticResource(model.eResource().getURI(), monitor);
SubMonitor commandMonitor = SubMonitor.convert(subMonitor);
session.addSemanticResource(model.eResource().getURI(), commandMonitor);
}
});

Expand All @@ -190,7 +204,8 @@ protected void doExecute() {
.execute(new RecordingCommand(session.getTransactionalEditingDomain()) {
@Override
protected void doExecute() {
session.createView(workflowViewpoint, asList(model), true, monitor);
SubMonitor commandMonitor = SubMonitor.convert(subMonitor);
session.createView(workflowViewpoint, asList(model), true, commandMonitor);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
******************************************************************************/
package fr.kazejiyu.ekumi.ide.ui.wizards;

import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toSet;

import java.util.Set;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
Expand All @@ -19,18 +27,35 @@
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import fr.kazejiyu.ekumi.model.scripting.ScriptingLanguage;
import fr.kazejiyu.ekumi.model.spec.Activity;

/**
* Wizard page used to enter the information required for the creation of a new {@link Activity}.
*/
public class NewActivityPage extends WizardPage {

/** Text box to type the name of the activity to create */
private Text activityName;

/** The languages that can be selected by the user */
private final Set<ScriptingLanguage> availableLanguages;

public NewActivityPage() {
/** Displays the list of available languages */
private CheckboxTableViewer languagesViewer;

/**
* Creates a new page allowing the user to type a name and select
* the languages that will be used to implement activities' behavior.
*
* @param availableLanguages
* The languages that can be selected by the user.
*/
public NewActivityPage(Set<ScriptingLanguage> availableLanguages) {
super("New Workflow");
setTitle("New Workflow");
setDescription("Set up project's workflow");
this.availableLanguages = availableLanguages;
}

@Override
Expand All @@ -39,10 +64,50 @@ public void createControl(Composite parent) {
GridLayout layout = new GridLayout();
container.setLayout(layout);
layout.numColumns = 2;
Label activityNameLabel = new Label(container, SWT.NONE);

createWorkflowNameTextField(container);
createEmptySeparator(container, layout.numColumns);
createLanguagesPicker(container, layout.numColumns);

// required to avoid an error in the system
setControl(container);
setPageComplete(false);
}

private void createLanguagesPicker(Composite parent, int numColumns) {
Label languagesLabel = new Label(parent, SWT.NONE);
languagesLabel.setText("Which languages will be used?");
GridData languagesLabelLayout = new GridData(GridData.FILL_HORIZONTAL);
languagesLabelLayout.horizontalSpan = numColumns;
languagesLabel.setLayoutData(languagesLabelLayout);

languagesViewer = CheckboxTableViewer.newCheckList(parent, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
languagesViewer.setLabelProvider(new LabelProvider() {
@Override
public String getText(Object element) {
return ((ScriptingLanguage) element).name();
}
});
languagesViewer.setContentProvider(new ArrayContentProvider());
languagesViewer.setInput(availableLanguages);
GridData languagesViewerLayout = new GridData(GridData.FILL_HORIZONTAL);
languagesViewerLayout.horizontalSpan = 2;
languagesViewerLayout.horizontalAlignment = GridData.CENTER;
languagesViewer.getTable().setLayoutData(languagesViewerLayout);
}

private static void createEmptySeparator(Composite parent, int numColumns) {
Label emptySeparator = new Label(parent, SWT.HORIZONTAL);
GridData separatorLayout = new GridData(GridData.FILL_HORIZONTAL);
separatorLayout.horizontalSpan = numColumns;
emptySeparator.setLayoutData(separatorLayout);
}

private void createWorkflowNameTextField(Composite parent) {
Label activityNameLabel = new Label(parent, SWT.NONE);
activityNameLabel.setText("Workflow name:");

activityName = new Text(container, SWT.BORDER | SWT.SINGLE);
activityName = new Text(parent, SWT.BORDER | SWT.SINGLE);
activityName.setText("");
activityName.addKeyListener(new KeyAdapter() {
@Override
Expand All @@ -53,13 +118,7 @@ public void keyReleased(KeyEvent e) {
});
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
activityName.setLayoutData(gd);

// required to avoid an error in the system
setControl(container);
setPageComplete(false);

activityName.setFocus();
}
}

@Override
public void setVisible(boolean visible) {
Expand All @@ -86,4 +145,14 @@ public String getActivityName() {
public void setActivityName(String name) {
activityName.setText(name);
}

/**
* Returns all the languages that have been selected.
* @return all the languages that have been selected
*/
public Set<ScriptingLanguage> getSelectedLanguages() {
return stream(languagesViewer.getCheckedElements())
.map(ScriptingLanguage.class::cast)
.collect(toSet());
}
}

0 comments on commit 21fa776

Please sign in to comment.