Peter Friese

Developer Advocate / Mobile Developer / Public Speaker

How to Filter Property Pages According to the Project Nature

Often, you need to hide property pages if a project doesn’t have a specific nature. Here’s how you can do that:

`

  <page adaptable="false"
        class="org.andromda.android.ui.ideas.AndroidProjectPropertyPage"
        id="org.andromda.android.ui.ideas.AndroidProjectPropertyPage"
        name="Android Project Layout"
        objectClass="org.eclipse.core.resources.IProject">
     <filter name="nature"
           value="org.andromda.android.core.androidnature"/>
  </page>

`

How does it work? In order to make this work, the class you register the property page for must implement IActionFilter. Projects do not implement IActionFilter, but there is a class WorkbenchProject that does so. This class is registered with the workbench adapter manager, so it will eventually be called (if no one else supplies a suitable adapter, that is) in order to find out whether the project has an attribute named nature whose value is org.andromda.android.core.androidnature. In order to resolve attributes, IActionFilter requires clients to implement the method public boolean testAttribute(Object target, String name, String value). Here is the implementation as supplied by WorkbenchProject:

`

public boolean testAttribute(Object target, String name, String value) {
    IProject proj = (IProject) target;
    if (name.equals(NATURE)) {
        try {
            return proj.isAccessible() && proj.hasNature(value);
        } catch (CoreException e) {
            return false;
        }
    } else if (name.equals(OPEN)) {
        value = value.toLowerCase();
        return (proj.isOpen() == value.equals("true"));//$NON-NLS-1$
    }
    return super.testAttribute(target, name, value);
}

`

As you can see, WorkbenchProject will check whether the underlying project has a certain nature.