Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Eclipse RCP, how do I disable a save toolbar button according to the "dirty" property in editor

In my eclipse RCP 3.3 application, I would like to enable or disable a 'save' toolbar button according to current editor dirty flag.

I'm trying to use the <enabledWhen> tag but I can't make it work.

Here's the portion of code in 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 ?

like image 484
paulgreg Avatar asked Jan 23 '23 18:01

paulgreg


2 Answers

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.

in your field declarations:

private IWorkbenchAction saveAction;
private IWorkbenchAction saveAllAction;

in your makeActions method:

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

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

in your fillActionBars method:

    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).

like image 135
user85259 Avatar answered Jan 26 '23 08:01

user85259


A brilliant colleague of mine just found the answer for eclipse >= 3.3 :

Here's how to define the command in the 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 :

 <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 YourPropertyTester class which do the test :

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;
   }
}
like image 29
paulgreg Avatar answered Jan 26 '23 07:01

paulgreg