Filtering actions and property pages by project nature
This sample demonstrates how to add a custom project nature to a subproject. The presence or absence of the project nature can then be used to control the visibility of menu items.
Sample scenario
To add the sample project nature to the subproject, right-click the subproject and select the API Sample Actions > Add Project Nature action.
Subproject nature definition
Subproject
natures in Developer for z/OS are
similar in concept to IProjectNature
in base Eclipse.
They provide a means to distinguish between remote subprojects based
on the function that is to be enabled for each subproject.
To
define a custom project nature for a remote subproject, you must create
a class that implements the ILogicalSubProjectNature
interface.
Thus the SampleNature
class contains the following
code:
public class SampleNature implements ILogicalSubProjectNature {
......
}
The ILogicalSubProjectNature
interface
is relatively simple. In SampleNature
, you add a
private field project
for the project property.
The complete source code can be found in SampleNature.java.
To
complete the definition of the project nature, declare the SampleNature
class
in the plug-in manifest as follows:
<extension
id=“samplenature”
name=“
point=“com.ibm.ftt.projects.core.natures”>
<nature class=“com.ibm.ftt.api.samples.natures.SampleNature”/>
</extension>
This snippet defines a project
nature with the ID com.ibm.ftt.api.samples.samplenature
,
which is obtained by appending the extension ID samplenature
to
the ID of the defining plug-in com.ibm.ftt.api.samples
.
Adding a project nature
The Add
Project Nature menu item is implemented by the AddSubProjectNatureAction
class.
Its run(IAction)
method adds the SampleNature
by
passing the ID of the nature to the addNatureId(String)
method
of ILogicalSubProject
.
try {
getSubProject().addNatureId("com.ibm.ftt.api.samples.samplenature");
} catch (CoreException e) {
// TODO: in production code exception should be handled for real
e.printStackTrace();
}
The complete source code can be found in AddSubProjectNatureAction.java.
The action is contributed by using standard Eclipse contribution mechanisms.
<objectContribution
adaptable=“false”
objectClass=“com.ibm.ftt.projects.core.logical.ILogicalSubProject”
id=“com.ibm.ftt.api.samples.addnature”>
<action
label=“
class=“com.ibm.ftt.api.samples.natures.AddSubProjectNatureAction”
id=“com.ibm.ftt.api.samples.addnatureaction”/>
</objectContribution>
Removing a project nature and contributing menu items filtered by project natures
The Remove
Project Nature menu item is implemented by the RemoveSubProjectNatureAction
class.
Its run(IAction)
method removes the SampleNature
by
passing the ID of the nature to the removeNatureId(String)
method
of ILogicalSubProject
.
try {
getSubProject().removeNatureId(“com.ibm.ftt.api.samples.samplenature”);
} catch (CoreException e) {
// TODO: in production code exception should be handled for real
e.printStackTrace();
}
The complete source code can be found at RemoveSubProjectAction.java.
The
action is contributed by using standard Eclipse contribution mechanisms,
similar to the Add Project Nature menu item,
with the additional use of action filters. This contribution causes
the Remove Project Nature action to be displayed
only if the SampleNature
is added to the project
in question.
<objectContribution
adaptable=“false”
objectClass=“com.ibm.ftt.projects.core.logical.ILogicalSubProject”
id=“com.ibm.ftt.api.samples.removenature”>
<filter
value=“com.ibm.ftt.api.samples.samplenature”
name=“projectNature”/>
<action
label=“
class=“com.ibm.ftt.api.samples.natures.RemoveSubProjectNatureAction”
id=“com.ibm.ftt.api.samples.removenatureaction”/>
</objectContribution>