簡體   English   中英

在 Eclipse RCP 中,如何根據編輯器中的“臟”屬性禁用保存工具欄按鈕

[英]In Eclipse RCP, how do I disable a save toolbar button according to the “dirty” property in editor

在我的 eclipse RCP 3.3 應用程序中,我想根據當前編輯器臟標志啟用或禁用“保存”工具欄按鈕。

我正在嘗試使用 < 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>

你知道這應該如何工作嗎?

工作台提供了對“保存”和“全部保存”操作的支持,因此您無需像嘗試做的那樣自己實現它。

推薦的方法是在擴展 ActionBarAdvisor 的 class 中添加支持。 確切的代碼將取決於 class 的結構,但您需要的代碼位如下。

在您的字段聲明中:

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 元素正在檢查選定的 object 是否是名為“activeEditor”的 class 的實例,這可能不是預期的)。

我的一位才華橫溢的同事剛剛找到了 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 class 進行測試:

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

如果您在激活屬性測試器時遇到問題,請注意您的屬性測試器實現必須位於它為org.eclipse.core.expressions.propertyTesters擴展點提供幫助的同一插件中。

我不確定它可以完全是聲明性的。

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

你檢查 以下線程是否可以幫助你
在 Save Action 的情況下,它可能是Retargetable action

嘗試這個:

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)

我不確定它是否會起作用,但有機會......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM