使命令仅在存在处理程序时可见
Make Command Only Visible When a Handler is Present
我知道我可以让命令像那样可见:
<command commandId="org.acme.command" style="push">
<visibleWhen checkEnabled="false">
<with variable="selection">
<test property="someProperty"
value="value">
</test>
</with>
</visibleWhen>
</command>
但是,如何才能使命令仅在有能够处理它的处理程序时才可见? (默认行为是命令存在,但被禁用。)
这感觉像是一个 hack,但是将此 属性 测试器连接到命令会有所帮助:
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();
}
}
Aaand:
<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>
我知道我可以让命令像那样可见:
<command commandId="org.acme.command" style="push">
<visibleWhen checkEnabled="false">
<with variable="selection">
<test property="someProperty"
value="value">
</test>
</with>
</visibleWhen>
</command>
但是,如何才能使命令仅在有能够处理它的处理程序时才可见? (默认行为是命令存在,但被禁用。)
这感觉像是一个 hack,但是将此 属性 测试器连接到命令会有所帮助:
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();
}
}
Aaand:
<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>