Part actionbar contributor [message #939268] |
Wed, 10 October 2012 11:29  |
Eclipse User |
|
|
|
Hi,
I would like to add few actions/handelers specific to a inputpart. In 3.x we used to define actionbarcontributor class in editor declaration which use to add actions to tool bar and to menu bar.
How can i achieve the same effect in eclipse 4?
Cheers,
|
|
|
|
|
|
Re: Part actionbar contributor [message #940007 is a reply to message #939375] |
Thu, 11 October 2012 04:19   |
Eclipse User |
|
|
|
Hi,
I'm not sure if this is the preferred way as I'm quite new to Eclipse 4, but for adding items to a part toolbar I've found the following.
The Eclipse 4 Application model is a dynamic model. So you can add/remove/modify everything at runtime.
As Brian said, there are some factory classes that help on creating new model elements.
With the help of the EModelService you are able to find model elements that you want to modify.
The following code will search for a part in the application model and add a toolbar with one item to the part. It is in the constructor of the part to modify.
@Inject
public MyPart(MApplication application, EModelService service) {
List<MPart> parts = service.findElements(application, ID, MPart.class, null);
if (parts != null && !parts.isEmpty()) {
MPart myPart = parts.get(0);
//create the toolbar programmatically
MToolBar toolbar = MMenuFactory.INSTANCE.createToolBar();
//create the tool item programmatically
MDirectToolItem element = MMenuFactory.INSTANCE.createDirectToolItem();
element.setElementId("myToolItemId");
element.setIconURI("platform:/plugin/myBundle/icons/someicon.gif");
element.setContributionURI("bundleclass://myBundle/myBundle.handler.MyTestHandler");
element.setVisible(true);
element.setEnabled(true);
toolbar.getChildren().add(element);
myPart.setToolbar(toolbar);
}
}
Making programmatical contributions to the menu of the window should work in a similar way, but you need to find the window menu bar instead of the part toolbar.
Hope it works for you and this is really the Eclipse 4 way to do so. 
Greez,
Dirk
|
|
|
|
|
Re: Part actionbar contributor [message #940298 is a reply to message #940284] |
Thu, 11 October 2012 10:05  |
Eclipse User |
|
|
|
There is the annotation @CanExecute where you define whether a command es enabled or disabled.
There is the annotation @Active which can be used to inject the current active part
@CanExecute
public void canExecute(@Active MPart part) {
//fancy code that checks if this is the part for which the handler should be active
}
This will brings us back to the model solution. Define your handlers in the application model, add them to the main menu and implement the @CanExecute properly.
Greez,
Dirk
|
|
|
Powered by
FUDForum. Page generated in 0.03919 seconds