如何检查项目是否包含启动快捷方式的特定文件

时间:2014-12-29 11:30:35

标签: java eclipse eclipse-plugin

基本上,我希望仅在项目具有特定性质且包含特定文件时启用LaunchShortcuts。项目的性质不是问题。但是,我无法找到如何检查项目是否包含特定文件。到目前为止,我在plugin.xml

中有以下代码
<contextualLaunch>
    <enablement>
        <with variable="selection">
            <count value="1"/>
            <iterate>
                <adapt type="org.eclipse.core.resources.IResource">
                    <test property="org.eclipse.core.resources.projectNature" 
                        value="my.project.nature" />
                </adapt> 
            </iterate>
        </with>
    </enablement>
</contextualLaunch> 

是否可以检查项目是否包含特定文件?

更新:

@ greg-449在下面的1条关于使用自定义 org.eclipse.core.expressions.propertyTesters 的评论中给了我一个很好的提示。所以,现在我的代码看起来像这样:

.
. 
.
<extension point="org.eclipse.core.expressions.propertyTesters">
  <propertyTester
       id="my.tester"
       type="org.eclipse.core.resources.IResource"
       namespace="my.namespace"
       properties="myProperty"
       class="my.MyTester">
   </propertyTester>
</extension>
.
.
.
<contextualLaunch>
    <enablement>
        <with variable="selection">
            <count value="1"/>
            <iterate>
                <adapt type="org.eclipse.core.resources.IResource">
                     <and>
                        <test property="org.eclipse.core.resources.projectNature" 
                                value="my.project.nature" />
                        <test property="my.namespace.myProperty"  
                                value="true"/>
                     </and> 
                </adapt> 
            </iterate>
        </with>
    </enablement>
</contextualLaunch> 

以下是 MyTester 的代码:

public class MyTester extends PropertyTester {
private static final String PROPERTY_NAME = "myProperty";

@Override
public boolean test(Object receiver, String property, Object[] arg2, Object expectedValue) {
    if (property.equals(PROPERTY_NAME) && receiver instanceof IProject) {
        return FileUtil.containsSpecificFile((IProject) receiver);
    }
    return false;
}
}

但这种方法似乎不起作用。从不调用调试MyTester.test()。有任何想法吗?

1 个答案:

答案 0 :(得分:0)

这个技巧与使用 forcePluginActivation 相结合。这定义了应该激活定义属性测试器的插件。如果未激活,则无法执行属性测试。 因此,代码的最终版本看起来应该是smth。像这样:

 
<contextualLaunch>
    <enablement>
        <with variable="selection">
            <count value="1"/>
            <iterate>
                <adapt type="org.eclipse.core.resources.IResource">
                    <and>
                       <test property="org.eclipse.core.resources.projectNature" 
                            value="my.project.nature" />
                       <test property="my.namespace.myProperty"  
                            forcePluginActivation="true"/>
                   </and> 
              </adapt> 
           </iterate>
       </with>
    </enablement>
</contextualLaunch> 

注意:引用documentation

forcePluginActivation - a flag indicating whether the plug-in contributing the property tester should be loaded if necessary. As such, this flag should be used judiciously, in order to avoid unnecessary plug-in activations. Most clients should avoid setting this flag to true. This flag is only honored if the evaluation context used to evaluate this expression allows plug-in activation. Otherwise the flag is ignored and no plug-in loading takes place.