Menu entry enabled when a certain key is pressed [message #513107] |
Mon, 08 February 2010 20:49 |
|
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 #513525 is a reply to message #513107] |
Wed, 10 February 2010 13:37 |
|
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
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.
|
|
|
|
Powered by
FUDForum. Page generated in 0.04581 seconds