如果编辑器未打开,则停用菜单项

Deactivate menu item if editor is not open

我的 Eclipse IDE 中有一个菜单项,如果编辑器当前未打开,则需要 disable/non-visible。

plugin.xml 中,我可以使用 visibleWhen 代码来实现,但是我不知道如何检查编辑器当前是否打开。

This thread讲一个监听器来跟踪编辑器。但是,应该有一种更简单的方法来查找编辑器。例如 page.getEditorReferences()。但是我似乎无法将所有这些放在一起。

那么如何从 plugin.xml 中打开编辑器?

这是 Ant 编辑器使用的。

首先是org.eclipse.core.expressions.definitions定义:

<extension
       point="org.eclipse.core.expressions.definitions">
    <definition
          id="org.eclipse.ant.ui.activeAntEditor">
       <with
             variable="activeEditorId">
          <equals
                value="org.eclipse.ant.ui.internal.editor.AntEditor">
          </equals>
       </with>
    </definition>
</extension>

它定义了一个名为 org.eclipse.ant.ui.activeAntEditor 的表达式,它测试活动编辑器是否是 Ant 编辑器。

然后每个菜单 command 想要在编辑器处于活动状态时可见的条目使用:

<command
     commandId="org.eclipse.ant.ui.open.declaration.command"
     style="push">
   <visibleWhen
         checkEnabled="false">
     <reference
            definitionId="org.eclipse.ant.ui.activeAntEditor">
     </reference>
   </visibleWhen>
</command>

它引用 org.eclipse.ant.ui.activeAntEditor 表达式来进行可见性测试。