0

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);
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Is there a reason why you are not just using a 'PartDescriptor' which will let you design all this in the Application.e4xmi? – greg-449 Nov 06 '15 at 11:25

1 Answers1

1

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);
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • what if I want to create a new part with every button click? how can I just take the descrption. the way I see it I will have to create a new Part programatically. How can I use my description in the workbench to do so? – todayihateprogramming Nov 06 '15 at 12:29
  • Use `EPartService.createPart` to create several parts from a descriptor. Added details to the answer. – greg-449 Nov 06 '15 at 13:06
  • okay I tried to do as you said but the parts I create with partService.createPart("part descriptor id") are always null, even though the id is definitly correct, although this would be a lot easier than my approach – todayihateprogramming Nov 06 '15 at 13:18
  • Works fine for me. Are you sure the Part Descriptor id is correct - this must be a 'PartDescriptor' object in the Application.e4xmi in the 'Part Descriptors' list (not a 'Part' in 'Windows and Dialogs')? – greg-449 Nov 06 '15 at 13:21
  • Oh my god, thank you so much. I thought you were refering to the usual Parts in the Application.e4xmi. Didn't even know something else existed :) – todayihateprogramming Nov 06 '15 at 13:26