Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Key binding with command framework in a dialog
Key binding with command framework in a dialog [message #696217] Wed, 13 July 2011 10:19 Go to next message
Sebastian  is currently offline Sebastian
Messages: 5
Registered: April 2011
Junior Member
Hi all,

I try to get a key binding to work in a dialog which extends FormDialog. Binding works fine in the view opening the dialog but not in the dialog itself.

Here are hopefully the relevant extracts of my code:

The command
<extension
point="org.eclipse.ui.commands">

<command
id="my.test.command"
name="Abort an Action">
</command>
</extension>
The handler
<extension
point="org.eclipse.ui.handlers">
<handler
class="mypackage.MyDialog$AbortHandler"
commandId="my.test.command">
</handler>
</extension>
The binding:
<extension
point="org.eclipse.ui.bindings">

<key
commandId="my.test.command"
contextId="org.eclipse.ui.contexts.dialogAndWindow"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="F4">
</key>
</extension>
Handler
...
public static class AbortHandler extends org.eclipse.core.commands.AbstractHandler{

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
System.out.println("Hello World");
return null;
}

}



Best Regards

Sebastian
Re: Key binding with command framework in a dialog [message #696524 is a reply to message #696217] Thu, 14 July 2011 04:01 Go to previous messageGo to next message
Sebastian  is currently offline Sebastian
Messages: 5
Registered: April 2011
Junior Member
Hello,

I have added a key event listener to the dialog, which prints the activate contexts.
form.addKeyListener(new KeyListener() {

@Override
public void keyReleased(KeyEvent e) {
}

@Override
public void keyPressed(KeyEvent e) {
IContextService contextService = (IContextService) PlatformUI
.getWorkbench().getService(IContextService.class);
Collection activeContextIds = contextService
.getActiveContextIds();
for (Object object : activeContextIds) {
System.out.println(object);
}
}

});
The output showed that the relevant context is missing. I do not know if this is the expected behaviour but I can solve the problem by using the context service to activate the context, I used for the key binding.

Is this the correct approach?

@Override
public int open() {
IContextService contextService = (IContextService) PlatformUI
.getWorkbench().getService(IContextService.class);
IContextActivation contextActivation = contextService
.activateContext("org.eclipse.ui.contexts.dialogAndWindow");
int result;
try {
result = super.open();
} finally {
contextService.deactivateContext(contextActivation);
}
return result;
}

The following page helped understanding the problem.

wiki.eclipse.org/Platform_Command_Framework

Best Regards

Sebastian
Re: Key binding with command framework in a dialog [message #698062 is a reply to message #696524] Mon, 18 July 2011 13:47 Go to previous message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

You shouldn't have to specifically activate/deactivate dialogAndWindows, as it is inferred from the other active contexts.

See the javadoc for org.eclipse.ui.contexts.IContextService.registerShell(Shell, int). Does your form dialog have a parent shell?


PW


Previous Topic:Master block in forms
Next Topic:how to define enabledwhen of a self instantiated command ?
Goto Forum:
  


Current Time: Mon Jul 28 14:30:05 EDT 2014

Powered by FUDForum. Page generated in 3.98015 seconds