Saurabh Chauhan
Saurabh Chauhan

Reputation: 3211

Custom Popup menu in eclipse plugin

In my current project, I have created popup menus using eclipse plugin. Following is the code of plugin.xml.

<?xml version="1.0" encoding="windows-1252"?>
 <?eclipse version="3.0"?>
 <plugin>
<extension point="org.eclipse.emf.ecore.generated_package">
        <package 
        uri = "http://www.xtext.org/example/mydsl/MyDsl"
        class = "org.xtext.example.mydsl.myDsl.MyDslPackage"
        genModel = "model/generated/MyDsl.genmodel" />          
</extension>
<extension
   point="org.eclipse.ui.popupMenus"> 
      <objectContribution
      id="org.xtext.example.mydsl.contribution1"
      objectClass="org.eclipse.core.resources.IFile">           

   <menu
         id="org.xtext.example.mydsl.menu1"
         label="IoTSuite Compilation"
         path="additions">             
      <separator
            name="group1">
      </separator>
   </menu>                 
   <visibleWhen
          checkEnabled="false">
       <iterate
             ifEmpty="false"
             operator="or">
              <test
                property="org.eclipse.core.resources.IFile"
                value="vocab.mydsl">
        <action
        class="org.xtext.example.mydsl.popup.actions.CompileVocabSpec"
         enablesFor="1"
         id="org.xtext.example.mydsl.newAction"
         label="Compile Vocab.mydsl"
         menubarPath="org.xtext.example.mydsl.menu1/group1">
   </action> 
                </test>
       </iterate>
    </visibleWhen>           

   <action
         class="org.xtext.example.mydsl.popup.actions.CompileArchSpec"
         enablesFor="1"
         id="org.xtext.example.mydsl.newAction"
         label="Compile Arch.mydsl"
         menubarPath="org.xtext.example.mydsl.menu1/group1">
   </action>
   <action
         class="org.xtext.example.mydsl.popup.actions.CompileInteractionSpec"
         enablesFor="1"
         id="org.xtext.example.mydsl.newAction"
         label="Compile Interaction.mydsl"
         menubarPath="org.xtext.example.mydsl.menu1/group1">
   </action>
   <action
         class="org.xtext.example.mydsl.popup.actions.CompileDeploySpec"
         enablesFor="1"
         id="org.xtext.example.mydsl.newAction"
         label="Compile Deploy.mydsl"
         menubarPath="org.xtext.example.mydsl.menu1/group1">
   </action>  

</objectContribution>

When I run above eclipse plugin application, it displays popup-menu as follows.Click here for image. The problem is, as shown in image I have four files vocab.mydsl, arch.mydsl, userinteraction.mydsl and deploy.mydsl and also four actions (Compile Vocab.mydsl, Compile Arch.mydsl, Compile Interaction.mydsl and Compile Deploy.mydsl ) under a popup menu. Now I want to customize action in such way that, when I click on vocab.mydsl then it should display only Compile Vocab.mydsl as an action in popup menu similarly when I click in arch.mydsl than it should display only Compile Arch.mydsl etc. I have made changes as per suggestion, but it display error like MESSAGE Plugin org.xtext.example.mydsl, extension org.eclipse.ui.popupMenus: Unknown extension tag found: visibleWhen. Am I missing something ??

Edit
Finally partial problem is solved using comment. Content of updated plugin.xml file is as below:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
 <extension
     point="org.eclipse.ui.menus">
     <menuContribution
    allPopups="false"
    locationURI="menu:org.eclipse.ui.main.menu">
    <command
       commandId="org.xtext.example.mydsl.popup.actions.CompileVocabSpec"
         defaultHandler="org.xtext.example.mydsl.popup.actions.CompileVocabSpec" 
       label="Compile Vocab"
       style="push">
        <visibleWhen
          checkEnabled="false">
       <iterate
             ifEmpty="false"
             operator="or">
          <test
                property="org.eclipse.core.resources.name"
                value="vocab.mydsl">
          </test>
       </iterate>
    </visibleWhen>
 </command>

  <command              
         commandId="org.xtext.example.mydsl.popup.actions.CompileArchSpec"
         defaultHandler="org.xtext.example.mydsl.popup.actions.CompileArchSpec"  
       label="Compile Arch"
       style="push">
        <visibleWhen
          checkEnabled="false">
       <iterate
             ifEmpty="false"
             operator="or">
          <test
                property="org.eclipse.core.resources.name"
                value="arch.mydsl">
          </test>
       </iterate>
    </visibleWhen>
 </command>
 </menuContribution>
  </extension>
 </plugin>

I am very new to this eclipse plugin development. When I fired Compile Vocab command than it should performed action mentioned in CompileVocabSpec.java. The content of CompileVocabSpec.java is as below:

package org.xtext.example.mydsl.popup.actions;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

public class CompileVocabSpec implements IObjectActionDelegate {

private Shell shell;

/**
 * Constructor for Action1.
 */
public CompileVocabSpec() {
    super();
}

/**
 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
 */
@Override
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
    shell = targetPart.getSite().getShell();
}

/**
 * @see IActionDelegate#run(IAction)
 */
@Override
public void run(IAction action) {

    String[] args = new String[3];
    args[0] = "compile-vocab-spec";
    args[1] = "C:/Template/";

    // Call to Main method in ToolSuite
    try {
        // Main.main(args);
        System.out.println("Compilation of Vocab");
    } catch (Exception e) {
        e.printStackTrace();
    }
    ;

}

/**
 * @see IActionDelegate#selectionChanged(IAction, ISelection)
 */
@Override
public void selectionChanged(IAction action, ISelection selection) {
}

}

Here when I fired Compile Vocab command it doesn't performed any action. Am I missing something ??

Upvotes: 1

Views: 3731

Answers (1)

greg-449
greg-449

Reputation: 111142

The org.eclipse.ui.popupMenus is deprecated and has been for a long time. You can only set visibility for the entire objectContribution and I don't think you can specify a single file name.

The org.eclipse.ui.menus extension point allows visibility of individual commands to be controlled.

For example:

<extension
     point="org.eclipse.ui.menus">
  <menuContribution
        allPopups="false"
        locationURI="popup:org.eclipse.ui.popup.any">
     <command
           commandId="test.command"
           label="Command Vocab"
           style="push">
        <visibleWhen
              checkEnabled="false">
           <iterate
                 ifEmpty="false"
                 operator="or">
              <test
                    property="org.eclipse.core.resources.name"
                    value="vocab.mydsl">
              </test>
           </iterate>
        </visibleWhen>
     </command>
  </menuContribution>

The visibleWhen element is restricting the visibility to just the 'vocab.mydsl' file.

Upvotes: 2

Related Questions