Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » [SOLVED] Enable Keybinding only when menu visible
[SOLVED] Enable Keybinding only when menu visible [message #819952] Tue, 13 March 2012 14:40 Go to next message
Martin Missing name is currently offline Martin Missing nameFriend
Messages: 10
Registered: March 2011
Junior Member
Hi,

I have a rather simple goal, but I cannot figure out how to do this:
I want key bindings only be active when the menu is visible. The menu includes a special toolchain, which shall be reachable only when a certain editor type is open.

For that I have defined a menu contribution, which only gets visible, when the active editor is of a certain kind, done through core expression - works fine. That menu is attached to a command id, for which in turn I defined a key binding.

Now, when the menu is invisible (no such editor active), then the key binding is still active, which is bad. Defining a context for it should do it somehow, but the only thing I got to work was defining a context "org.example.my.ui.KeybindingScope" and by letting the Editor set the scope when opened:
	protected void initializeKeyBindingScopes() {
		setKeyBindingScopes(new String[] { "org.example.my.ui.KeybindingScope" });
		System.out.println("Activating my key binding context ...");
	}


Unfortunately the key binding is now only considered active when the editor is focussed, whereas the menu visibility is true as long as the editor is "opened" (which is the wanted behavior).
How do I set the key binding to be effective ONLY when the menu is visible, or better - only for that certain kind of editor?

plugin.xml:
   <extension
         point="org.eclipse.ui.commands">
      <category
            description="Delegates to a special toolchain"
            id="org.example.my.builder.commands.category"
            name="Esterel Category">
      </category>
      <command
            categoryId="org.example.my.builder.commands.category"
            description="Calls toolchain in mode 1"
            id="org.example.my.builder.commands.buildMode1"
            name="Build Mode 1...">
      </command>      
   </extension>
   <extension
         point="org.eclipse.ui.handlers">
      <handler
            class="org.example.my.builder.menues.HBuildMode1"
            commandId="org.example.my.builder.commands.buildMode1">
      </handler>
   </extension>
   <extension
         point="org.eclipse.ui.bindings">
      <key
            commandId="org.example.my.builder.commands.buildMode1"
            contextId="org.example.my.ui.KeybindingScope" 
            schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
            sequence="F7 F7">
      </key>
   </extension>
<extension
         point="org.eclipse.ui.contexts">
      <context
            id="org.example.my.ui.KeybindingScope"
            name="My Editor Opened">
      </context>
   </extension>

   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="menu:org.eclipse.ui.main.menu?after=additions">
         <menu
               id="org.example.my.builder.ui.menus.sampleMenu"
               label="My Toolchain"
               mnemonic="T">
            <command
                  commandId="org.example.my.builder.commands.buildMode1"
                  id="org.example.my.builder.ui.menus.buildMode1"
                  mnemonic="1">
            </command>
            <visibleWhen>
      			<reference
                     definitionId="onlyForMyEditor">
               </reference>
   			</visibleWhen>
      	</menu>
      </menuContribution>
   </extension>
   <extension
         point="org.eclipse.core.expressions.definitions">
      <definition
            id="onlyForMyEditor">
 		         <with variable="activeEditor">
                     <test property="org.eclipse.xtext.ui.editor.XtextEditor.languageName" 
                         value="org.example.my.My"/>
                 </with>          
      </definition>
   </extension>

</plugin>



Thanks,
vbmazter

[Updated on: Tue, 20 March 2012 09:26]

Report message to a moderator

Re: Enable Keybinding only when menu visible [message #819977 is a reply to message #819952] Tue, 13 March 2012 15:14 Go to previous messageGo to next message
Thorsten Schlathölter is currently offline Thorsten SchlathölterFriend
Messages: 211
Registered: February 2012
Location: Düsseldorf
Senior Member
You should activate/deactivate the command rather than hiding the menu. To make the menu invisible just define the visibleWhen expression of the menu and set the checkEnabled to true. Doing so will make the menu invisible if it doesnt have an activeHandler. Without having an active command the binding should also be inactive.

[Updated on: Wed, 14 March 2012 09:41]

Report message to a moderator

Re: Enable Keybinding only when menu visible [message #824949 is a reply to message #819977] Tue, 20 March 2012 09:25 Go to previous message
Martin Missing name is currently offline Martin Missing nameFriend
Messages: 10
Registered: March 2011
Junior Member
Thank you very much, that works perfectly!

Interestingly enough, if the visibleWhen-expression is set for the whole menu contribution, but just a single command's handler is disabled, then the menu element stays visible, but gets disabled. Which is perfectly fine though.

Thanks again for your very valuable help.
Previous Topic:JasperWave 0.9.4: advanced styles management, Zoom and JasperReports Server integration
Next Topic:Activator not found after product export
Goto Forum:
  


Current Time: Sun Nov 23 03:52:41 GMT 2014

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

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