Jump to: navigation, search

Pave/Tutorials/Create Sequence Pattern

In this tutorial you will learn how to create a complex pattern. The new pattern that will be created extends the one created in Pave/Tutorials/Create Simple Pattern


Create a new Interface called NewPackageProperties to keep the property names

For more detailed information on how to create this Inteface go to Pave/Tutorials/Create Simple Pattern

package com.sap.ide.pattern.simple;

public interface NewPackageProperties {
	public static final String PROJECT_NAME = "NewPackageProperties.PROJECT_NAME";
	public static final String PACKAGE_NAME = "NewPackageProperties.PACKAGE_NAME";
}


Create a new DataModelProvider called NewPackageDataModelProvider

For more detailed information on how to create this Class go to Pave/Tutorials/Create Simple Pattern

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 NewPackageDataModelProvider extends AbstractDataModelProvider implements NewPackageProperties{
	@Override
	public IDataModelOperation getDefaultOperation() {
		return new NewPackageOperation(getDataModel());
	}

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

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

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


Create a new DataModelOperation called NewPackageOperation

For more detailed information on how to create this Class go to Pave/Tutorials/Create Simple Pattern

package com.sap.ide.pattern.simple;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFolder;
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 NewPackageOperation extends AbstractDataModelOperation {

	public NewPackageOperation(IDataModel model) {
        super(model);
    }

    @Override
    public IStatus execute(IProgressMonitor monitor, IAdaptable info)
            throws ExecutionException {
        String packageName = getDataModel().getStringProperty(NewPackageProperties.PACKAGE_NAME);
        String projectName = getDataModel().getStringProperty(NewPackageProperties.PROJECT_NAME);
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
        if (!project.isAccessible()){
            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Project is not accessible.");
        }
        try {
        	NullProgressMonitor nullProgressMonitor = new NullProgressMonitor();
        	IFolder folder = project.getFolder(packageName);
        	folder.create(true, true, nullProgressMonitor);
        } catch (CoreException e) {
            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to create package. " + e.getLocalizedMessage());
        }
        return Status.OK_STATUS;
    }

}


Create a new Synchronizer

Both NewPackageDataModelProvider and SimplePatternDataModelProvider(created in Pave/Tutorials/Create Simple Pattern) work with a property PROJECT_NAME.

To make sure identical information is stored in both dataModel properties a synchronizer is needed.

Create a new class com.sap.ide.pattern.simple.SimpleSynchronizer that extends com.sap.ide.pattern.application.framework.AbstractModelSynchronizer.

File:TUTORIAL3 1.gif

In the recently created class override the getMapTable method. To the map returned from its superclass, add the names of fields that should be synchronized.

Have in mind that synchronization is done only from the first to the second parameter.

package com.sap.ide.pattern.simple;

import java.util.Map;

import com.sap.ide.pattern.application.framework.AbstractModelSynchronizer;

public class SimpleSynchronizer extends AbstractModelSynchronizer {
	@Override
	public Map<String, String> getMapTable() {
		Map<String, String> hm = super.getMapTable();
		hm.put(SimpleProperties.PROJECT_NAME, NewPackageProperties.PROJECT_NAME);
		return hm;
	}
}

Register the new pattern in the plugin.xml file

Create an extension to extension point com.sap.ide.pattern.application.framework.Pattern.

Add a child node extends.

Set its property ExtendsPattern to SimpleProjectCreation.

File:TUTORIAL3 2.gif

Set synchronizer property to com.sap.ide.pattern.simple.SimpleSynchronizer to register previously created synchronizer.

File:TUTORIAL3 3.gif


Working without a synchronizer

It is possible to avoid using a synchronizer class in this case. Registered dataModel operations are executed with the root model as a parameter.

The root model nests all other registered through com.sap.ide.pattern.application.framework.Pattern extension point dataModels.

This means that properties from SimplePatternDataModelProvider are visible in NewPackageOperation.

So if it is possible not to include the Template:PROJECT NAMEproperty in NewPackageDataModelProvider at all, but to keep working with the one from SimplePatternDataModelProvider.

In this case NewPackageOperation looks like this:

package com.sap.ide.pattern.simple;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFolder;
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 NewPackageOperation extends AbstractDataModelOperation {

	public NewPackageOperation(IDataModel model) {
        super(model);
    }

    @Override
    public IStatus execute(IProgressMonitor monitor, IAdaptable info)
            throws ExecutionException {
        String packageName = getDataModel().getStringProperty(NewPackageProperties.PACKAGE_NAME);
        String projectName = getDataModel().getStringProperty(SimpleProperties.PROJECT_NAME);
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
        if (!project.isAccessible()){
            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Project is not accessible.");
        }
        try {
        	NullProgressMonitor nullProgressMonitor = new NullProgressMonitor();
        	IFolder folder = project.getFolder(packageName);
        	folder.create(true, true, nullProgressMonitor);
        } catch (CoreException e) {
            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to create package. " + e.getLocalizedMessage());
        }
        return Status.OK_STATUS;
    }

}