VisibleWhen 为选择的 Eclipse 项目或选择的项目中的任何 file/folder

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

我想在用户 select 具有项目性质 customnature 的特定项目时显示特定的菜单贡献。用户可以 select 项目中具有项目性质 customnature 的任何文件或文件夹,它也会显示菜单。

目前我有 visibleWhen 的菜单贡献如下:

        <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>

此配置仅在 selecting 项目文件夹时成功显示菜单。

请给我一些实现这一目标的指导。

只是测试适应 IResource 而不是 IProject:

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

我是用属性测试仪实现的,class如下:

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;
}

}

和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>

并使用它

<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>

在项目中选择 file/folder 结果有效,菜单项将显示。