Java 使命令仅在处理程序存在时可见
我知道我可以使命令如此可见:Java 使命令仅在处理程序存在时可见,java,eclipse-plugin,Java,Eclipse Plugin,我知道我可以使命令如此可见: <command commandId="org.acme.command" style="push"> <visibleWhen checkEnabled="false"> <with variable="selection"> <test property="someProperty" value="valu
<command commandId="org.acme.command" style="push">
<visibleWhen checkEnabled="false">
<with variable="selection">
<test property="someProperty"
value="value">
</test>
</with>
</visibleWhen>
</command>
但是,如何使命令仅在有处理程序能够处理它时才可见?(默认行为是命令存在,但被禁用。)这感觉像是黑客攻击,但将此属性测试仪挂接到命令有助于:
public class HandlerEnabledTester extends PropertyTester {
private static final String PROPERTY_HANDLER_ENABLED = "handlerEnabled";
@Override
public boolean test(final Object receiver, final String property, final Object[] args, final Object expectedValue) {
if (PROPERTY_HANDLER_ENABLED.equals(property)) {
return isHandlerEnabled((String) expectedValue);
}
return false;
}
private static boolean isHandlerEnabled(String commandId) {
ICommandService commandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
Command command = commandService.getCommand(commandId);
return command.isEnabled();
}
}
aa和:
<extension point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="org.acme.HandlerEnabledTester"
id="org.acme.HandlerEnabledTester"
namespace="scope"
properties="handlerEnabled"
type="org.eclipse.jface.viewers.ISelection">
</propertyTester>
</extension>
...
<command commandId="org.acme.command" style="push">
<visibleWhen checkEnabled="false">
<with variable="selection">
<test property="scope.handlerEnabled"
value="org.acme.command">
</test>
</with>
</visibleWhen>
</command>
...