eclipse插件中的自定义弹出菜单

时间:2016-01-25 07:40:53

标签: eclipse eclipse-plugin

在我目前的项目中,我使用eclipse插件创建了弹出菜单。以下是 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>

当我在eclipse插件应用程序上运行时,它会显示如下弹出菜单。Click here for image。问题是,如图像所示,我有四个文件 vocab.mydsl,arch.mydsl,userinteraction.mydsl和deploy.mydsl 以及四个动作(Compile Vocab.mydsl,Compile Arch。弹出菜单下的mydsl,Compile Interaction.mydsl和Compile Deploy.mydsl)。现在,我希望自定义操作,当我点击 vocab.mydsl 时,它应仅显示Compile Vocab.mydsl 弹出菜单中的动作类似于我在arch.mydsl中单击时应该只显示Compile Arch.mydsl等。我已根据建议进行了更改,但它显示的错误如MESSAGE Plugin org.xtext.example.mydsl, extension org.eclipse.ui.popupMenus: Unknown extension tag found: visibleWhen。我错过了什么吗?

修改
最后使用评论来解决部分问题。更新的plugin.xml文件的内容如下:

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

我对这个eclipse插件开发很新。当我解除 Compile Vocab命令而不是CompileVocabSpec.java中提到的执行的操作时。 CompileVocabSpec.java的内容如下:

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) {
}

}

这里当我解雇Compile Vocab命令时,它没有执行任何操作。我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

org.eclipse.ui.popupMenus已被弃用且已存在很长时间。您只能设置整个objectContribution的可见性,我不认为您可以指定单个文件名。

org.eclipse.ui.menus扩展点允许查看要控制的各个命令。

例如:

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

visibleWhen元素将可见性限制在&#39; vocab.mydsl&#39;文件。