问题
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 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>
Here is the code of 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;
}
}
But this approach doesn't seem to work. While debugging MyTester.test() is never called. Any ideas?
回答1:
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>
NOTE: Quoting the 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.
来源:https://stackoverflow.com/questions/27688010/how-to-check-if-project-contains-specific-file-for-launch-shortcuts