eclipseeclipse-plugineclipse-rcpe4

How do I pass the MItem, that defines an Imperative Expression in the visibleWhen-Clause, to the Imperative Expressions @Evaluate method?


In Eclipse e4 RCP one can control the visibility of ToolBar and Menu items by adding a visible-when expression in the model definition.

I want to use e4 style imperative expressions instead of the old core expressions. Imperative expressions are defined by creating a class with a method that is annotated with @Evaluate and which returns a boolean.

My question is as follows: When the @Evaluate-method is eventually called, is it possible to somehow find out for which MItem the method is called?

I can pass the eclipse context into the method via dependency injection, get the MApplication object and from there look for any MItem. However, the problem is that I don't know which MItem to look for.

I doesn't work to inject the MItem into the method directly like one can inject an MPart into a handler for example.

What do I want to achieve: My program can be run in several "modes" (think of it like different perspectives). Depending on the mode different menu and toolbar items are supposed to be visible or invisible.

However, I don't want to create a different imperative expression for every item to control its visibility. And I don't want to create one giant imperative expression that defines each items visibility individually inside its method body. It should be doable with one general imperative expression.

My thinking was to add tags (in the model editor) to each item for every mode in which the item is visible. For example, let's call the modes A, B and C. I'd like to add the tags "A" and "B" to an item, if it is supposed to be visible in modes A and B, but invisible in mode C.

For that to work, I need to be able to find out for which MItem the @Evaluate method was called. Then I can get the tags from the item and compare it with the current mode.


Solution

  • It doesn't seem to be possible to get the current item in the imperative expression.

    You can get the current imperative expression model object MImperativeExpression and that can also have tags set.

    @Evaluate
    public boolean test(MImperativeExpression expression)
    {
      List<String> tags = expression.getTags();
      
      ...