Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Command and KeyBinding in "classic" window
Command and KeyBinding in "classic" window [message #462917] Fri, 02 February 2007 10:28 Go to next message
Nicolas Mura is currently offline Nicolas MuraFriend
Messages: 24
Registered: July 2009
Junior Member
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 #463017 is a reply to message #462917] Sat, 03 February 2007 23:51 Go to previous messageGo to next message
Nicolas Mura is currently offline Nicolas MuraFriend
Messages: 24
Registered: July 2009
Junior Member
Nobody ?
Re: Command and KeyBinding in "classic" window [message #463059 is a reply to message #463017] Mon, 05 February 2007 18:21 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Dude, you need to wait a little while (like a couple of days :-)
PW


Re: Command and KeyBinding in "classic" window [message #463061 is a reply to message #462917] Mon, 05 February 2007 18:29 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

There are 2 parts I guess to getting your shell to work ...

1) you need to decide if it is a dialog or window. See
IContextService#registerShell(*)

That determines if "org.eclipse.ui.contexts.window" or
"org.eclipse.ui.contexts.dialog" is active from the binding service
point of view.

2) You need to supply your handler to the global handler service and
include an active when expression. You might be able to do it
declaratively in org.eclipse.ui.handlers, but if not you would use the
workbench to get the IHandlerService and use ActiveShellExpression to
tie activation to your newly created shells.

Don't forget to deactivate those handlers when your shells disappear or
you'll leak resources.


Later,
PW


Re: Command and KeyBinding in "classic" window [message #463071 is a reply to message #463061] Mon, 05 February 2007 22:40 Go to previous messageGo to next message
Nicolas Mura is currently offline Nicolas MuraFriend
Messages: 24
Registered: July 2009
Junior Member
Hi,
thank you very much for your help. I don't understand all your
informations but I will try to get it working.

PS: Sorry for my second post but I was desperate. I haven't found
anything about this subject over the net.
Re: Command and KeyBinding in "classic" window [message #463238 is a reply to message #463061] Wed, 07 February 2007 10:37 Go to previous messageGo to next message
Nicolas Mura is currently offline Nicolas MuraFriend
Messages: 24
Registered: July 2009
Junior Member
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 #463278 is a reply to message #463238] Wed, 07 February 2007 18:27 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Actions are the old way ... command and handlers are the "new" way,
although they've been around since 3.0, and 3.1 in more or less their
current form.

But in 3.2 you can write your command and your handler to do your
behaviour. Then register your shell and your keybinding will work.

Then create your actions and use the handler service to execute your
command from the run(*) method:
handlerService.executeCommand("AATestSWT.command1", event);

Then your behaviour is correctly in a command and handler, but you can
instantiate your action and still use setText(*) setIcon(*) etc.

Later,
PW


Re: Command and KeyBinding in "classic" window [message #463288 is a reply to message #463278] Wed, 07 February 2007 21:21 Go to previous messageGo to next message
Nicolas Mura is currently offline Nicolas MuraFriend
Messages: 24
Registered: July 2009
Junior Member
Yes, but the setEnable(*) method of the action won't enable or disable
the command (so the keybinding is still active/inactive).
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.

So I have 2 possibilities:
* I can add an "enable" property to my handler class but it less useful.
* 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.

Thank you again for yourr help.

Bye,
Nicolas
Re: Command and KeyBinding in "classic" window [message #463334 is a reply to message #463288] Thu, 08 February 2007 15:07 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

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


Previous Topic:How to enable the FIND-Action
Next Topic:Eclipse crashes by increasing PermGen
Goto Forum:
  


Current Time: Fri Sep 20 21:30:43 GMT 2024

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

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

Back to the top