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:
<extension point="org.eclipse.ui.propertyPages">
<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>
</extension>
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.
Thanks for reading this post. Follow me on twitter here to be notified about updates and other posts I write. Or, subscribe to my RSS feed here


Comments are closed.