Skip to main content
Jump to: navigation, search

Pave/Tutorials/Create Simple Pattern


This tutorial explains how you can create step by step a simple Pattern that will generate a project resource in your workspace. First, you have to define a pattern. To do that you have to create an extension point containing the following information:

the id has to be unique for the pattern.

which will be shown in the default wizard.

this is a long description of what the pattern does; it is aimed to inform the end
user of what operations will be executed when the pattern is applied.

which will show if the action can be executed in the current context.

Pattern generator class
this class will actually manage the model and it will return the generator itself.

Create Plug-in project

You need a new plug-in project, for example

Select New > Project > Plug-in Project


Enter a name for the project:


Complete the wizard.


The next thing you have to do is to setup the plug-in dependencies.

These are the dependencies that you need during the entire tutorial, so you do not have to edit them again.



Having prepared your environment, you can start the pattern implementation.

Create DataModelProvider

Next thing you have to do is to generate the main operation files of the pattern.

These are the DataModelProvider and DataModelOperation.

Let's start with DataModelProvider.

You need to create new class called: SimplePatternDataModelProvider, which will extend AbstractDataModelProvider


Next you have to override some methods from the abstract class.


You have to override:

   * getDefaultOperation() - this is generator, whose execute method will be called when finishing.
   * getDefaultProperty(propertyName) - this is a method, which will return default values for the metadata stored in the model. This is particularly useful when a pattern can be executed entirely with default values, without user interaction.
   * getPropertyNames(propertyName) - this method shows the framework, which properties its model will use. These properties will be passed to the validate method and getDefaultProperty method will be called on them.
   * validate(propertyName) - validation method, where you need to validate the properties that you have specified in the getPropertyNames method.

Next, you have to specify the properties. It is recommended that you create a separate interface for them and let the SimplePatternDataModelProvider to implement this interface.

Create SimpleProperties interface and place one property in it.


It is recommended that the properties' values are formed with the <ClassName>.<PropertyName>. This is done for uniqueness when more complex operations are done.


Now make the SimplePatternDataModelProvider implement the SimpleProperies.


To add the property into getPropertyNames

implement getDefaultProperty, so that it returns simpleProject value.

Implement validation, so that the property has a valid name.


import java.util.Set;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelProvider;
import org.eclipse.wst.common.frameworks.datamodel.IDataModelOperation;

public class SimplePatternDataModelProvider extends AbstractDataModelProvider implements SimpleProperties {

	public IDataModelOperation getDefaultOperation() {
		return new SimpleOperation(getDataModel());

	public Set getPropertyNames() {
		Set propertyNames = super.getPropertyNames();
		return propertyNames;

	public Object getDefaultProperty(String propertyName) {
		if (PROJECT_NAME.equals(propertyName)){
			return "simpleProject";
		return super.getDefaultProperty(propertyName);

	public IStatus validate(String name) {
		if (PROJECT_NAME.equals(name)){
			return ResourcesPlugin.getWorkspace().validateName(
					getDataModel().getStringProperty(name), IResource.PROJECT);
		return super.validate(name);


Create Operation

You have noted that you implemented the getDefaultOperation as well. Let's create the SimpleOperation class that extends AbstractDataModelOperation class


You get this class file:

TUTORIAL1 10.gif

and implement it:

You have to get the name of the project from the model and create it.


import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelOperation;
import org.eclipse.wst.common.frameworks.datamodel.IDataModel;

public class SimpleOperation extends AbstractDataModelOperation {
    public SimpleOperation(IDataModel model) {

    public IStatus execute(IProgressMonitor monitor, IAdaptable info)
            throws ExecutionException {
        String projectName = getDataModel().getStringProperty(SimpleProperties.PROJECT_NAME);
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
        if (project.exists()){
            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Project already exists.");
        try {
            project.create(new NullProgressMonitor());
        } catch (CoreException e) {
            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to create project. " + e.getLocalizedMessage());
        return Status.OK_STATUS;


Register Pattern in plugin.xml

The last thing to do is to define the pattern extension point and test it.

Open the file select extensions and choose Add.

Select Pattern ID and the skeleton will be generated for you.

TUTORIAL1 11.gif

Now you need to set up the data.

You will add unique Name and Id for your pattern.

You have to define the generator: SimplePatternDataModelProvider

and define the applicability.

In enablement tag add <or> and then instanceof tag: insert IJavaProject and IProject. So your pattern will be available in the context menu of the Java or other projects.

TUTORIAL1 12.gif

Test it!

You can test your pattern now. Launch runtime workbench select Patterns > Apply... in the context menu of the package/project explorer.

TUTORIAL1 13.gif

Back to the top