java 在 Eclipse RCP 中,如何根据编辑器中的“脏”属性禁用保存工具栏按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/650775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Eclipse RCP, how do I disable a save toolbar button according to the "dirty" property in editor
提问by paulgreg
In my eclipse RCP 3.3 application, I would like to enable or disable a 'save' toolbar button according to current editor dirty flag.
在我的 eclipse RCP 3.3 应用程序中,我想根据当前编辑器脏标志启用或禁用“保存”工具栏按钮。
I'm trying to use the <enabledWhen> tag but I can't make it work.
我正在尝试使用 < enabledWhen> 标记,但我无法使其工作。
Here's the portion of code in plugin.xml :
这是 plugin.xml 中的代码部分:
<command
commandId="org.acme.command.save"
icon="icons/save.png"
id="org.acme.command.save"
style="push">
<enabledWhen>
<instanceof value="activeEditor"/>
<test property="dirty" value="true"/>
</enabledWhen>
</command>
Do you have any idea how that is supposed to work ?
你知道这应该如何工作吗?
回答by user85259
Support for the 'Save' and 'Save All' actions is provided by the workbench so you don't need to implement it yourself as you are trying to do.
工作台提供了对“保存”和“全部保存”操作的支持,因此您无需在尝试时自己实现它。
The recommended way is to add the support in your class that extends ActionBarAdvisor. The exact code will depend on the structure of the class, but the bits of code you will need are as follows.
推荐的方法是在扩展 ActionBarAdvisor 的类中添加支持。确切的代码将取决于类的结构,但您需要的代码如下。
in your field declarations:
在您的字段声明中:
private IWorkbenchAction saveAction;
private IWorkbenchAction saveAllAction;
in your makeActions method:
在您的 makeActions 方法中:
saveAction = ActionFactory.SAVE.create(window);
register(saveAction);
saveAllAction = ActionFactory.SAVE_ALL.create(window);
register(saveAllAction);
in your fillActionBars method:
在您的 fillActionBars 方法中:
IToolBarManager saveToolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);
saveToolbar.add(saveAction);
saveToolbar.add(saveAllAction);
coolBar.add(new ToolBarContributionItem(saveToolbar, "save"));
The workbench will take care of the enabling and disabling for you.
工作台将为您处理启用和禁用。
If you do want to implement your own code to do this for whatever reason then the approach you are taking will work. You will need to correct the XML (for example, the instanceof element is checking that the selected object is an instance of a class called 'activeEditor', which is probably not what was intended).
如果您出于任何原因确实想实现自己的代码来执行此操作,那么您所采用的方法将起作用。您将需要更正 XML(例如,instanceof 元素正在检查所选对象是否是名为“activeEditor”的类的实例,这可能不是预期的)。
回答by paulgreg
A brilliant colleague of mine just found the answer for eclipse >= 3.3 :
我的一位才华横溢的同事刚刚找到了 eclipse >= 3.3 的答案:
Here's how to define the command in the plugin.xml :
以下是在 plugin.xml 中定义命令的方法:
<command
commandId="com.acme.bo.command.done"
id="com.acme.bo.menu.done"
label="Command to do">
<visibleWhen>
<with variable="activeMenuSelection">
<iterate>
<adapt type="com.acme.bo.model.Pojo"></adapt>
<test
args="valueThatYouWantToPassTest"
property="com.acme.namespace.testedProperty"
value="something">
</test>
</iterate>
</with>
</visibleWhen>
</command>
Then, you have to define the propertyTester, again in plugin.xml :
然后,您必须再次在 plugin.xml 中定义 propertyTester:
<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="com.acme.namespace.tester.YourPropertyTester"
id="com.acme.namespace.tester.testedPropertyTester"
namespace="com.acme.namespace"
properties="testedProperty"
type="com.acme.bo.model.Pojo">
</propertyTester>
</extension>
And here is YourPropertyTesterclass which do the test :
这是进行测试的YourPropertyTester类:
package com.acme.namespace.tester;
import org.eclipse.core.expressions.PropertyTester;
import com.acme.bo.model.Pojo;
public class YourPropertyTester extends PropertyTester {
@Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
if (receiver == null || !(receiver instanceof Pojo))
return false;
Pojo pojo = (Pojo) receiver;
if (args != null) {
for (Object object : args) {
if (pojo.getYourProperty().contains((String)object))
return true;
}
}
return false;
}
}
回答by VonC
I am not sure it can be entirely declarative.
我不确定它可以完全是声明性的。
saveAction = ActionFactory.SAVE.create(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
configurer.registerGlobalAction(saveAction);
Ca you check if the following thread can help you?
In the case of the Save Action, it may be a Retargetable action.
您可以检查以下线程是否可以帮助您?
在 Save Action 的情况下,它可能是Retargetable action。
回答by Imaskar
try this:
试试这个:
org.eclipse.core.variables.dynamicVariables
-(variable) [here you should implement resolver class to return active editor]
org.eclipse.core.expressions.definitions
-(definition)
--(with) [reference here your variable]
---(test) [test if dirty]
org.eclipse.ui.commands !mistake: not commands, but handlers
-(handler)
--(enabledWhen)
---(reference) [reference your definition here]
**(updated)**
org.eclipse.core.properties.propertyTesters
-(propertyTester)
I'm not sure if it will work, but there is a chance...
我不确定它是否会起作用,但有机会......
回答by czerwin
If you encounter problems with property tester activation be aware that your property tester implementation must be located in the same plugin in which it contributes to org.eclipse.core.expressions.propertyTestersextension point.
如果您在激活属性测试器时遇到问题,请注意您的属性测试器实现必须位于它贡献于org.eclipse.core.expressions.propertyTesters扩展点的同一个插件中。

