Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » DirectMenuItem and their IDs in a handler class
DirectMenuItem and their IDs in a handler class [message #1187900] Fri, 15 November 2013 10:38 Go to next message
Michael Relby is currently offline Michael RelbyFriend
Messages: 9
Registered: November 2013
Junior Member
Hello,
I want to implement one class, which will be used by several DirectMenuItem elements in "Class URI" property. Then I want to get source DirectMenuItem's ID in the class method
@Execute
public void execute()

and do different actions depending on the ID.
If look at dependency injection features, I want something like this:
@Execute
public void execute(MDirectMenuItem a_item)

where a_item will be injected as source menu item that triggered that method.
How can I do this?
Thanks.
Re: DirectMenuItem and their IDs in a handler class [message #1188075 is a reply to message #1187900] Fri, 15 November 2013 12:32 Go to previous messageGo to next message
Ozgur Cagdas is currently offline Ozgur CagdasFriend
Messages: 32
Registered: May 2013
Member
Hi Michael,

I'm not sure if I correctly understood why you need this because normally in such a case I'd expect you to have a base class for the common functionality and implement different behaviour in sub classes and link those classes to your DirectMenuItem's class URI. Also, in such a case, I think you can switch to using HandledMenuItem instead of DirectMenuItem so you can get benefit of the command/handler mechanism where/if you need to and you won't have to check the item's id or anything before taking any action.

Anyway, MDirectMenuItem class has getElementId, getContributorURI and getLabel methods which you can possibly use to address your issue. The snippet you put in your question looks correct. So, you should be able to use it in your application the way you already showed.

I've actually created and linked two DirectMenuItems to the same class and each time it's printing the correct id.

Regards,

Ozgur.
Re: DirectMenuItem and their IDs in a handler class [message #1188126 is a reply to message #1188075] Fri, 15 November 2013 13:09 Go to previous messageGo to next message
Michael Relby is currently offline Michael RelbyFriend
Messages: 9
Registered: November 2013
Junior Member
Hi Ozgur,
oh, I've been confused experimenting with Handlers and various menu item implementations and have not noticed the right solution for DirectMenuItem which I provide in first message as an assumption. Thanks for pointing it out and sorry me wasted your time, my assumption really works as I want.
BTW, consider the same situation. How can I figure out things that I am able to use through dependency injection in specific method of handler class? Is there some reference documentation or ability to figure it out at runtime (while debugging)?
For another example, consider model object "Handler" with Class URI linking to particular class. The class has following code:
@Execute
public void execute(MHandler a_hndlr) {
    System.out.println(a_hndlr.getElementId());
}

And this doesn't work: when clicking on corresponding HandledMenuItem - nothing happens (no exceptions, no output at all). I think that MHandler injection is not available, so "execute" method is not executed. So the problem: how can I figure out what is available to me, and what is not?
PS. thank you again! DirectMenuItem snippet worked flawlessly.

[Updated on: Fri, 15 November 2013 13:13]

Report message to a moderator

Re: DirectMenuItem and their IDs in a handler class [message #1196218 is a reply to message #1188126] Tue, 19 November 2013 09:58 Go to previous messageGo to next message
Andrzej Szczepanski is currently offline Andrzej SzczepanskiFriend
Messages: 16
Registered: May 2013
Junior Member
While it doesn't quite answer the question you asked, the following may be of some use to you:

1. you can annotate handler's method parameter with @Optional (org.eclipse.e4.core.di.annotations.Optional), so (can)execute method will be invoked regardless object was available for injection or not (if not, null will be passed).

2. for experimenting you can inject IEclipseContext and query it for objects you want: IEclipseContext::get(Class<?>)
Re: DirectMenuItem and their IDs in a handler class [message #1196373 is a reply to message #1196218] Tue, 19 November 2013 11:39 Go to previous messageGo to next message
Eclipse UserFriend
Out of the model elements you can inject only those who are instances of MContext (extend the MContext interface). There is also a context explorer tool in the making http://dev.eclipse.org/mhonarc/lists/e4-dev/msg08032.html .
Re: DirectMenuItem and their IDs in a handler class [message #1197438 is a reply to message #1196373] Tue, 19 November 2013 22:50 Go to previous message
Ozgur Cagdas is currently offline Ozgur CagdasFriend
Messages: 32
Registered: May 2013
Member
Michael,

Have a look at http://www.vogella.com/articles/EclipseRCP/article.html#commandsmore so maybe you can pass a parameter from each command and in the handler you can differentiate based on the passed in parameter.

However, if you are not just experimenting, I'd still stick with my original suggestion and advise to implement a base class for the common functionality and sub class it for every handler that is going to use it. So, instead of checking which handler caused your @Execute method to get called, you can directly put your handler specific code in its very own class.

Regards,

O.
Previous Topic:Re: Issues when creating a new custom menu using command
Next Topic:InjectionException caused by null argument
Goto Forum:
  


Current Time: Thu Mar 28 14:55:44 GMT 2024

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

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

Back to the top