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 12:13  |
Eclipse User |
|
|
|
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 #514646 is a reply to message #514489] |
Mon, 15 February 2010 22:44   |
Eclipse User |
|
|
|
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 11:50   |
Eclipse User |
|
|
|
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 #514941 is a reply to message #514801] |
Wed, 17 February 2010 04:52   |
Eclipse User |
|
|
|
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 00:04   |
Eclipse User |
|
|
|
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 05:36  |
Eclipse User |
|
|
|
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 
|
|
|
Goto Forum:
Current Time: Thu Mar 13 15:15:54 EDT 2025
Powered by FUDForum. Page generated in 0.05266 seconds
|