如何将弹出菜单的可见性仅限制为某个项目类型?

时间:2011-08-13 09:39:57

标签: eclipse-plugin contextmenu

我正在使用menuContributions + popup在我的plugin.xml中显示上下文菜单。我需要将其可见性仅限于

  1. 某种类型的项目(例如动态Web项目)(菜单应该只显示在父项目文件夹的右键)和
  2. 特定文件夹(例如Web内容)及其在项目文件夹结构中的子文件夹。
  3. 通过使用

    ,我能够在某种程度上达到1)条件
    <menuContribution locationURI="popup:common.new.menu?after=additions">
                <command
                    label="Web Wiz"
                    commandId="commandId"
                    icon="icons/sample.gif">
                   <visibleWhen>
                      <with  variable="selection">
                                <iterate ifEmpty="false"
                            operator="or">
                         <instanceof
                               value="org.eclipse.core.resources.IProject"/>
                      </iterate>
                      </with>
                   </visibleWhen>
                </command>
            </menuContribution>
    

    但它出现在所有类型的项目中...我需要将其限制为仅仅动态Web项目,那么我应该添加什么来满足plugin.xml中的这个要求?

3 个答案:

答案 0 :(得分:4)

  1. 添加将测试项目类型的propertyTester。
  2. 在visibleWhen
  3. 中使用该测试仪

    您可以在eclipse帮助或扩展帮助本身阅读有关属性测试人员的信息:)

    编辑 - 同样检查一下 - http://wiki.eclipse.org/Command_Core_Expressions#Property_Testers(尤其是ResourcePropertyTester,它可以为您提供可以使用的内置实现)

答案 1 :(得分:3)

<menuContribution locationURI="popup:common.new.menu?after=additions">
  <command
            label="Web Wiz"
            commandId="commandId"
            icon="icons/sample.gif">
    <visibleWhen>
      <with variable="selection">
        <iterate operator="and" ifEmpty="false">
           <test 
                  property="org.eclipse.core.resources.projectNature" 
                  value="your-project-nature" />
        </iterate>
     </with>
    </visibleWhen>
</command>
<menuContribution>

答案 2 :(得分:2)

对于第二个条件:

<test   forcePluginActivation="true"
            property="testWizard.propertyTester.checkFolder"
             value="org.eclipse.wst.jsdt.core.jsNature"
     </test> 

是属性测试器的引用,可以定义为


<extension
        point="org.eclipse.core.expressions.propertyTesters">
     <propertyTester          
        class="testwizard.wizards.MyPropTester"
           id="MyPropTesterFolder"
           namespace="testWizard.propertyTester"
           properties="checkFolder"
           type="org.eclipse.core.resources.IFolder">
     </propertyTester>

然后可以在

中测试文件夹及其子文件夹的类型
package testwizard.wizards;

import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.CoreException;

public class MyPropTester extends PropertyTester{

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

        IFolder folder=(IFolder)receiver;
        String folderPath=folder.getProjectRelativePath().toString();
        String arr[]=folderPath.split("/");     
        try {
            if(folder.getProject().hasNature(expectedValue.toString()))
            {
                if(arr[0].equals("XYZ"))
                {
                    return true;
                }
            }
        } catch (CoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return false;
    }

}