![](/img/trans.png)
[英]Eclipse RCP: How to add a new element to a default Java dropdown menu in the toolbar(New Java Class)
[英]How to programmatically add an enableWhen condition in my java class -Eclipse RCP
首先,我認為我必須澄清與我的問題有關的一些要點:我想重寫RCP項目上按鈕的行為以添加特定的處理方法。 所以我做了一些步驟:
我在這個現有插件上創建了一個新片段,但我無法訪問它。
我在新片段上覆蓋了現有的處理程序。
我添加了fragement.xml,在其中定義了新上下文,即重寫處理程序,如下面的代碼所示:
<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?><?eclipse version="3.4"?>
<fragment>
<extension point="org.eclipse.ui.contexts">
<context id="myContext" name="myContext">
</context>
</extension>
<extension point="org.eclipse.ui.handlers">
<handler class="myHandler" commandId="myCommand">
<enabledWhen>
<and>
<reference definitionId="object_selected">
</reference>
<reference definitionId="operation_allowed">
</reference>
</and>
</enabledWhen>
<activeWhen>
<with variable="activeContexts">
</with>
<iterate ifEmpty="false" operator="or">
<equals value="myContext">
</equals>
</iterate>
</activeWhen>
</handler>
</extension>
</fragment>
但是我的上下文一直都是不確定的(我不知道為什么嗎?),所以我以語法方式定義了上下文,覆蓋處理程序,並將其鏈接到myCommand,如以下代碼所示:
//Define myContext
IContextService contextService = (IContextService) PlatformUI.getWorkbench().getService(IContextService.class);
Context myContext = contextService.getContext("myContext");
if (!myContext.isDefined()) {
myContext.define("myContext", "To allow the consumption of my overrided Handler", "org.eclipse.ui.contexts.window");
}
contextService.activateContext(myContext.getId());
//link handler to myContext
//Command
ICommandService iCommandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
Command myCommand = iCommandService.getCommand("myCommand");
//Handler
IHandlerService iHandlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
MyHandler myHandler = new MyHandler();
myCommand.setHandler(handler);
//set activation conditions
if(myContext!= null && contextService.getActiveContextIds().contains(myContext.getId())) {
iHandlerService.activateHandler("myCommand", myHandler);
// I'm stuck on this step, i need to know how to declare an enableWhen //condition like: myHandler.setEnabled(evaluationContext);
}
現在,我陷入了這一步:我不知道如何以編程方式為我的重寫處理程序添加啟用條件(例如myHandler.setEnabled(evaluationContext))。
您可以使用org.eclipse.core.expressions.ExpressionConverter
將XML DOM或IConfigurationElement
(來自Plug-xml)轉換為可以求值的表達式。
ExpressionConverter converter = ExpressionConverter.getDefault();
Expression expression = converter.perform(element);
其中element
是IConfigurationElement
或org.w3c.dom.Element
。
獲得表達式后,就可以對其進行評估:
EvaluationResult result = expression.evaluate(context);
boolean isEnabled = result == EvaluationResult.TRUE;
context
是IEvaluationContext
對於上下文,您可以使用org.eclipse.e4.core.commands.ExpressionContext
IEclipseContext eclipseContext = ... current Eclipse context
IEvaluationContext context = new ExpressionContext(eclipseContext);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.