单击时更新按钮文本

时间:2018-06-15 15:54:41

标签: eclipse-plugin

我创建了一个命令:

<command
      id="disableOrEnableCommand"
      name="Disable or Enable Command">
</command>

然后我在工具栏上添加了一个按钮:

<extension
      point="org.eclipse.ui.menus">
   <menuContribution
         allPopups="false"
         locationURI="toolbar:org.eclipse.ui.main.toolbar">
         <toolbar
            id="disableOrEnable">
            <command
               commandId="disableOrEnableCommand"
               label="Disable Me"
               style="push">
         </command>
      </toolbar>
   </menuContribution>
</extension>

下一步是将命令绑定到处理程序:

<extension
        point="org.eclipse.ui.handlers">
    <handler
        class="DisableOrEnableHandler"
        commandId="disableOrEnableCommand">
    </handler>
</extension>

我将处理程序配置为实现IHandlerIElementUpdater(因为我想更新按钮文本):

public class DisableOrEnableHandler implements IHandler, IElementUpdater{

    public boolean isEnabled = true;

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        isEnabled = !isEnabled;
        // Trigger somehow the updateElement() method
        return null;
    }

    @Override
    public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {
        if ( isEnabled) {
            element.setText("Disable me");
        } else {
            element.setText("Enable me");
        }
    }

    // other overriden methods from IHandler and IElementUpdater
}

我错过了一个难题,如何配置按钮以在按下按钮时触发updateElement?

1 个答案:

答案 0 :(得分:1)

您使用ICommandService.refreshElements调用来调用更新元素。在处理程序中,您可以使用以下内容:

IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);

ICommandService commandService = window.getService(ICommandService.class);

commandService.refreshElements(event.getCommand().getId(), null);

(从org.eclipse.ui.internal.handlers.PinEditorHandler抽象的代码)