Varun Raval
Varun Raval

Reputation: 480

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

I am working on a project in which I need to show a popup menu for files having particular extensions only and from popup of editor window and project explorer.

Following is the plugin.xml file that I have made:

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

I want to show command either only when editor is active i.e. it is having focus in window or during selection of project from project explorer.

What should I do to check whether editor is active part i.e. whether it is having focus in window?

Currently I am using <test/> with org.eclipse.ui.IWorkbenchPart property but command is not at all visible in popup of editor window.

Running without that property, command is even shown when appropriate file is open in editor though not active and popup is opened from project explorer not from appropriate file.

Thanks for the help

EDIT

Updated code after successfully running command for editor as directed by @greg-449. But now, command is not visible when trying to restrict to particular file types.

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

Upvotes: 2

Views: 625

Answers (2)

Chetan Bhagat
Chetan Bhagat

Reputation: 572

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

Multiple Editor open at time not dublicate or reopen new 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;
}

Upvotes: 0

greg-449
greg-449

Reputation: 111217

Use activePart and test for an instance of the editor class.

So:

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

Tests for any editor.

You can also use activePartId to test against an editor id.

If you want to test the editor input as well you will need to use an <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>

Upvotes: 1

Related Questions