【问题标题】:In Eclipse RCP, how do I disable a save toolbar button according to the "dirty" property in editor在 Eclipse RCP 中,如何根据编辑器中的“脏”属性禁用保存工具栏按钮
【发布时间】:2009-03-16 14:57:58
【问题描述】:

在我的 Eclipse RCP 3.3 应用程序中,我想根据当前编辑器 dirty flag 启用或禁用“保存”工具栏按钮。

我正在尝试使用 enabledWhen> 标签,但我无法让它工作。

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

你知道它应该如何工作吗?

【问题讨论】:

  • 我只想使用声明式/XML 方式,而不是 Java

标签: java eclipse-rcp


【解决方案1】:

工作台提供对“保存”和“全部保存”操作的支持,因此您无需像尝试那样自行实施。

推荐的方法是在扩展 ActionBarAdvisor 的类中添加支持。确切的代码取决于类的结构,但您需要的代码如下。

在您的字段声明中:

private IWorkbenchAction saveAction;
private IWorkbenchAction saveAllAction;

在您的 makeActions 方法中:

    saveAction = ActionFactory.SAVE.create(window);
    register(saveAction);

    saveAllAction = ActionFactory.SAVE_ALL.create(window);
    register(saveAllAction);

在您的 fillActionBars 方法中:

    IToolBarManager saveToolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);
    saveToolbar.add(saveAction);
    saveToolbar.add(saveAllAction);
    coolBar.add(new ToolBarContributionItem(saveToolbar, "save"));   

工作台将为您处理启用和禁用。

如果您出于某种原因确实想实现自己的代码来执行此操作,那么您所采取的方法将会奏效。您将需要更正 XML(例如,instanceof 元素正在检查所选对象是否是名为“activeEditor”的类的实例,这可能不是预期的)。

【讨论】:

  • 谢谢。我想使用 XML 声明方式,但似乎没有办法。所以你的代码可能有用。
【解决方案2】:

我的一位才华横溢的同事刚刚找到了 eclipse >= 3.3 的答案:

下面是如何在 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>

然后,您必须再次在 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>

这是进行测试的 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;
   }
}

【讨论】:

    【解决方案3】:

    我不确定它是否可以完全是声明性的。

    saveAction = ActionFactory.SAVE.create(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
    configurer.registerGlobalAction(saveAction);
    

    你会检查following thread can help you 吗?
    在保存操作的情况下,它可能是Retargetable action

    【讨论】:

    • 感谢您的回答。但是,我想使用声明方式(顺便说一下,我现在也尝试过使用 PropertyTester 没有运气)。该线程对我的具体问题没有帮助。
    【解决方案4】:

    试试这个:

    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)
    

    我不确定它是否会起作用,但有机会......

    【讨论】:

    • 发现,有预定义的活动编辑器变量-“activeEditor”,所以不需要步骤1。
    • 顺便说一句,即使我还没有在那里找到我的答案,该 URL 对于 Eclipse 核心表达式 (wiki.eclipse.org/Command_Core_Expressions) 很有用。
    • 发现,属性是由属性测试人员评估的,并不检查真实对象的属性本身。第二是 IEditorPart 的脏度没有标准属性,所以你需要 org.eclipse.core.properties.propertyTesters ext.point 和你自己的测试器
    • 此外,当编辑器变脏时,该属性将不会被重新评估(仅当 activeEditor 被更改时)。要解决此问题,您需要 org.eclipse.ui.services.IEvaluationService.requestEvaluation(String) 在适当的时间请求重新评估。所以无论如何它都会去java
    • Arg... 好的,所以没有办法让它以这种方式工作。非常感谢您的回答!
    【解决方案5】:

    如果您在激活属性测试器时遇到问题,请注意您的属性测试器实现必须位于它为 org.eclipse.core.expressions.propertyTesters 扩展点提供帮助的同一插件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-08
      • 2010-09-14
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多