Search code examples
rcpe4

RCP e4 programmatically create Toolbar with HandledToolItem in Part


I'm having a problem regarding the HandledToolItem in my MToolbar. When pressing a button the application should create a new Part with a Toolbar and one HandledToolItem. The problem is that the HandledToolItem is always greyed out and I don't know why yet.

final MPart mPart = modelService.createModelElement(MPart.class);
mPart.setLabel("Test");
mPart.setElementId("newid");
mPart.setContributionURI("bundleclass://something");
mPart.setCloseable(true);

// create Toolbar
final MToolBar mBar = modelService.createModelElement(MToolBar.class);
mPart.setToolbar(mBar);

// create HanledToolItem
final MHandledToolItem mItem = modelService.createModelElement(MHandledToolItem.class);
mBar.getChildren().add(mItem);

// create Handle and Command
final MHandler toolHandler = modelService.createModelElement(MHandler.class);
final MCommand toolCommand = modelService.createModelElement(MCommand.class);

toolCommand.setElementId("dsadsadsa");
toolHandler.setCommand(toolCommand);
toolHandler.setContributionURI("bundleclass://something");

mItem.setIconURI("platform:/plugin/RCPCAN/icons/icon_con_scroll_lock.png");
mItem.setTooltip("Lock Table Scrollbar");
mItem.setCommand(toolCommand);
mItem.setEnabled(true);

// show part
partService.showPart(mPart, PartState.ACTIVATE);

Solution

  • You must add any handler you create to the list of handlers for the application or component:

    @Inject
    MApplication app;
    
    ...
    
    app.getHandlers().add(handler);
    

    Similarly commands must be added to the getCommands list.

    Note: It is much easier to use a 'PartDescriptor' in your Application.e4xmi containing the design of the part. You can then just call

    partService.showPart("part descriptor id", PartState.ACTIVATE);
    

    without needing to create anything in your code.

    If you want to create multiple copies of a part use:

    MPart newPart = partService.createPart("part descriptor id");
    
    partService.showPart(newPart, PartState.ACTIVATE);