Search code examples
javaeclipse-rcpe4

How to get the icon from a handler execute method in eclipse e4


Using a handler for a menu contribution I can get the ParameterizedCommand in the execute method as below.

@Execute
public void execute(final Event event, final ParameterizedCommand command)

How can I get the iconURI of the selected menu item?

I tried this but get "Discouraged Access" warnings

final MenuItem menuItem = (MenuItem) event.widget;
    Object obj = (Object) menuItem.getData("modelElement");
    HandledMenuItemImpl item = (HandledMenuItemImpl) obj;
    String iconUrl = item.getIconURI();

Solution

  • Just inject the MMenuItem to the handler:

    @Execute
    public void execute(MMenuItem menuItem, ..... other parameters)
    {
       String iconUrl = menuItem.getIconURI();
       ...
    }
    

    If the handler is also called from non-menu events make the menu item optional

    @Execute
    public void execute(@Optional MMenuItem menuItem, ..... other parameters)
    {
       if (menuItem != null) {
          String iconUrl = menuItem.getIconURI();
          ...
       }
    }