[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.rcp] Re: orge.eclipse.ui.handlers enabledWhen using a custom class for evaluation

Thanks Mike,

I'm using PropertyTester but it is not working as expected. Maybe due to this bug:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=199454
I'have tried in 3.4 and it works.
Thanks again,
Cheers,


Julien



Mike Parker a écrit :
Julien wrote:
Hello,

I'm using eclipse 3.3 commands and I handlers. I want that a command can be enabled or disabled according to the user credentials on the selected element. In the enabledWhen details, I don't find any way to call another class to know if the command can be enabled or not. Maybe I'm missing something or it is the wrong way to do this.
Please let me know if I'm wrong,


Best regards,
Julien

Hi Julien,

The intended way of doing what you want is to write a so-called "property tester". See the abstract base class PropertyTester for more info. If you extend this class (as you should), the only method in IPropertyTester you'll need to implement yourself is the test() method:

public boolean test(Object receiver,
                    String property,
                    Object[] args,
                    Object expectedValue)

The idea is that, once you've registered your property tester (via the extension point "org.eclipse.core.expressions.propertyTesters" (see below)), you can use your property in expressions such as:

<enabledWhen>
   <with variable="selection">
      <count
         value="+">
      </count>
      <iterate>
         <test
            property="namespace.being.extended.checkUserCredentials">
         </test>
      </iterate>
   </with>
</enabledWhen>

Note that the "receiver" argument to the test() method should be type-castable to an ISelection in this case, because the "selection" variable is being used. Once you have that, it's simply a case of examining this selection and returning the corresponding boolean result of the sub-expression defined by the "test" XML element, which is used here for determining command enablement (enabledWhen).

In addition to the "property" attribute, the "test" XML element can also contain "args" and/or "value" attributes, that are passed to your test() method as third and fourth arguments, respectively.

Note that the property name needs to match one of those registered with your property tester. VIZ:

<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="(full name of your property tester class goes here)"
id="(ID of your property tester goes here)"
namespace="namespace.being.extended"
properties="checkUserCredentials, ..."
type="(object type to which your property applies, (e.g., org.eclipse.core.resources.IResource))">
</propertyTester>
</extension>


Good luck!

Cheers,
Mike