[英]Custom Popup menu in eclipse plugin
在當前項目中,我使用eclipse plugin創建了彈出菜單 。 以下是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插件應用程序上運行時,它將顯示彈出菜單,如下所示。 點擊這里查看圖片 。 問題是,如圖所示,我有四個文件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
。 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命令時,它沒有執行任何操作。 我錯過了什么嗎?
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
元素將可見性限制為僅vocab.mydsl文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.