[SOLVED] Enable Keybinding only when menu visible [message #819952] |
Tue, 13 March 2012 10:40  |
Eclipse User |
|
|
|
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 05:26] by Moderator
|
|
|
|
Re: Enable Keybinding only when menu visible [message #824949 is a reply to message #819977] |
Tue, 20 March 2012 05:25  |
Eclipse User |
|
|
|
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.
|
|
|
Powered by
FUDForum. Page generated in 0.03333 seconds