Thursday, July 21, 2011

Making extensible GUI with org.eclipse.ui.menus

It's actually first time I had to make a custom part of GUI extensible. In an Eclipse forms editor like following, we wanted custom actions to come from various plugins:



Since eclipse APIs provides this sort of extensibility with their org.eclipse.ui.menus extension point, I looked at how to reuse that mechanism. It turns simple. All you need to do is, while creating the editable area, add:


final Composite parent = ... // that's the area we want to make extensible

ContributionManager contributionManager = new ContributionManager() {
public void update(boolean force) {
for (IContributionItem item : items) {
item.fill(parent);
}
}
};

IMenuService service = (IMenuService) getSite().getService(IMenuService.class);
contributionManager.add(new GroupMarker("testing"));
service.populateContributionManager(contributionManager, "toolbar:org.zend.editor1?after=testing");
contributionManager.update(false);


Now to contribute something to that area, you need to extend org.eclipse.ui.menus extension point:

<extension point="org.eclipse.ui.menus">
<menuContribution allPopups="false"
locationURI="toolbar:org.zend.editor1?after=testing">
… Your contributions here …
</menuContribution>
</extension>

So now, if you for example prepared some commands in advance, you can easily link them without any extra coding. Since I wanted some nice looking link, I used control contribution. It let's you create entirely custom widget.

See the end result:



Lessons learned:
1. When specifying locationURI, it's worth choosing 'toolbar', over 'menu' or others because only 'toolbar' locations support custom control contributions.
2. Only supported URI parameters are 'after', 'before' and 'endof'. When using different parameters, I'm not sure how (if at all) it will be handled and where exactly your contribution will be landed.
3. Not quite sure why, but all the existing docs that I have come accross, only say how to use menus extension point in standard places like view menubar, window toolbar, etc. Nothing about re-using whole mechanism..

3 comments:

  1. INCREDIBLE Idea to solve things !! I was also trying it but didn't think like you. The code written good. It sounds like it will solve the whole problem.

    comment system

    ReplyDelete