Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使命令仅在处理程序存在时可见_Java_Eclipse Plugin - Fatal编程技术网

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>

...