Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Part actionbar contributor
Part actionbar contributor [message #939268] Wed, 10 October 2012 15:29 Go to next message
ModelGeek Mising name is currently offline ModelGeek Mising nameFriend
Messages: 529
Registered: June 2011
Senior Member
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 #939273 is a reply to message #939268] Wed, 10 October 2012 15:35 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 1407
Registered: July 2012
Senior Member
Hi,

the question is if you need to do it programmatically or if it can be done within the Application Model.

For the last one you can open the Application Model Editor, open your Part, enable the Toolbar (there is a checkbox I searched myself today Smile ). After that you are able to add HandledToolItems to your part specific toolbar.

Hope that helps,
Dirk
Re: Part actionbar contributor [message #939333 is a reply to message #939273] Wed, 10 October 2012 16:55 Go to previous messageGo to next message
ModelGeek Mising name is currently offline ModelGeek Mising nameFriend
Messages: 529
Registered: June 2011
Senior Member
thanks!

actually i forgot to mention, i want to do it programmatically.

any idea?

Re: Part actionbar contributor [message #939375 is a reply to message #939333] Wed, 10 October 2012 17:58 Go to previous messageGo to next message
Brian de Alwis is currently offline Brian de AlwisFriend
Messages: 194
Registered: January 2012
Senior Member

As Dirk mentioned, first figure out how to do it using the model editor. Everything in the model editor is done by manipulating an EMF model, so you can do the same steps in code. The only trick is to figure out which factory you need to create the different objects (E4 has just shy of a dozen, i believe) -- the factories are all in org.eclipse.e4.ui.model.workbench and are named something like M*Factory.

Brian.


Eclipse Platform committer. Ask me about Eclipse support, training, and consulting.
Re: Part actionbar contributor [message #940007 is a reply to message #939375] Thu, 11 October 2012 08:19 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 1407
Registered: July 2012
Senior Member
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. Smile

Greez,
Dirk
Re: Part actionbar contributor [message #940029 is a reply to message #940007] Thu, 11 October 2012 08:47 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot CelaFriend
Messages: 597
Registered: December 2010
Senior Member

You can also inject the MPart in the @PostConstruct method of the part so you don't have to do the searching. Create a method annotated with @PostConstruct and put a MPart p argument on it. After that do with p what Dirk did with myPart.
Re: Part actionbar contributor [message #940284 is a reply to message #940029] Thu, 11 October 2012 13:53 Go to previous messageGo to next message
ModelGeek Mising name is currently offline ModelGeek Mising nameFriend
Messages: 529
Registered: June 2011
Senior Member
thank you so much for assistance.

I would like to add contribution to main tool bar. For example normally we have print/copy/ save etc buttons on tool bar and when some editor is activated and selected they can be enabled. But if editor is not selected then they are not enabled. I have done that in eclipse 3.x using actions. I had editor and use to register action in action registry of editor and EditorActionBarContributor class use to build actions.

I would like to achieve similar functionality in 4x.

Any idea?

Cheers
Re: Part actionbar contributor [message #940298 is a reply to message #940284] Thu, 11 October 2012 14:05 Go to previous message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 1407
Registered: July 2012
Senior Member
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
Previous Topic:org.eclipse.core.runtime.AssertionFailedException
Next Topic:Icon size in Part toolbar
Goto Forum:
  


Current Time: Thu Nov 27 06:04:21 GMT 2014

Powered by FUDForum. Page generated in 0.14797 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software