Eclipse Plug-in Development:Working with Project Facet

Every project may have one or more natures. Each nature brings set of properties to the project.For example a Java project has “src” directory and “bin” as build directory or you may want to associate a set of libraries to your custom project.

Faceted Project Framework provides a powerful mechanism for extending the capabilities of the Web Tools Platform. Project facets are typically used as a way of adding functionality to a project. When a facet is added to the project it can perform any necessary setup actions such as copying resources, installing builders, adding natures, etc. Facets can also be used as markers for enabling user interface elements.

This post explains defining a facet and using it.

Prerequisites :

  • Install WST eclipse plug-in
  • Add dependencies to MANIFEST.MF
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Sample
Bundle-SymbolicName: com.facet.sample;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: com.facet.sample.Activator
Bundle-Vendor: FACET
Require-Bundle: org.eclipse.ui,
 org.eclipse.core.runtime,
 org.eclipse.wst.common.project.facet.core;bundle-version="1.4.100",
 org.eclipse.jst.common.project.facet.ui;bundle-version="1.4.100",
 org.eclipse.core.resources;bundle-version="3.6.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy

1. Extend extension-point “org.eclipse.wst.common.project.facet.core.facets”, provide name description, version , etc . Plug-in.xml will look like below



<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="org.eclipse.wst.common.project.facet.core.facets">
      <category id="com.facet.sample.category1">
         <description> Sample Facet Desciption </description>
      </category>
      <project-facet id="my.facet">
         <label> My Facet </label>
         <description> My Facet Description </description>
      </project-facet>
      <project-facet-version facet="my.facet" version="1.0"/>
   </extension>
</plugin>

After above modification in plug-in.xml , “Project Facets” page in “Facet Project” wizard will shows newly declared facet in available facet list


image


Here we have just declared facet only, we need to provide action delegate as well as configurator .



<action
           facet="my.facet"
           id="my.facet.action.INSTALL"
           type="install"
           version="1.0">
        <delegate
              class="com.facet.sample.MyFacetInstallDegegate">
        </delegate>
     </action>

Now we have declared “Install” action and corresponding action delegate. MyFacetInstallDelegate has to implement org.eclipse.wst.common.project.facet.core.IDelegate and provide implementation for execute() method. Whenever “MyFacet” is selected to install, MyFacetInstallDelegate.execute() gets call back. I am creating two directories “src” & “bin” under project.



import java.io.File;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.wst.common.project.facet.core.IDelegate;
import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;
 
public class MyFacetInstallDegegate implements IDelegate {
 
    @Override
    public void execute(IProject project, IProjectFacetVersion fv,
            Object config, IProgressMonitor monitor) throws CoreException {
        IPath projectPath = project.getLocation();
        //creating src & bin folder structure 
        createFolderStructure(projectPath);
    }
 
    private void createFolderStructure(IPath path){
     //....
    }
}


image



In this post, I have explaining creating facet for project and taken appropriate action when the facet is being selected.

Comments

Popular posts from this blog

State Design Pattern by Example

Eclipse command framework core expression: Property tester

Composite Design Pattern by example