VisibleWhen选择Eclipse项目或选择项目中的任何文件/文件夹

时间:2015-02-17 09:35:48

标签: eclipse eclipse-plugin eclipse-rcp

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

目前我对菜单贡献的可见性如下:

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

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

请给我一些指针来实现这一目标。

2 个答案:

答案 0 :(得分:1)

我已经使用属性测试器实现了它,类如下:

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>

在项目中选择文件/文件夹时,结果正常,菜单项将显示。

答案 1 :(得分:0)

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

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