代码之家  ›  专栏  ›  技术社区  ›  John Doe

单击时更新按钮文本

  •  1
  • John Doe  · 技术社区  · 7 年前

    我创建了一个命令:

    <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>
    

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

    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 回复  |  直到 7 年前
        1
  •  1
  •   greg-449    7 年前

    你使用 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 )