Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Associating an Action with a key binding
Associating an Action with a key binding [message #524827] Fri, 02 April 2010 19:14 Go to next message
Andy is currently offline AndyFriend
Messages: 47
Registered: July 2009
Member
I have a plugin that displays a representation of a custom file system in a tree. There is a custom menu that appears when you right mouse click on a node in the tree. I have a bunch of actions associated with those entries in the menu(deleteAction, cutAction, etc).

Everything works fine if I use the mouse. I can right click, select a node, and then select Delete from the menu and the Action is fired. The issue is I can't seem to figure out any way to associate the "DEL" key with the same operation.

I found this link: http://www.vogella.de/articles/EclipseCommands/article.html# keybinding

but it appears that does something with a "Command" as opposed to an "Action". So, is there any example out there that shows how to associate a key or key sequence with an Action or do I have to try and use "Command"s instead? I don't have any CommandID to refer to in my plugin.xml.

Thanks,

Andy
Re: Associating an Action with a key binding [message #525034 is a reply to message #524827] Mon, 05 April 2010 13:53 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Andy wrote:
>
> but it appears that does something with a "Command" as opposed to an
> "Action". So, is there any example out there that shows how to
> associate a key or key sequence with an Action or do I have to try and
> use "Command"s instead? I don't have any CommandID to refer to in my
> plugin.xml.


keybindings only work with commands. You must create a command and then
the keybinding.

If you have a legacy IAction that you want to bind to the command, you
can hook them up in your createPartControl(*) or wherever you create the
actions:

IHandlerService hs
= (IHandlerService) getSite().getService(IHandlerService.class);
hs.activateHandler(ActionFactory.COPY.getCommandId(),
new ActionHandler(myCopyAction));

PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: Associating an Action with a key binding [message #532917 is a reply to message #524827] Tue, 11 May 2010 14:43 Go to previous messageGo to next message
Andy is currently offline AndyFriend
Messages: 47
Registered: July 2009
Member
Thanks for the code, though I am a bit stumped as to how to implement the steps. Is there a tutorial that explains a step by step process? I would have no problem writing one and posting it so everyone can use it, I just need an outline to work from.

Is there a series of steps that someone could add to the thread and then I could come up with a working example of a conversion?

Thanks,

Andy
Keybinding And Command Step By Step Instructions [message #533970 is a reply to message #524827] Mon, 17 May 2010 15:12 Go to previous message
Andy is currently offline AndyFriend
Messages: 47
Registered: July 2009
Member
Thought I would post this to help anyone else out trying to get a keybinding to work with a command. In this example I want to override the DEL key in Eclipse for my window.

1) Go to your plugin.xml => extensions tab => add org.eclipse.ui.commands

2) Right mouse on org.eclipse.ui.commands => new => command

3) Enter an id such as com.mycompany.commands.delete.

4) Enter a name, such as "Delete"

5) Click the defaultHandler link. On the resulting screen assign a package, have the class extend org.eclipse.core.commands.AbstractHandler, and enter a name for the class. In this case it is called com.mycompany.handlers.DeleteHandler. Leave the class as it stands for now.

6) Back in plugin.xml on the extensions tab, right mouse on org.eclipse.ui.commands => new => context.

7) Enter an id for the context, such as com.mycompany.contexts.forkeybinding

Cool Enter a name, such as ForKeyBinding

9) Important! Enter the parentId as org.eclipse.ui.contexts.window. Eclipse leaves this field empty by default and the command will fail if this isn't added.

10) Still on the plugin.xml / extensions tab, press Add. Add org.eclipse.ui.bindings.

11) Right mouse on org.eclipse.ui.bindings => key. In the resulting screen enter the following:

sequence: DEL (If you want to override a different key, right mouse over the text "sequence*". A dialog will appear that will explain key combinations)

schemeId: org.eclipse.ui.defaultAcceleratorConfiguration

contextId: com.mycompany.contexts.forkeybinding (The id of the context we just created)

commandId: com.mycompany.commands.delete (The id of the command we created earlier)

12) At this point if you run your plugin the DEL key will now fire off the execute method in the command handler. Add whatever logic is necessary that you want achieved in the command.

13) If you have a ViewPart that needs to be updated as a result of the command, the way to get a hold of the ViewPart from within your command is as follows:

public Object execute(ExecutionEvent event) throws ExecutionException {

IWorkbenchPart viewer = HandlerUtil.getActivePart(event);

}

Then you can fire an event into the viewer and update your display.
Previous Topic:Refreshing View
Next Topic:Dyanmic AboutDialog text
Goto Forum:
  


Current Time: Sat Dec 20 06:40:29 GMT 2014

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

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