当处理程序存在时,使命令仅可见

时间:2015-05-05 12:57:58

标签: java eclipse-plugin

我知道我可以像这样看到命令:

     <command commandId="org.acme.command" style="push">
        <visibleWhen checkEnabled="false">
           <with variable="selection">
              <test property="someProperty"
                    value="value">
              </test>
           </with>
        </visibleWhen>
     </command>

但是,如果有一个能够处理它的处理程序,我怎么才能使命令可见? (默认行为是命令存在但已禁用。)

1 个答案:

答案 0 :(得分:1)

这感觉就像一个黑客,但是将这个属性测试程序挂钩到命令有助于:

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>