如何在 Eclipse 中弹出时检查编辑器是否处于活动状态?

How to check whether editor is active while popup in eclipse?

我正在处理一个项目,在该项目中,我需要为仅具有特定扩展名的文件以及来自编辑器 window 和项目资源管理器的弹出菜单显示一个弹出菜单。

以下是我制作的plugin.xml文件:

  <menuContribution
        locationURI="popup:org.eclipse.ui.popup.any">
     <command
           commandId="abc.contextMenu"
           label="Open"
           mnemonic="P"
           style="push">
        <visibleWhen
              checkEnabled="false">
           <or>
              <with
                    variable="selection">
                 <iterate
                       ifEmpty="false">
                    <reference
                          definitionId="com.varun.ease.ui.sign.definition.testType">
                    </reference>
                 </iterate>
              </with>
              <with
                    variable="activeEditorInput">
                 <and>
                    <test
                          property="org.eclipse.ui.IWorkbenchPart"
                          value="true">
                    </test>
                    <reference
                          definitionId="com.varun.ease.ui.sign.definition.testType">
                    </reference>
                 </and>
              </with>
           </or>
        </visibleWhen>
     </command>
  </menuContribution>

<extension
     point="org.eclipse.core.expressions.definitions">
  <definition
        id="com.varun.ease.ui.sign.definition.testType">
     <adapt
           type="org.eclipse.core.resources.IFile">
        <or>
           <test
                 property="org.eclipse.core.resources.name"
                 value="*.py">
           </test>
           <test
                 property="org.eclipse.core.resources.name"
                 value="*.js">
           </test>
        </or>
     </adapt>
  </definition>

我只想在编辑器处于活动状态时显示命令,即它在 window 中具有焦点,或者在从项目资源管理器选择项目期间显示命令。

我应该如何检查编辑器是否处于活动状态,即它是否在 window 中具有焦点?

目前我正在使用 <test/>org.eclipse.ui.IWorkbenchPart 属性 但命令在编辑器 window.

的弹出窗口中根本不可见

运行 没有那个 属性,即使在编辑器中打开适当的文件但未处于活动状态时甚至会显示命令,并且弹出窗口是从项目资源管理器而不是从适当的文件打开的。

感谢您的帮助

编辑

按照@greg-449 的指示,在成功为编辑器执行 运行 命令后更新了代码。但是现在,当尝试限制为特定文件类型时,命令不可见。

        <visibleWhen
              checkEnabled="false">
           <or>
              <with
                    variable="selection">
                 <iterate
                       ifEmpty="false">
                    <reference
                          definitionId="com.varun.ease.ui.sign.definition.testType">
                    </reference>
                 </iterate>
              </with>
              <with
                    variable="activePart">
                 <and>
                    <reference
                          definitionId="com.varun.ease.ui.sign.definition.testType">
                    </reference>
                    <instanceof
                          value="org.eclipse.ui.IEditorPart">
                    </instanceof>
                 </and>
              </with>
           </or>
        </visibleWhen>

使用 activePart 并测试编辑器实例 class。

所以:

<with
   variable="activePart">
   <instanceof
        value="org.eclipse.ui.IEditorPart">
   </instanceof>
</with>

适用于任何编辑器的测试。

您还可以使用 activePartId 来测试编辑器 ID。

如果您还想测试编辑器输入,则需要使用 <and>:

<and>
  <with
     variable="activePart">
     <instanceof
          value="org.eclipse.ui.IEditorPart">
     </instanceof>
  </with>
  <with
     variable="activeEditorInput">
     <reference
         definitionId="com.varun.ease.ui.sign.definition.testType">
     </reference>
  </with>
</and>

Check whether editor is active or not using rcp eclipse

public class Stud_editor_open extends AbstractHandler{

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {

    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
    IWorkbenchPage page = window.getActivePage();
    IEditorReference[] editors = page.getEditorReferences();
    //  How many editor open..
    //  System.out.println("Length : "+editors.length);

    if(editors.length==0){
            System.out.println("Editor is not an active");
    }else{
            System.out.println("Editor is an active");
            System.out.println("Editor Name is :: "+page.getActiveEditor().getTitle());
    }
}

List out open editor

同时打开多个编辑器而不是复制或重新打开新编辑器

for (int i=0; i<editors.length; i++) {
    //page.activate(editors[i].getEditor(true));
    System.out.println("Editor Name :"+editors[i].getTitle().toString());
    return null;
}