使用方法 setToolTip() 更改后,操作上的键绑定工具提示消失

Keybinding tooltip on action disappears after changing in with method setToolTip()

我在 java class 中定义了一个操作,并在 plugins.xml 文件中对其进行了键绑定和绑定。当我第一次启动我的应用程序时,我在工具提示中看到了我的键绑定(它应该是这样的)但是在使用 setToolTip(String toolTip) 更改工具提示后,按钮绑定从工具栏操作按钮中消失,但键绑定仍然有效。我如何再次显示它,或者我如何为我的操作获取特定的键绑定,因为 getAccelerator() 方法 returns 0。 这是我的操作代码 class

public class SampleAction extends DiagramAction{


public static final String ID = "sampleFunction";
 private void initData() {
        setId(ID);
        setText(Messages.cmd_AddBranchAction_text);
        setToolTipText(Messages.cmd_AddBranchAction_desc);
    }
    @Override
      public void refresh() {
         super.refresh();
       //checks if button in toolbar is enabled
          boolean isEnabled = isEnabled();
        if(isEnabled) {
            setToolTipText("Do something");
        } else {
            setToolTipText("Node not selected, select a node");
         }
     }
  }

plugin.xml

中是这样定义的
 <key
        commandId="com.sample.sampleFunction"
        contextId="org.eclipse.ui.context.window"
        schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
        sequence="M1+A" />

您可以使用 IBindingService 获取命令的键绑定文本:

TriggerSequence activeBinding = bindingService.getBestActiveBindingFor("command id");
if (activeBinding != null && !activeBinding.isEmpty()) {
    String acceleratorText = activeBinding.format();
}

在视图或编辑器中,这将获得绑定服务:

IBindingService service = (IBindingService)getSite().getService(IBindingService.class);

您可以在其他地方使用

IBindingService bindingSvc = (IBindingService)PlatformUI.getWorkbench().getAdapter(IBindingService.class);