Command and KeyBinding in "classic" window [message #462917] |
Fri, 02 February 2007 05:28  |
Eclipse User |
|
|
|
Hi,
Is it possible to use command and keybinding in a "classic" SWT window (ie: Shell, Dialog) ?
I'm developping an RCP application, but many of my screen are shell and not in a WorkbenchPart (View,Editor,...).
I also need to be able to change the implementation of the command (Handler or Action) at runtime.
I've found a sample code about command and keybinding. I've tried it on the WorkbenchWindow and it works fine.
In this example, when I create the Handler I need IHandlerService
IHandlerService handlerService = (IHandlerService) window.getService(IHandlerService.class);
//getSite().getService(IHandlerService.class);
IHandler handler = new org.eclipse.core.commands.AbstractHandler() {
public Object execute(ExecutionEvent event) throws ExecutionException{
System.out.println("This is MyPaste");
return event;
}
};
handlerService.activateHandler("TestKeybinding.command1", handler);
But, how can I obtain an IHandlerService from a "classic" Shell ?
Is there another solution?
What is the best way to create keybinding in "classic" SWT window in an RCP application?
Another question:
Does it mean that it is not possible to use command in a SWT/JFace standalone program because it needs the workbench?
Thanks for any help.
|
|
|
|
|
|
|
Re: Command and KeyBinding in "classic" window [message #463238 is a reply to message #463061] |
Wed, 07 February 2007 05:37   |
Eclipse User |
|
|
|
Thanks, that works great with handlers, but i didn't manage to do the same with IAction.
If I replace :
handlerService.activateHandler("AATestSWT.command1", myHandler, new ActiveShellExpression(testDialog.getDialogShell()));
by :
TestAction2 a = new TestAction2(); //TestAction2 implements IAction
a.setActionDefinitionId("AATestSWT.command1");
Nothing appends when I use the key sequence of my command in my Shell.
I think this is because context of my shell is not active (there is no "ActiveShellExpression(*)" like with handler).
I haven't seen anything that deals with context in the IAction class.
Is it possible to use Command with Action in a "classic" window ?
I need IAction because, I need the same behavior on a button, a key binding and a menu item (popup menu)
and I seem that this is not possible with handler. (And Action have the setActive(*) method that is very useful to
disable binding,button and menu item at the same time)
[XML]
//////////////////////////////////////////////////////////// //////////////////////////////
<extension
point="org.eclipse.ui.commands">
<command
description="Test command"
id="AATestSWT.command1"
name="AATestSWT.command1"/>
</extension>
<extension
point="org.eclipse.ui.bindings">
<key
commandId="AATestSWT.command1"
contextId="org.eclipse.ui.contexts.dialog"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="M1+A"/>
</extension>
//////////////////////////////////////////////////////////// //////////////////////////////
[XML]
[Code]
//////////////////////////////////////////////////////////// //////////////////////////////
IHandler myHandler = new org.eclipse.core.commands.AbstractHandler() {
public Object execute(ExecutionEvent event) throws ExecutionException{
System.out.println("This is MyHandler");
return event;
}
};
IContextService contextService = (IContextService)PlatformUI.getWorkbench()
.getService(IContextService.class);
contextService.registerShell(testDialog.getDialogShell(), IContextService.TYPE_DIALOG);
IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench()
.getService(IHandlerService.class);
TestAction2 a = new TestAction2();
a.setActionDefinitionId("AATestSWT.command1");
//handlerService.activateHandler("AATestSWT.command1", myHandler, new //ActiveShellExpression(testDialog.getDialogShell()));
//////////////////////////////////////////////////////////// //////////////////////////////
[Code]
Thanks,
Nicolas
|
|
|
|
|
Re: Command and KeyBinding in "classic" window [message #463334 is a reply to message #463288] |
Thu, 08 February 2007 10:07  |
Eclipse User |
|
|
|
Nicolas Mura wrote:
> Yes, but the setEnable(*) method of the action won't enable or disable
> the command (so the keybinding is still active/inactive).
Right ... the IAction is just appearance. It's your handler that has to
override isEnabled() to return true or false depending on its state. If
the active handler is disabled, so is the command.
> I think it would great to be able to link the a command to a button/menu
> item by code and be able to enable/disable the command that
> will enable/disable the keybinding and the linked widget.
That's coming in 3.3. Using org.eclipse.ui.menus and menuContribution,
a command placed in a menu or toolbar gets notified when the actual
command becomes enabled or disabled.
>
> So I have 2 possibilities:
> * I can add an "enable" property to my handler class but it less useful.
This is the new model. It's the active handler that determines if it is
enabled or not (and by extension, the command is enabled or not). What
you show on a button or menu item is just appearance, not state.
> * implement the IHandler interface in my Action class : so the
> isEnable(*) method work the action and for the Handler, but maybe there
> is another problem doing this that I don't know.
I'd be hesitant to do this, since you really don't want to mix up Action
and AbstractHandler (or IHandler) classes.
PW
|
|
|
Powered by
FUDForum. Page generated in 0.07788 seconds