Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » How to get the command from a handler?
How to get the command from a handler? [message #325904] Sun, 02 March 2008 17:13 Go to next message
Uwe Stieber is currently offline Uwe StieberFriend
Messages: 19
Registered: July 2009
Junior Member
Hi,
Given an active handler instance, how to get the command this handler is
associated with? I need the command id for querying some command specific
properties from a shared data object. With the JFace action framework, both
action id and command id had been always available. With the command/handler
framework I cannot figure out how to do it. Any hint or example for things
like this?

Thanks, Regards, Uwe Stieber
Re: How to get the command from a handler? [message #325905 is a reply to message #325904] Sun, 02 March 2008 17:54 Go to previous messageGo to next message
Uwe Stieber is currently offline Uwe StieberFriend
Messages: 19
Registered: July 2009
Junior Member
> Given an active handler instance, how to get the command this handler is
> associated with? I need the command id for querying some command specific
> properties from a shared data object. With the JFace action framework,
> both action id and command id had been always available. With the
> command/handler framework I cannot figure out how to do it. Any hint or
> example for things like this?

Ok. I can give the answer myself.

If implementing IExecutableExtension, the commandId can be queried and
remembered from the configuration element which triggered the instanciation.
That way a given handler instance always has a reference to the command the
handler had been registered for. Enough for me for the moment.

Thanks, Regards,
Uwe Stieber
Re: How to get the command from a handler? [message #514242 is a reply to message #325905] Fri, 12 February 2010 23:47 Go to previous messageGo to next message
Eugene  is currently offline Eugene Friend
Messages: 25
Registered: July 2009
Junior Member
I actually have a similar problem. I have a toggle button for which I have a handler. The button in the UI needs to have its state updated depending on the current selection, however, the only way I know of changing the button state is to call HandlerUtil.toggleCommandState(Command command).

The problem with this is that I have no way to get the command in my handler. Does anyone have any hints for this?
Re: How to get the command from a handler? [message #514335 is a reply to message #325904] Sun, 14 February 2010 16:23 Go to previous messageGo to next message
Eugene  is currently offline Eugene Friend
Messages: 25
Registered: July 2009
Junior Member
created this out by looking at some other threads:

	protected Command getCommand() {
		Command command = null;
		
		IWorkbench workbench = PlatformUI.getWorkbench();
		if(workbench != null) {
			IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
			if(activeWorkbenchWindow != null) {
				Object service = activeWorkbenchWindow.getService(ICommandService.class);
				if(service != null && service instanceof ICommandService) {
					ICommandService commandService = (ICommandService)service;
					
					//COMMAND_ID is defined in plugin.xml with defaultHandler class
					command = commandService.getCommand(COMMAND_ID);
				}
			}
		}
		
		return command;		
	}	

[Updated on: Sun, 14 February 2010 16:25]

Report message to a moderator

Re: How to get the command from a handler? [message #514375 is a reply to message #514242] Mon, 15 February 2010 06:25 Go to previous messageGo to next message
Prakash G.R. is currently offline Prakash G.R.Friend
Messages: 621
Registered: July 2009
Senior Member
On 13/02/10 5:17 AM, Eugene wrote:
> The problem with this is that I have no way to get the command in my
> handler. Does anyone have any hints for this?

Try executionEvent.getCommand();

- Prakash
Platform UI Team, IBM

Blog <http://blog.eclipse-tips.com>
Twitter <http://www.twitter.com/Eclipse_Tips>
Re: How to get the command from a handler? [message #514489 is a reply to message #514375] Mon, 15 February 2010 15:11 Go to previous messageGo to next message
Eugene  is currently offline Eugene Friend
Messages: 25
Registered: July 2009
Junior Member
Prakash G.R. wrote on Mon, 15 February 2010 01:25
On 13/02/10 5:17 AM, Eugene wrote:
> The problem with this is that I have no way to get the command in my
> handler. Does anyone have any hints for this?

Try executionEvent.getCommand();

- Prakash
Platform UI Team, IBM

Blog <http://blog.eclipse-tips.com>
Twitter <http://www.twitter.com/Eclipse_Tips>


Hi Prakash,

the execution event reference is only available through the handler "as it executes." Please take a look at this javadoc segment:

The data object to pass to the command (and its handler) as it executes.


You will notice that the only place the execution event is available is during the execute method:

	/**
	 * Executes with the map of parameter values by name.
	 * 
	 * @param event
	 *            An event containing all the information about the current
	 *            state of the application; must not be <code>null</code>.
	 * @return the result of the execution. Reserved for future use, must be
	 *         <code>null</code>.
	 * @throws ExecutionException
	 *             if an exception occurred during execution.
	 */
	Object execute(ExecutionEvent event) throws ExecutionException;
Re: How to get the command from a handler? [message #514646 is a reply to message #514489] Tue, 16 February 2010 03:44 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 15.02.2010 16:12, Eugene wrote:
> Prakash G.R. wrote on Mon, 15 February 2010 01:25
>> On 13/02/10 5:17 AM, Eugene wrote:
>> > The problem with this is that I have no way to get the command in my
>> > handler. Does anyone have any hints for this?
>>
>> Try executionEvent.getCommand();
>>
>> - Prakash
>> Platform UI Team, IBM
>>
>> Blog <http://blog.eclipse-tips.com>
>> Twitter <http://www.twitter.com/Eclipse_Tips>
>
>
> Hi Prakash,
>
> the execution event reference is only available through the handler "as
> it executes." Please take a look at this javadoc segment:
>
> The data object to pass to the command (and its handler) as it executes.
>
> You will notice that the only place the execution event is available is
> during the execute method:
>
> /**
> * Executes with the map of parameter values by name.
> * * @param event
> * An event containing all the information about the current
> * state of the application; must not be <code>null</code>.
> * @return the result of the execution. Reserved for future use, must be
> * <code>null</code>.
> * @throws ExecutionException
> * if an exception occurred during execution.
> */
> Object execute(ExecutionEvent event) throws ExecutionException;

Your original description was quite misleading: By speaking
of "to get the command in my handler" it is most natural to
assume that you mean within the execute method.

So, after your correction, it seems, that you are not necessary
within a handler. Please give a short snippet to describe
your problem more specific. In which context are you working,
what is available?

Greetings from Bremen,

Daniel Krügler
Re: How to get the command from a handler? [message #514801 is a reply to message #514646] Tue, 16 February 2010 16:50 Go to previous messageGo to next message
Eugene  is currently offline Eugene Friend
Messages: 25
Registered: July 2009
Junior Member
Here is the description:

You have a toggle button in your UI. Depending on what is selected by the user, the button's state is reflected by the selection. For one item the button can be "on" and for the other item it can be "off."

To implement such behavior, we would have a class that implements IHandler (since we have a Command) and then since this command would respond to the selection, we need our class to also implement ISelectionChangedListener.

Note the methods of the IHandler interface:

public interface IHandler {
	void addHandlerListener(IHandlerListener handlerListener);

	public void dispose();

	Object execute(ExecutionEvent event) throws ExecutionException;

	public boolean isEnabled();

	public boolean isHandled();

	void removeHandlerListener(IHandlerListener handlerListener);
}


Now we add in another method:

public void selectionChanged(SelectionChangedEvent event);


and our handler class now looks like this:
public interface IHandler {
	void addHandlerListener(IHandlerListener handlerListener);

	public void dispose();

	Object execute(ExecutionEvent event) throws ExecutionException;

	public boolean isEnabled();

	public boolean isHandled();

	void removeHandlerListener(IHandlerListener handlerListener);

	public void selectionChanged(SelectionChangedEvent event);
}


When our application starts and the user clicks on a selectable item WITHOUT activating the command first, the item may cause the button to have to update it's toggle state, which would require a reference to the command object. Unfortunately, the only way to get the command object, as Prakash has pointed out, is through the execution event, which we don't have since we are not executing the command.
Re: How to get the command from a handler? [message #514805 is a reply to message #514801] Tue, 16 February 2010 17:04 Go to previous messageGo to next message
Prakash G.R. is currently offline Prakash G.R.Friend
Messages: 621
Registered: July 2009
Senior Member
On 16/02/10 10:20 PM, Eugene wrote:

> You have a toggle button in your UI. Depending on what is selected by
> the user, the button's state is reflected by the selection. For one item
> the button can be "on" and for the other item it can be "off."

Have a look at the IElementUpdater. It helps to update the contribution.

- Prakash
Platform UI Team, IBM

www.eclipse-tips.com
Re: How to get the command from a handler? [message #514814 is a reply to message #514805] Tue, 16 February 2010 17:31 Go to previous messageGo to next message
Eugene  is currently offline Eugene Friend
Messages: 25
Registered: July 2009
Junior Member
Prakash G.R. wrote on Tue, 16 February 2010 12:04
On 16/02/10 10:20 PM, Eugene wrote:

> You have a toggle button in your UI. Depending on what is selected by
> the user, the button's state is reflected by the selection. For one item
> the button can be "on" and for the other item it can be "off."

Have a look at the IElementUpdater. It helps to update the contribution.

- Prakash
Platform UI Team, IBM

www.eclipse-tips.com


My current implementation already implements the IElementUpdater to listen for when an editor or part changes. How can I get the currently selected item(s) (a list) to be sent through the IElementUpdater framework? Like I mentioned earlier, I am just using a selection listener at this point.

[Updated on: Tue, 16 February 2010 17:32]

Report message to a moderator

Re: How to get the command from a handler? [message #514941 is a reply to message #514801] Wed, 17 February 2010 09:52 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 16.02.2010 17:50, Eugene wrote:
> Here is the description:
>
> You have a toggle button in your UI. Depending on what is selected by
> the user, the button's state is reflected by the selection. For one item
> the button can be "on" and for the other item it can be "off."
>
> To implement such behavior, we would have a class that implements
> IHandler (since we have a Command) and then since this command would
> respond to the selection, we need our class to also implement
> ISelectionChangedListener.
>
> Note the methods of the IHandler interface:
>
> public interface IHandler {
> void addHandlerListener(IHandlerListener handlerListener);
>
> public void dispose();
>
> Object execute(ExecutionEvent event) throws ExecutionException;
>
> public boolean isEnabled();
>
> public boolean isHandled();
>
> void removeHandlerListener(IHandlerListener handlerListener);
> }
>
> Now we add in another method:
>
> public void selectionChanged(SelectionChangedEvent event);
>
> and our handler class now looks like this:
> public interface IHandler {
> void addHandlerListener(IHandlerListener handlerListener);
>
> public void dispose();
>
> Object execute(ExecutionEvent event) throws ExecutionException;
>
> public boolean isEnabled();
>
> public boolean isHandled();
>
> void removeHandlerListener(IHandlerListener handlerListener);
>
> public void selectionChanged(SelectionChangedEvent event);
> }
>
> When our application starts and the user clicks on a selectable item
> WITHOUT activating the command first, the item may cause the button to
> have to update it's toggle state, which would require a reference to the
> command object. Unfortunately, the only way to get the command object,
> as Prakash has pointed out, is through the execution event, which we
> don't have since we are not executing the command.

If I understand your design correctly, I'm not sure that it
is reasonable ;-) Obviously a handler has to register to
selection providers and that again would require that it is
created at a point where those selection providers become
active. So it looks to me, as if your handler should *not*
implement as selection observer, instead you use a different
object to realize that.

Regarding your question of getting a command: I do not
understand why you do not use the ICommandService for this,
which provides:

Command getCommand(String commandId);

HTH & Greetings from Bremen,

Daniel Krügler
Re: How to get the command from a handler? [message #514945 is a reply to message #514814] Wed, 17 February 2010 05:04 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 16.02.2010 18:31, Eugene wrote:
> Prakash G.R. wrote on Tue, 16 February 2010 12:04
>> On 16/02/10 10:20 PM, Eugene wrote:
>>
>> > You have a toggle button in your UI. Depending on what is selected by
>> > the user, the button's state is reflected by the selection. For one
>> item
>> > the button can be "on" and for the other item it can be "off."
>>
>> Have a look at the IElementUpdater. It helps to update the contribution.
>>
>> - Prakash
>> Platform UI Team, IBM
>>
>> www.eclipse-tips.com
>
>
> My current implementation already implements the IElementUpdater to
> listen for when an editor or part changes. How can I get the currently
> selected item(s) (a list) to be sent through the IElementUpdater framework?

I'm really not sure that I understand your problem
correctly, but as I noted in another reply, I believe
you need to a different object that takes responsibility
of for selection observations. As a rough idea, you
could use a source provider for this and the source
provider is able to return the list of currently registered
selection providers. Either the source provider uses the
ICommandService to inform the Command or your command
handler uses the ISourceProviderService to get access to
your source provider (observer) and can obtain the
list of registered selection providers.

Does that solve your problem,

- Daniel
Re: How to get the command from a handler? [message #514956 is a reply to message #514945] Wed, 17 February 2010 10:36 Go to previous message
Eugene  is currently offline Eugene Friend
Messages: 25
Registered: July 2009
Junior Member
Daniel Krügler wrote on Wed, 17 February 2010 00:04
On 16.02.2010 18:31, Eugene wrote:
> Prakash G.R. wrote on Tue, 16 February 2010 12:04
>> On 16/02/10 10:20 PM, Eugene wrote:
>>
>> > You have a toggle button in your UI. Depending on what is selected by
>> > the user, the button's state is reflected by the selection. For one
>> item
>> > the button can be "on" and for the other item it can be "off."
>>
>> Have a look at the IElementUpdater. It helps to update the contribution.
>>
>> - Prakash
>> Platform UI Team, IBM
>>
>> www.eclipse-tips.com
>
>
> My current implementation already implements the IElementUpdater to
> listen for when an editor or part changes. How can I get the currently
> selected item(s) (a list) to be sent through the IElementUpdater framework?

I'm really not sure that I understand your problem
correctly, but as I noted in another reply, I believe
you need to a different object that takes responsibility
of for selection observations. As a rough idea, you
could use a source provider for this and the source
provider is able to return the list of currently registered
selection providers. Either the source provider uses the
ICommandService to inform the Command or your command
handler uses the ISourceProviderService to get access to
your source provider (observer) and can obtain the
list of registered selection providers.

Does that solve your problem,

- Daniel


Hi Daniel,

I believe that does solve my problem, however, the second approach you mentioned (getting the command service inside the handler) is the same approach I have implemented and documented above. Thank you for confirming I have done this correctly Smile
Previous Topic:Safe to call IAdaptable.getAdapter() from inside an IAdapterFactory?
Next Topic:Cannot connect to VM
Goto Forum:
  


Current Time: Fri Apr 19 07:54:18 GMT 2024

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

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

Back to the top