Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Calling Command from Code in Kepler (4.3)
Calling Command from Code in Kepler (4.3) [message #1121377] Mon, 30 September 2013 15:03 Go to next message
Johannes Spreemann is currently offline Johannes SpreemannFriend
Messages: 19
Registered: August 2013
Location: Germany/Netherlands
Junior Member
Hi,

I'm using an AWTtoSWT bridge because I have to use a 3rd party Swing component. This component shows a JPopUpMenu that contains several Actions. Now I need to call a Command within one of these Actions. But how? I'm stuck at this point. The Code of the Swing Action looks like this one:

public static class ShowDetailsAction extends AbstractAction {
        @Override
	public void actionPerformed(ActionEvent e) {
             // call Command at this point
        }
}


Hope someone knows an easy solution, because I found only the ICommandService for Eclipse 3.7 that's not existing anymore as it seems.

Kind regards
Johannes
Re: Calling Command from Code in Kepler (4.3) [message #1121382 is a reply to message #1121377] Mon, 30 September 2013 15:05 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
ECommand and EHandlerService are your friends in e4.

Tom

On 30.09.13 17:03, Johannes Spreemann wrote:
> Hi,
>
> I'm using an AWTtoSWT bridge because I have to use a 3rd party Swing
> component. This component shows a JPopUpMenu that contains several
> Actions. Now I need to call a Command within one of these Actions. But
> how? I'm stuck at this point. The Code of the Swing Action looks like
> this one:
>
> public static class ShowDetailsAction extends AbstractAction {
> @Override
> public void actionPerformed(ActionEvent e) {
> // call Command at this point
> }
> }
>
> Hope someone knows an easy solution, because I found only the
> ICommandService for Eclipse 3.7 that's not existing anymore as it seems.
>
> Kind regards
> Johannes
Re: Calling Command from Code in Kepler (4.3) [message #1121414 is a reply to message #1121382] Mon, 30 September 2013 15:48 Go to previous messageGo to next message
Johannes Spreemann is currently offline Johannes SpreemannFriend
Messages: 19
Registered: August 2013
Location: Germany/Netherlands
Junior Member
Hmm Ok,

I found the chapter 9.3 of Lars Vogel's Eclipse platform services - Tutorial, but I'm not able to Inject the EHandlerService and the ECommandService. The org.eclipse.e4.core.commands plug-in is specified in my product file (e4 RCP application). But I just get the message EHandlerService cannot be resolved to a type.

Johannes
Re: Calling Command from Code in Kepler (4.3) [message #1122135 is a reply to message #1121414] Tue, 01 October 2013 09:03 Go to previous messageGo to next message
Sumit Singh is currently offline Sumit SinghFriend
Messages: 141
Registered: October 2012
Location: Bangalore
Senior Member

use following code it may helps you.
 ParameterizedCommand openCommand = commandService.createCommand("your-command-ID", null);
 handlerService.executeHandler(openCommand);
        

[Updated on: Tue, 01 October 2013 09:04]

Report message to a moderator

Re: Calling Command from Code in Kepler (4.3) [message #1122270 is a reply to message #1122135] Tue, 01 October 2013 11:41 Go to previous messageGo to next message
Johannes Spreemann is currently offline Johannes SpreemannFriend
Messages: 19
Registered: August 2013
Location: Germany/Netherlands
Junior Member
Hey sumit singh,

Thanks for your answer, but unfortunately I figured out this part on my own. My problem is, that Eclipse doesn't allow me to access the ECommandService nor the EHandlerService. Sad
Everythings working fine (Handled ToolBar Elements and also Handled Menu Buttons). My problem is that I have specified the plug-in org.eclipse.e4.core.commands in my Dependencies, but I can't access the class from within my code.
Re: Calling Command from Code in Kepler (4.3) [message #1122278 is a reply to message #1122270] Tue, 01 October 2013 11:55 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
How do you try to get the EHandlerService? - you need to use @Inject as a constructor, field or method parameter. Note that for handlers you always should prefer the method injection.

How do you create the class where the EHandlerService should be injected? - note that if you create your class using the new operator, injection will not happen!
Re: Calling Command from Code in Kepler (4.3) [message #1122286 is a reply to message #1122278] Tue, 01 October 2013 12:05 Go to previous messageGo to next message
Johannes Spreemann is currently offline Johannes SpreemannFriend
Messages: 19
Registered: August 2013
Location: Germany/Netherlands
Junior Member
1. Restarting Eclipse and renewing the dependencies sometimes helps a lot!
2. Dirk, you're right!
I can't inject it. I have no other possibility than to create the class with the new operator. In this case is there any other solution to call the command?
Re: Calling Command from Code in Kepler (4.3) [message #1122295 is a reply to message #1122286] Tue, 01 October 2013 12:15 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
There are several topics in this forum about that. Smile

You need to use ContextInjectionFactory to either let your object be created via the DI framework or create it your own and inject the values afterwards.
Re: Calling Command from Code in Kepler (4.3) [message #1122357 is a reply to message #1122295] Tue, 01 October 2013 13:31 Go to previous messageGo to next message
Johannes Spreemann is currently offline Johannes SpreemannFriend
Messages: 19
Registered: August 2013
Location: Germany/Netherlands
Junior Member
I don't get it. For the ContextInjectionFactory I also need an injected IEclipseContext.
What I exactly need to do is:
I have a dynamically created JPopUpMenu that is generated new, everytime the user makes an right-click on a JFrame :

popup.add(generateAction(new ShowInfoAbstractAction()));


The ShowInfoAbstractAction static class looks like this:

public static class ShowInfoAbstractAction extends AbstractAction {		

	@Override
	public void actionPerformed(ActionEvent e) {
		// call my Command / Handler
	}
}


I've done it now the "easy" way to get it working. I added the constructor and passed the ECommandService/EHandlerService within the constructor:
public static class ShowInfoAbstractAction extends AbstractAction {

	ECommandService commandService;
	EHandlerService handlerService;

	public ShowInfoAbstractAction (ECommandService commandService,
			EHandlerService handlerService) {
		this.commandService = commandService;
		this.handlerService = handlerService;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Command openCommand = commandService
				.getCommand("xyz.command.infos_open");
		ParameterizedCommand pCmd = new ParameterizedCommand(openCommand,
				null);
		handlerService.executeHandler(pCmd);
	}
}
}
Re: Calling Command from Code in Kepler (4.3) [message #1122432 is a reply to message #1122357] Tue, 01 October 2013 15:15 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Yes of course, if you want to use injection, you need to use the ContextInjectionFactory which is performing the injection and you need the context which contains the values for the injection. That's the way it works.

If you are able to set the ECommandService and the EHandlerService as constructor parameter it is also possible to inject it. Or where do you get the values from?

Nevertheless, it is of course also a solution.
Re: Calling Command from Code in Kepler (4.3) [message #1123355 is a reply to message #1122432] Wed, 02 October 2013 13:31 Go to previous message
Johannes Spreemann is currently offline Johannes SpreemannFriend
Messages: 19
Registered: August 2013
Location: Germany/Netherlands
Junior Member
The ECommandService and EHandlerService are injected into the JFrame class that is generated by Eclipse via the @Creatable annotation. I add this class to the Application context without the IContextFactory, but with the Injected MApplication. Maybe it's not the way it should be done, but it works (for me) Very Happy

@Creatable
public class MyJPanel extends JPanel {
...
        @Inject
	public MyJFrame(MApplication application) {
		application.getContext().set(this.getClass().getName(), this);
        }
...
}
Previous Topic:Add a ShellListener
Next Topic:Exploring your E4 context. What is inside ?
Goto Forum:
  


Current Time: Wed Apr 24 18:04:20 GMT 2024

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

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

Back to the top