Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Menu entry enabled when a certain key is pressed
Menu entry enabled when a certain key is pressed [message #513107] Mon, 08 February 2010 20:49 Go to next message
Catalin Gerea is currently offline Catalin GereaFriend
Messages: 89
Registered: July 2009
Location: Bucharest, Romania
Member

I have a menu consisting of multiple menu entries. All the menu entries are contributed via org.eclipse.ui.menus extension point in my plugin.xml. Each menu entry is a command that has a default handler assigned.

I want to show one of the menu entries only if the CTRL key is pressed.

From the description of the above extension point I think that I should use "enabledWhen" but I do not know what to put next.

Or is there another way to achieve this?


Time is what you make of it.
Re: Menu entry enabled when a certain key is pressed [message #513240 is a reply to message #513107] Tue, 09 February 2010 13:29 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

I don't believe there's a way to achieve this unless you are writing the
SWT yourself. You would basically have to know that there was a
SWT.KeyDown event and then your SWT.Show event for the Menu (without an
SWT.KeyUp event or a second SWT.KeyDown)

There might be another way, but I don't know it.

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: Menu entry enabled when a certain key is pressed [message #513525 is a reply to message #513107] Wed, 10 February 2010 13:37 Go to previous message
Catalin Gerea is currently offline Catalin GereaFriend
Messages: 89
Registered: July 2009
Location: Bucharest, Romania
Member

Hello Paul

1. I 'hacked' something and it seems to work (see the code bellow). My only problem now is that I do not know how to add my test action from the code bellow to the menu defined in the plugin.xml Sad

2. Is my 'hack' bellow correct or do I need to do additional stuff?

3. How is a key binding defined in plugin.xml used? I mean who links the key sequence of a key binding to the actual command?

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

  private static boolean ctrlPressed = false;

  static {
    PlatformUI.getWorkbench().getDisplay().addFilter(SWT.KeyDown,
        new Listener() {

          public void handleEvent(Event e) {
            if ((e.keyCode & SWT.CTRL) != 0) {
              setCtrlPressed(true);
            }
          }
        });
  }

  private static void setCtrlPressed(boolean b) {
    ctrlPressed = b;
  }

  private boolean getCtrlPressed() {
    return ctrlPressed;
  }

  public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
    super(configurer);
  }

  @Override
  protected void fillMenuBar(IMenuManager menuBar) {
    final IMenuService menuService = (IMenuService) PlatformUI.getWorkbench()
        .getService(IMenuService.class);

    AbstractContributionFactory factory = new AbstractContributionFactory(
        "menu:org.eclipse.ui.main.menu?after=myHelpMenu", null) {
      public void createContributionItems(final IServiceLocator serviceLocator,
          final IContributionRoot additions) {

        final MenuManager testMenu = new MenuManager("TestMenu");
        testMenu.setRemoveAllWhenShown(true);
        testMenu.addMenuListener(new IMenuListener() {

          @Override
          public void menuAboutToShow(IMenuManager manager) {
            if (getCtrlPressed()) {
              testMenu.add(new Action("TestAction") {
                @Override
                public void run() {
                  IWorkbench workbench = PlatformUI.getWorkbench();
                  IBindingService bindingService = (IBindingService) workbench
                      .getService(IBindingService.class);
                  bindingService.openKeyAssistDialog();
                }
              });
              setCtrlPressed(false);
            }
          }
        });
        additions.addContributionItem(testMenu, null);

      }
    };
    menuService.addContributionFactory(factory);

  }

}


Time is what you make of it.
Re: Menu entry enabled when a certain key is pressed [message #513570 is a reply to message #513525] Wed, 10 February 2010 10:28 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Catalin Gerea wrote:
> Hello Paul
>
> 1. I 'hacked' something and it seems to work (see the code bellow). My
> only problem now is that I do not know how to add my test action from
> the code bellow to the menu defined in the plugin.xml :(

Your plugin.xml menuContribution can specify a class attribute that
points to your ContributionFactory.

>
> 2. Is my 'hack' bellow correct or do I need to do additional stuff?

As long as someone has pressed CTRL even once, you will always see your
menu.

>
> 3. How is a key binding defined in plugin.xml used? I mean who links the
> key sequence of a key binding to the actual command?
>

keybinding sequence -> command id is done in the plugin.xml. Then
command id -> handler is done either in the plugin.xml or in the code
(IHandlerService.activateHandler(*))

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


Previous Topic:ContainerSelectionGroup -- advice on reuse?
Next Topic:Eclipse/RCP Application installed on Network Drive Bug
Goto Forum:
  


Current Time: Sun Nov 23 16:19:20 GMT 2014

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

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