简体   繁体   English

在 Eclipse 插件中启用/禁用菜单项

[英]Enable/disable menu item in Eclipse plugin

I've made a pop-up menu with one menu item, I want to enable it only when I do a right click on a tree item of a certain class type otherwise disable it.我制作了一个带有一个菜单项的弹出菜单,我只想在右键单击某个类类型的树项时启用它,否则禁用它。

How can I achieve this?我怎样才能做到这一点?

You can add a handler that uses activeWhen and associate it with that menu's command id.您可以添加一个使用activeWhen的处理程序并将其与该菜单的命令 ID 相关联。

Here is a handler that makes a command active only when the current selection is not empty, and the selection is an item that can be adapted to an object of type Widget :这是一个仅当当前选择不为空时才激活命令的处理程序,并且该选择是一个可以适应Widget类型对象的项目:

<extension point="org.eclipse.ui.handlers">
  <handler class="com.myproject.handlers.ExportWidgetHandler"
           commandId="com.myproject.commands.exportWidget">
     <activeWhen>
        <with variable="selection">
           <iterate ifEmpty="false" operator="and">
              <adapt type="com.myproject.objects.Widget"/>
           </iterate>
        </with>
     </activeWhen>
  </handler>
</extension>

We can enable or disable a menu item by overriding the isEnabled method of Handler class.我们可以通过覆盖 Handler 类的 isEnabled 方法来启用或禁用菜单项。 Below is the sample code:下面是示例代码:

@Override
public boolean isEnabled() {
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM