如何检查项目是否包含启动快捷方式的特定文件 [英] How to check if project contains specific file for launch shortcuts
问题描述
基本上,我想启用我的 LaunchShortcuts ,只有当项目具有特定性质并且包含特定文件时。项目的性质不是问题。但是,我找不到如何检查项目是否包含特定的文件。到目前为止,我在 plugin.xml
中有以下代码:
Basically, I want to enable my LaunchShortcuts only when project has specific nature and contains specific file. Project's nature is not a problem. However, I can't find out how to check if project contains specific file. By far I have the following code in the 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>
是否可以检查项目是否包含特定文件?
Is it possible to check if a project contains a specific file ?
UPDATED:
@ greg-449在下面的1条评论中给了我很好的提示,使用自定义的 org.eclipse.core。 expressions.propertyTesters 即可。所以,现在我的代码看起来像这样:
@greg-449 gave me a great hint in the 1 comment below about using custom org.eclipse.core.expressions.propertyTesters. So, now my code looks like this:
.
.
.
<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()从不调用。有任何想法吗?
But this approach doesn't seem to work. While debugging MyTester.test() is never called. Any ideas?
推荐答案
诀窍与使用 forcePluginActivation 相结合。这定义了应该激活定义属性测试程序的插件。如果未激活,则不能执行属性测试。
所以,代码的最终版本应该是smth。像这样:
The trick is coupled with using forcePluginActivation. This defines that the Plug-in defining the property tester should be activated. If it is not activated, the property test cannot be performed. So, the final version of the code should look smth. like this:
<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>
注意:引用文档,
forcePluginActivation - 一个标志,指示是否需要加载属性测试程序的插件。因此,应该谨慎使用该标志,以避免不必要的插件激活。大多数客户端应避免将此标志设置为true。如果用于评估此表达式的评估上下文允许插件激活,则此标志仅被授予。否则标志被忽略,并且不会插入插件。
这篇关于如何检查项目是否包含启动快捷方式的特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!