Search code examples
eclipse-juno

How do I disable/enable a handled toolitem based on which MPart is selected?


I have several toolitems that I want to enable/disable based on which MPart the user selected. How can I accomplish that ?


Solution

  • You can add the following code to the handler of your toolbar items:

    @CanExecute
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart part) {
    
      // enable item if active part is part with id "my.part.id"
      if ("my.part.id".equals(part.getElementId())) {
        return true;
      }
    
      // disable item if any other part is active
      return false;
    }
    

    @CanExecute is called before the handled entry is shown in menus or toolbars. If an entry cannot be executed, its state is set to "disabled". In the above code, the active part is injected and can then be used to determine the handler's executable state.