Jump to: navigation, search

Pave/Tutorials/Create Simple Pattern

Overview

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:

Id
the id has to be unique for the pattern.


Name
which will be shown in the default wizard.


Description
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.


Enablement
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 com.sap.ide.pattern.simple.

Select New > Project > Plug-in Project

TUTORIAL1 1.gif

Enter a name for the project: com.sap.ide.pattern.simple

TUTORIAL2.gif

Complete the wizard.

TUTORIAL1 2.gif

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.

TUTORIAL1 3.gif

org.eclipse.ui,
org.eclipse.core.runtime,
com.sap.ide.pattern.application.framework,
org.eclipse.jst.common.frameworks,
org.eclipse.wst.common.frameworks,
org.eclipse.core.resources,
org.eclipse.core.expressions,
org.junit

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

TUTORIAL1 4.gif

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

TUTORIAL1 5.gif

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.

TUTORIAL1 6.gif

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

TUTORIAL1 7.gif

Now make the SimplePatternDataModelProvider implement the SimpleProperies.

TUTORIAL1 8.gif

To add the property into getPropertyNames

implement getDefaultProperty, so that it returns simpleProject value.

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

 package com.sap.ide.pattern.simple;

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 {

	@Override
	public IDataModelOperation getDefaultOperation() {
		return new SimpleOperation(getDataModel());
	}

	@Override
	public Set getPropertyNames() {
		Set propertyNames = super.getPropertyNames();
		propertyNames.add(PROJECT_NAME);
		return propertyNames;
	}

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

	@Override
	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

TUTORIAL1 9.gif

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.

package com.sap.ide.pattern.simple;

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) {
        super(model);
    }

    @Override
    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 Manifest.mf 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