Seng Zhe
Seng Zhe

Reputation: 623

VisibleWhen for Eclipse project selected or any file/folder in project selected

I want to show a particular menu contribution when user selects a particular project with project nature customnature . User can select any file or folder in the project with project nature customnature and it will show the menu also.

Currently I have the visibleWhen for the menu contribution as following:

        <visibleWhen
             checkEnabled="false">
          <with
                variable="activeMenuSelection">
                <iterate>
                    <adapt type="org.eclipse.core.resources.IProject">
                         <and>
                         <test
                         property="org.eclipse.core.resources.projectNature"
                         value="customnature">
                         </test>
                         </and>
                    </adapt>
             </iterate>
                <count
                      value="1">
                </count>
          </with>
       </visibleWhen>

This configuration successfully show menu when selecting project folder only.

Please give me some pointer to achieve this.

Upvotes: 0

Views: 1108

Answers (2)

Seng Zhe
Seng Zhe

Reputation: 623

I have achieve it using property tester, the class is as below:

public class ProjectNaturePropertyTester extends PropertyTester {

@Override
public boolean test(Object receiver, String property, Object[] args,
        Object expectedValue) {

    IResource rsc=(IResource)receiver;
    try {
        IProject project = rsc.getProject();
        if(project.hasNature(CustomNature.NATURE_ID))
           return true;

    } catch (CoreException e) {
        throw new RuntimeException("Problem getting nature from IResource" + e.getMessage() , e);
    }

    return false;
}

}

and plugin.xml

<extension
    point="org.eclipse.core.expressions.propertyTesters">
 <propertyTester
       class="org.example.ui.propertytester.ProjectNaturePropertyTester"
       id="ProjectNaturePropertyTester"
       namespace="org.example.ui.propertytester"
       properties="checkProjectNature"
       type="org.eclipse.core.resources.IResource">
 </propertyTester>

and using it

<visibleWhen
             checkEnabled="false">
          <with
                variable="activeMenuSelection">
                <iterate>
                 <adapt
                         type="org.eclipse.core.resources.IResource">
                      <test
                            property="org.example.ui.propertytester.checkProjectNature">
                      </test>
                   </adapt>
             </iterate>
                <count
                      value="1">
                </count>
          </with>
       </visibleWhen>

The result is working when selecting a file/folder in a project, the menu item will show up.

Upvotes: 1

greg-449
greg-449

Reputation: 111142

Just test for adapting to IResource instead of IProject:

<adapt
    type="org.eclipse.core.resources.IResource">
    <test
        property="org.eclipse.core.resources.projectNature"
        value="customnature">
    </test>
 </adapt>

Upvotes: 1

Related Questions