Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 38351

Summary: [Commands] instrumentation: Listening to commands invoked from menu or key
Product: [Eclipse Project] Platform Reporter: Channing Walton <channingwalton>
Component: UIAssignee: Douglas Pollock <douglas.pollock>
Status: VERIFIED FIXED QA Contact:
Severity: enhancement    
Priority: P2 CC: dejan, eclipse, eclipse, n.a.edgar, sxenos
Version: 2.1   
Target Milestone: 3.1 M6   
Hardware: All   
OS: All   
Whiteboard:

Description Channing Walton CLA 2003-06-02 18:54:12 EDT
I would like to be able to listen to workbench actions - in particular menu actions.

This was discussed here:
http://www.eclipse.org/newsportal/article.php3?id=2059&group=eclipse.platform
http://dev.eclipse.org/newslists/news.eclipse.tools/msg20145.html

I would like to write a plugin that records the number of times menu actions are
performed, and displays the keybindings for the most used actions. This will
help the user learn the keybindings. To do this I need to be notified of actions
that the user performs via menus.

There are other uses such as recording user actions for macros or functional
test suites.

I am thinking that this could be done with an extension point that specifies a
menubar path of interest, and a class that implements an appropriate listener
interface to be notified when actions under the path occur. 

Alternatively, the listener could be added programmtically by the plugin.

Channing
Comment 1 Nick Edgar CLA 2003-12-01 11:42:36 EST
Note that in 3.0, all SWT events get routed through the event filters added 
using Display.addFilter.

I also suggest checking out the Eclipse Instrumentation Framework at:
http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-ui-
home/instrumentation/index.html
Comment 2 Douglas Pollock CLA 2005-03-06 10:53:01 EST
*** Bug 87224 has been marked as a duplicate of this bug. ***
Comment 3 Douglas Pollock CLA 2005-03-11 12:46:57 EST
Okay, I'm writing up the implementation now, and I have one question.  Is it
useful to receive notification of an attempt to execute a command, even if it
doesn't have a valid handler?


The mechanism will work something like this:

Command.addExecutionListener(IExecutionListener)
Command.removeExecutionListener(IExecutionListener)

CommandManager.addExecutionListener(IExecutionListener)
CommandManager.removeExecutionListener(IExecutionListener)

IExecutionListener.aboutToExecute(String commandId,ExecutionEvent)
IExecutionListener.executionFailed(String commandId,ExecutionException)
IExecutionListener.executionSucceeded(String commandId,Object returnValue)


Or, perhaps a 4th method on IExecutionListener?
IExecutionListener.notHandled(String commandId,NotHandledException)


Input is appreciated.
Comment 4 Douglas Pollock CLA 2005-03-11 13:10:55 EST
Another question.  Right now, commands will return an arbitrary object.  Would
it be better if I forced commands to return a particular type of object?  (Note:
this return value would be available in the "executionSucceeded" method.)
Comment 5 Nick Edgar CLA 2005-03-14 11:30:25 EST
Suggest just passing a single event object in the listener methods, and
encapsulating the command id, (optional) result and (optional) exception in the
event object.

Rather than having separate events for success or failure, I suggest having just
one, where the event indicates whether it succeeded or failed.

For naming conventions, the advisors use "pre"/"post" prefixes, e.g.
WindowAdvisor.preWindowOpen/postWindowOpen.  The regular workbench interfaces
use the "ed" suffix, e.g. IWorkbenchWindowListener.windowOpened.  If they
supported pre-notification, the suffix "ing" would be used, e.g.
IWorkbenchWindowListener.windowOpening.  
I'd be fine with either convention for the command listeners.
Comment 6 Nick Edgar CLA 2005-03-14 11:33:11 EST
> Is it useful to receive notification of an attempt to execute a command, 
> even if it doesn't have a valid handler?

Not sure.  Let's leave it out if there's no proven need.

> Would it be better if I forced commands to return a particular type of object?

I don't see any need to constrain the return type.


Comment 7 Douglas Pollock CLA 2005-03-14 18:18:07 EST
Fixed in CVS.  You can get at this in a couple of ways.

ICommandService commandService = (ICommandService)
Workbench.getService(ICommandService.class);
commandService.addExecutionListener(...);

...

Command command = commandService.getCommand("id");
command.addExecutionListener(...);
Comment 8 Douglas Pollock CLA 2005-03-14 18:25:02 EST
As a note, I am note actually modifying the instrumentation plug-in.  I'm not
sure if this is clear.  If someone wants the instrumentation plug-in to take
advantage of this API, please open a separate bug.
Comment 9 Douglas Pollock CLA 2005-03-30 15:01:38 EST
Verified that the API exists on ICommandService.  Any bugs with the actual
service (if any) can be dealt with after 3.1 M6.
Comment 10 Channing Walton CLA 2005-03-30 16:08:28 EST
Excellent, thanks.