Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Dynamic ContributionItem menus keyboard Accelerators
Dynamic ContributionItem menus keyboard Accelerators [message #513221] Tue, 09 February 2010 12:21 Go to next message
ali Mising name is currently offline ali Mising name
Messages: 38
Registered: July 2009
Member
Hello all,

I have developed a number of custom ContributionItem that I am contributing to various menus and toolbars of my RCP Application as dynamic "menu:org.eclipse.ui.main.menu" items.

This was working well until I needed to assign keyboard accelerators to some of the dynamic menu items. I can set the accelerator property of the "filled" MenuItem, but, understandably, this does not become active until the menu has been shown for the first time and it's menu items have been constructed and filled.

Does anyone have any suggestions? I have tried extending ActionContributionItem and setting the action accelerator in code, but this is ignored.

I need to use a dynamic ContributionItem so I can use the fill methods to determine which window (shell) my item belongs to and enable / disable it appropriately.

Does anyone know a way of assigning keyboard accelerators to "dynamic" menu items which will work before the actual menu items have been constructed and become visible for the first time (I'd like the shortcut keys to work even though the menus haven't been opened)?
Re: Dynamic ContributionItem menus keyboard Accelerators [message #513241 is a reply to message #513221] Tue, 09 February 2010 13:33 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

ali wrote:
> Hello all,
>
> I have developed a number of custom ContributionItem that I am
> contributing to various menus and toolbars of my RCP Application as
> dynamic "menu:org.eclipse.ui.main.menu" items.

If you delegate off the actual behaviour to a command you can work with
the handlers to do what you want.

>
> This was working well until I needed to assign keyboard accelerators to
> some of the dynamic menu items.

You can do this in an SWT only app, but it won't work reliably (or
sometimes at all) in an RCP app. If you want to use "accelerators" or
shortcuts, you need to use commands/handlers/keybindings. You would use
keybindings to execute the command.

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: Dynamic ContributionItem menus keyboard Accelerators [message #513354 is a reply to message #513241] Tue, 09 February 2010 20:16 Go to previous messageGo to next message
ali Mising name is currently offline ali Mising name
Messages: 38
Registered: July 2009
Member
Hi PW,

Thanks for the quick reply. I agree that that Commands and Handlers seem to be the best way of doing this, unfortunately I have been unable to find a way to determine which Shell / IworkbenchWindow each Command is associated with. I need this information to properly enable and disable the menu and toolbar items. I am aware of how to use the HandlerUtil class to determine the active IworkbenchWindow and validate ALL instances of handled commands, but I am trying to avoid this solution because I'd like the menu and toolbar items to be correctly validated on background windows based on the state of those windows and not just the state of the active window.

I'm unsure if this is clear. For example, the Eclipse IDE uses the Commands and Handlers in many of it's toolbar contributions. Try opening two windows for the same workspace, one with an open java editor and one with no open editors. Note that the toolbar enablement of all background windows incorrectly reflects the state of the active window. This is fine from a usability point of view because the enablement is corrected when the inactive windows become active, but I don't think it's a good practice or promoted a good user experience. Note that the save button behaves correctly because it is an embedded ControlContributionItem. I'm intending to contribute many menu items in my project and I'd rather avoid placing many of these heavyweight controls in my menus.

I've read the links you sent me, but I'm still not sure how to solve this issue (sorry if I missed something).

I'd just like to be able to menu and toolbar items that can be properly validated (enabled / disabled) to their parent window, without that window needing to be active.

And, if I am forced to use dynamic ContributionItems, I'm in need of a way to associate keyboard accelerators to the menu items.

Any advice would be greatly appreciated.

Thanks.
Re: Dynamic ContributionItem menus keyboard Accelerators [message #513568 is a reply to message #513354] Wed, 10 February 2010 10:28 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

ali wrote:
> Hi PW,
> ALL instances of handled commands, but I am trying to avoid this
> solution because I'd like the menu and toolbar items to be correctly
> validated on background windows based on the state of those windows and
> not just the state of the active window.

I understand. Currently, the background window toolitem state won't
(shouldn't) change based on what is happening in the active window, but
it won't update either if it was supposed to change.

> behaves correctly because it is an embedded ControlContributionItem. I'm
> intending to contribute many menu items in my project and I'd rather
> avoid placing many of these heavyweight controls in my menus.

You cannot contribute controls to menus, and the problem doesn't show up
in menus anyway, only tool bars because they're visible all the time.

> And, if I am forced to use dynamic ContributionItems, I'm in need of a
> way to associate keyboard accelerators to the menu items.

It wouldn't be a dynamic item, and ActionContributionItem works (Actions
are tightly coupled MVC, and like the Save tool item are tied to
whatever instantiated them). They're bound to their instantiating
window in their ActionBarAdvisor ... if you want to tightly bind actions
to your window only, you would probably still do it in there.
register(IAction) will also make your action available to the command
system as an ActionHandler (if you define a command and set the
actionDefinitionId on your Action).

You could then link your command to your keybindings.

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: Dynamic ContributionItem menus keyboard Accelerators [message #518336 is a reply to message #513568] Wed, 03 March 2010 20:20 Go to previous message
ali Mising name is currently offline ali Mising name
Messages: 38
Registered: July 2009
Member
Hi Paul,

I have been able to solve this issue by creating a custom extension point called dynamicCommands which allows commands and menu contribution items to be associated using their respective ids. This has allowed me to leverage the command framework to define commands with key bindings (using org.eclipse.ui.bindings) and associate it with my dynamic menu contribution items.

I have created a custom, abstract ContributionItem that uses my custom dynamicCommands configuration to provide the following benefits:

  • The correct Handler can be resolved and executed when my ContributionItem Menu and ToolBar items are selected.
  • Keyboard accelerator functionality is automatically provided by the Command Framework (using org.eclipse.ui.bindings). I did need to add some code in my abstract ContributionItem to find the correct binding to allow menu items to show the correct accelerator.
  • I have also added code to use the org.eclipse.ui.commandImages extension point to define images for my abstract ContributionItem toolbar and menu items.
  • The correct Handler can be resolved to control my abstract ContributionItem enablement.

I have started mass-producing all of my RCP application menu and tool bar buttons using my custom dynamic ContributionItem and it seems to be working very nicely.

Thanks for help with this Paul.

It is very much appreciated.
Previous Topic:help understanding the IPageLayout ratio
Next Topic:RCP app installed as an OSGI bundle
Goto Forum:
  


Current Time: Thu Oct 23 19:18:58 GMT 2014

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

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