Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Command with parameters and DI ?
Command with parameters and DI ? [message #728308] Thu, 22 September 2011 23:26 Go to next message
Cristiano Gaviao is currently offline Cristiano Gaviao
Messages: 253
Registered: July 2009
Senior Member
Hi guys,

I'm trying to learn how to deal with Commands and E4. I'm using the E4 Contacts as example.

I've created this command:

  <commands xmi:id="_VNE7QOVpEeCBqrdVElCRVQ" elementId="teste.c" commandName="Teste">
    <parameters xmi:id="_1ec7cOVqEeCBqrdVElCRVQ" elementId="meu.par.id" name="meu.par" optional="false"/>
  </commands>

 <children xsi:type="menu:HandledMenuItem" xmi:id="_9Yw44OVlEeCBqrdVElCRVQ" elementId="teste" label="Change to Black" command="_VNE7QOVpEeCBqrdVElCRVQ">
          <parameters xmi:id="_wFhu8OVqEeCBqrdVElCRVQ" elementId="x1" name="meu.par.id" value="example"/>
 </children>


And this handler:

@Execute
	public void execute(
			@Named("meu.par.id") String themeId) {
		logger.log(LogService.LOG_DEBUG, "starting myhandler..." + themeId);
	}


It works well...

But when I try to pass another parameter in the execute method (the same as in the Contacts example), it doesn't work. The execute method is not called:
@Execute
	public void execute(
			@Named("meu.par.id") String themeId,
			IThemeEngine engine) {
		logger.log(LogService.LOG_DEBUG, "starting myhandler..." + themeId);
	}


Am I missing something?

thanks for any help...

Cristiano
Re: Command with parameters and DI ? [message #728386 is a reply to message #728308] Fri, 23 September 2011 07:22 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5332
Registered: July 2009
Senior Member
Hi,

Your code looks good and because a IThemeEngine should be there all the
time it looks like a bug. Just to make sure can you try using simply
IEclipseContext which is even more guarenteed to be available or use
@Optional on the IThemeEngine.

If this is NOT working please file a bugzilla (you can CC me on the bug)
and post the bug-id here. Ideally we have a minimal example to reproduce.

Thanks

Tom

Am 23.09.11 01:26, schrieb Cristiano avi:
> Hi guys,
>
> I'm trying to learn how to deal with Commands and E4. I'm using the E4
> Contacts as example.
>
> I've created this command:
>
>
> <commands xmi:id="_VNE7QOVpEeCBqrdVElCRVQ" elementId="teste.c"
> commandName="Teste">
> <parameters xmi:id="_1ec7cOVqEeCBqrdVElCRVQ" elementId="meu.par.id"
> name="meu.par" optional="false"/>
> </commands>
>
> <children xsi:type="menu:HandledMenuItem"
> xmi:id="_9Yw44OVlEeCBqrdVElCRVQ" elementId="teste" label="Change to
> Black" command="_VNE7QOVpEeCBqrdVElCRVQ">
> <parameters xmi:id="_wFhu8OVqEeCBqrdVElCRVQ" elementId="x1"
> name="meu.par.id" value="example"/>
> </children>
>
>
> And this handler:
>
>
> @Execute
> public void execute(
> @Named("meu.par.id") String themeId) {
> logger.log(LogService.LOG_DEBUG, "starting myhandler..." +
> themeId);
> }
>
>
> It works well...
>
> But when I try to pass another parameter in the execute method (the same
> as in the Contacts example), it doesn't work. The execute method is not
> called:
>
> @Execute
> public void execute(
> @Named("meu.par.id") String themeId,
> IThemeEngine engine) {
> logger.log(LogService.LOG_DEBUG, "starting myhandler..." +
> themeId);
> }
>
>
> Am I missing something?
>
> thanks for any help...
>
> Cristiano
Re: Command with parameters and DI ? [message #728533 is a reply to message #728386] Fri, 23 September 2011 12:41 Go to previous messageGo to next message
Cristiano Gaviao is currently offline Cristiano Gaviao
Messages: 253
Registered: July 2009
Senior Member

Hi Tom,

well, if you say that IThemeEngine should be in context all time, so it seems to be a bug.

But I could make it work. What I have to do was to inject IThemeManager and get IThemeEngine from it instead. It works injecting as a field and as method parameter:

        @Inject
	IThemeManager mgr;

@Execute
	public void switchTheme(
			@Named("meu.par.id") String themeId) {

		System.out.println("Escolhedo o tema: " + themeId);
		engine = manager.getEngineForDisplay(Display.getCurrent());

		engine.setTheme(themeId, true);

	}


I will file a bugzilla...

cheers

Cristiano

[Updated on: Fri, 23 September 2011 12:41]

Report message to a moderator

Re: Command with parameters and DI ? [message #728541 is a reply to message #728533] Fri, 23 September 2011 12:42 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5332
Registered: July 2009
Senior Member
Hi,

Hm ... maybe I'm wrong and IThemeEngine is not subject of injection.

Tom

Am 23.09.11 14:41, schrieb Cristiano avi:
> Hi Tom,
> HI Tom,
> well, if you say that IThemeEngine should be in context all time, so it
> seems to be a bug.
> But I could make it work. What I have to do was to inject IThemeManager
> and get IThemeEngine from it instead. It works injecting as a field and
> as method parameter:
>
> @Inject
> IThemeManager mgr;
>
> @Execute
> public void switchTheme(
> @Named("meu.par.id") String themeId) {
>
> System.out.println("Escolhedo o tema: " + themeId);
> engine = manager.getEngineForDisplay(Display.getCurrent());
>
> engine.setTheme(themeId, true);
>
> }
>
>
> I will file a bugzilla...
>
> cheers
>
> Cristiano
Re: Command with parameters and DI ? [message #728542 is a reply to message #728541] Fri, 23 September 2011 12:45 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5332
Registered: July 2009
Senior Member
Hi,

Looks like IThemeEngine is not store in the root context so my
assumption was wrong.

Tom

Am 23.09.11 14:42, schrieb Tom Schindl:
> Hi,
>
> Hm ... maybe I'm wrong and IThemeEngine is not subject of injection.
>
> Tom
>
> Am 23.09.11 14:41, schrieb Cristiano avi:
>> Hi Tom,
>> HI Tom,
>> well, if you say that IThemeEngine should be in context all time, so it
>> seems to be a bug.
>> But I could make it work. What I have to do was to inject IThemeManager
>> and get IThemeEngine from it instead. It works injecting as a field and
>> as method parameter:
>>
>> @Inject
>> IThemeManager mgr;
>>
>> @Execute
>> public void switchTheme(
>> @Named("meu.par.id") String themeId) {
>>
>> System.out.println("Escolhedo o tema: " + themeId);
>> engine = manager.getEngineForDisplay(Display.getCurrent());
>>
>> engine.setTheme(themeId, true);
>>
>> }
>>
>>
>> I will file a bugzilla...
>>
>> cheers
>>
>> Cristiano
>
Re: Command with parameters and DI ? [message #728632 is a reply to message #728542] Fri, 23 September 2011 15:05 Go to previous messageGo to next message
Cristiano Gaviao is currently offline Cristiano Gaviao
Messages: 253
Registered: July 2009
Senior Member
well, it seems that it was once in past, because the e4 Contacts sample(org.eclipse.e4.demo.contacts) at the cvs has this:

public class SwitchThemeHandler {
	@Execute
	public void switchTheme(
			@Named("contacts.commands.switchtheme.themeid") String themeId,
			IThemeEngine engine) {
		engine.setTheme(themeId, true);
	}
}


cheers

Cristiano
Re: Command with parameters and DI ? [message #741256 is a reply to message #728632] Wed, 19 October 2011 10:48 Go to previous messageGo to next message
Sir Wayne is currently offline Sir Wayne
Messages: 214
Registered: July 2009
Senior Member
I have the same Problem. I can't inject my own parameter:

My Handler

public class MyHandler {
	
	@Execute
	public void switchTheme(@Named("test.param.id")String param) {
		System.out.println(themeId);
	}



My Command
<commands xmi:id="_xkR8sPo2EeCGFcYVaQwS9g" elementId="myCommandId" commandName="myCommand">
    <parameters xmi:id="_JsNMsPo8EeCGFcYVaQwS9g" elementId="test.param.id" name="test.param" optional="false"/>
  </commands>



<children xsi:type="menu:HandledMenuItem" xmi:id="_VIFMYPo3EeCGFcYVaQwS9g" label="Blue Theme" command="_xkR8sPo2EeCGFcYVaQwS9g">
          <parameters xmi:id="_XbYEsPo3EeCGFcYVaQwS9g" elementId="x1" name="test.param.id" value="example"/>
        </children>



But nothing was executed

EDIT :

okay i need some AddOns
  <addons xmi:id="_XGB3wPZlEd-XstlTZ6nTXg" elementId="org.eclipse.e4.core.commands.service" contributionURI="platform:/plugin/org.eclipse.e4.core.commands/org.eclipse.e4.core.commands.CommandServiceAddon"/>
  <addons xmi:id="_XGB3wPZlEd-XstlTZ6nTXh" elementId="org.eclipse.e4.ui.contexts.service" contributionURI="platform:/plugin/org.eclipse.e4.ui.services/org.eclipse.e4.ui.services.ContextServiceAddon"/>
  <addons xmi:id="_XGB3wPZlEd-XstlTZ6nTXi" elementId="org.eclipse.e4.ui.bindings.service" contributionURI="platform:/plugin/org.eclipse.e4.ui.bindings/org.eclipse.e4.ui.bindings.BindingServiceAddon"/>
  <addons xmi:id="_LK0NgPZmEd-XstlTZ6nTXj" elementId="org.eclipse.e4.ui.workbench.commands.model" contributionURI="platform:/plugin/org.eclipse.e4.ui.workbench/org.eclipse.e4.ui.internal.workbench.addons.CommandProcessingAddon"/>
  <addons xmi:id="_LK0NgPZmEd-XstlTZ6nTXk" elementId="org.eclipse.e4.ui.workbench.contexts.model" contributionURI="platform:/plugin/org.eclipse.e4.ui.workbench/org.eclipse.e4.ui.internal.workbench.addons.ContextProcessingAddon"/>
  <addons xmi:id="_LK0NgPZmEd-XstlTZ6nTXl" elementId="org.eclipse.e4.ui.workbench.bindings.model" contributionURI="platform:/plugin/org.eclipse.e4.ui.workbench.swt/org.eclipse.e4.ui.workbench.swt.util.BindingProcessingAddon"/>

[Updated on: Thu, 20 October 2011 07:53]

Report message to a moderator

Re: Command with parameters and DI ? [message #880914 is a reply to message #741256] Sun, 03 June 2012 10:54 Go to previous messageGo to next message
Lars Vogel is currently offline Lars Vogel
Messages: 1049
Registered: July 2009
Senior Member

You find an example for defining and using parameters in my tutorial: Eclipse 4 Tutorial - Parameters for commands.
Re: Command with parameters and DI ? [message #881399 is a reply to message #880914] Mon, 04 June 2012 13:34 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

I'll just add you don't generally @Inject handlers. The context they're created in won't necessarily be the context they're executed in.

PW


Re: Command with parameters and DI ? [message #916004 is a reply to message #881399] Tue, 18 September 2012 11:46 Go to previous messageGo to next message
Laura V is currently offline Laura V
Messages: 32
Registered: March 2012
Member
Dear E4 Developers,
can anyone explain why do we need to define a Command parameter in the model at all??
Couldn't the method ParameterizedCommand.generateCommand simply look at what parameters the execute method in the corresponding handler has? The types can be determined via reflection.
This is for me a useless complexity layer that is making my life difficult - if there is a good reason for this I am grateful if any of you could explain it to me.
I probably misunderstood something.
Thanks

Laura
Re: Command with parameters and DI ? [message #916009 is a reply to message #916004] Tue, 18 September 2012 11:57 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5332
Registered: July 2009
Senior Member
This way it would have to load all and every handler and remember
there's not always only one handler for a command!

And not all informations you find in the execute-Method are passed as
parameters. e.g. you can get the active shell injected, the current
perspective (in fact anything you get from the current active
IEclipseContext leave).

Tom

Am 18.09.12 13:46, schrieb Laura V:
> Dear E4 Developers,
> can anyone explain why do we need to define a Command parameter in the
> model at all??
> Couldn't the method ParameterizedCommand.generateCommand simply look at
> what parameters the execute method in the corresponding handler has? The
> types can be determined via reflection.
> This is for me a useless complexity layer that is making my life
> difficult - if there is a good reason for this I am grateful if any of
> you could explain it to me.
> I probably misunderstood something.
> Thanks
>
> Laura
Re: Command with parameters and DI ? [message #916042 is a reply to message #916009] Tue, 18 September 2012 12:52 Go to previous messageGo to next message
Laura V is currently offline Laura V
Messages: 32
Registered: March 2012
Member
Hi Tom
I see your point, thanks for explaining it to me
Laura
Re: Command with parameters and DI ? [message #923056 is a reply to message #916004] Tue, 25 September 2012 15:10 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

They're also not quite the parameters you are thinking of.

Command parameters in the definition are for capturing fairly static parameters. Like MenuItem-A is the "Show In Command (view=package explorer)"

The method parameters in @Execute are about accessing the runtime state of the application, and so aren't considered in the command definition.

PW


Re: Command with parameters and DI ? [message #989221 is a reply to message #728308] Wed, 05 December 2012 09:44 Go to previous messageGo to next message
Gaetan Pitteloud is currently offline Gaetan Pitteloud
Messages: 17
Registered: September 2012
Junior Member
Hello all,
Can anybody explain the purpose of the Name and TypeId attributes of a Command Parameter ?
Re: Command with parameters and DI ? [message #989264 is a reply to message #989221] Wed, 05 December 2012 12:18 Go to previous message
Gaetan Pitteloud is currently offline Gaetan Pitteloud
Messages: 17
Registered: September 2012
Junior Member
Looking through source code for the creation of parameterized commands, I notice that parameters are always converted (implicitly with a cast or explicitly with a valueConverter) into a String. Does it mean I cannot pass an arbitrary object as a parameter to a command ?
My use case consists in invoking a handler in response to a user event (list item selection). The selected item is to be passed to the @execute method of my handler, which also takes other injected parameters (like the parent shell, MApplication, EPartService). The initial solution was to use a parameterized command, with the selected item as a parameter, but it doesn't work (class cast exception). Thus the above question about TypeId. But ParameterizedCommand.generateCommand() method reads that even with TypeId (which I suppose is linked to a ParameterType object that contains a ValueConverter to convert the value into a String), I cannot implement my use case this way. Am I right ?

[Updated on: Wed, 05 December 2012 12:36]

Report message to a moderator

Previous Topic:Add editors to an empty parts stack
Next Topic:Does a Lifecycle / Context creation diagram exist for e4?
Goto Forum:
  


Current Time: Tue Sep 23 08:25:56 GMT 2014

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

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