How to create context menu or right click menu on button - eclipse-rcp

when i right click on button open a context menu. how i will do it. i try this code also
MenuManager menuManager = new MenuManager();
Menu menu = menuManager.createContextMenu(btn.getText());
btn.getText().setMenu(menu);
getSite().registerContextMenu(menuManager, btn);
getSite().setSelectionProvider(btn);
I got problem in createContextMenu. Please Help me

createContextMenu requires a Control as its argument, you are passing the button text String. The setMenu method also belongs to Control. So:
MenuManager menuManager = new MenuManager();
Menu menu = menuManager.createContextMenu(btn);
btn.setMenu(menu);
getSite().registerContextMenu(menuManager, btn);
Button does not implement ISelectionProvider so you will have to write one if you want to use the button as the selection provider.
Note that a part can only register one context menu like this and there can only be one selection provider for a part.
If you want context menu contributions from other parts of Eclipe to be added to the menu you must add the line:
menuManager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));

Related

Is there a possibility to send ToolbarItems bounds as paramter to an command handler?

In my E4 Application I have a Toolbar with ToolbarItems, on click on one of them I want to display a small dialog directly under the Toolbaritem. To achieve that I need the coordinates of the button.
Is there a way to pass it via paramters to the #Execute annotated method in my handler?
I solved it via injection of MPart into my Handler and a call of getToolbar. But it looks very dirty.
You can inject the MToolItem to get the item rather than injecting the MPart.
#Execute
public void execute(MToolItem mitem)
{
ToolItem item = (ToolItem)mitem.getWidget();
...
}
But you can associate a menu with a tool item by checking the "Menu" check box in the 'Handled Tool Item' entry in the e4xmi file. You will then be able to define menu items as children of the tool item.

Adding menu to In E4 MWindow

In our Eclipse E4 (pure e4) application I have to open a new window and show some views in a new window (which is completely different from the main window). In the new window I am trying to add menu (File->Import) pro-grammatically. I have written the below code but the new window is not showing the menus. Anything I am missing?
`
MTrimmedWindow window = MBasicFactory.INSTANCE.createTrimmedWindow();
....
MMenu menuBar = MenuFactoryImpl.eINSTANCE.createMenu();
menuBar.setLabel("Test");
window.setMainMenu(menuBar);
MMenu fileMenu = MenuFactoryImpl.eINSTANCE.createMenu();
fileMenu.setElementId("file");
fileMenu.setLabel("File");
menuBar.getChildren().add(fileMenu);
MMenuItem item1 = MenuFactoryImpl.eINSTANCE.createDirectMenuItem();
item1.setElementId("item1");
item1.setLabel("item1");
fileMenu.getChildren().add(item1);`
Don't add the window to the application children until after you have created and setup the main menu (and anything else you want to have in the window).
When you add the window to the application child list it is rendered immediately. So if you have not set the main menu at that point it will not be shown.
If your window design is fixed you can avoid all this code by designing the window in the Application.e4xmi. Just turn off the 'to be rendered' flag so it is not show initially and then do:
MTrimmedWindow window = (MTrimmedWindow)modelService.find("window id", app);
window.setToBeRendered(true);
to show the window.

Change menus and menu items programmatically in Eclipse E4

I am having trouble removing existing menus from the model, in a running app.
For example:
MMenu menu = modelService.findElements(app, "idMenuFoo", MMenu.class,
Collections.<String>emptyList(), EModelService.IN_MAIN_MENU).get(0);
menu.setLabel("X");
menu.setVisible(false);
menu.setToBeRendered(false);
After this code gets executed:
The label has been changed to 'X'
But the menu entry is still visible/rendered
If I start the app without clearPersistedState, then restart it, the menu has disappeared. This leads me to be believe the the visibility and rendering attributes were set in the first place, but not applied to the model (unlike the label attribute).
How can I programmatically trigger a main menu bar "refresh" after such changes?
As a Greg in the comment above posted, there is an open bug filed to address this issue. An easy to implement a workaround involves manually refreshing the underlying SWT menu. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=365724#c9 for details. In a gist:
// retrieve the main menu, containing the top-level menu element that needs to be refreshed
MMenu mainMenu = ...
// org.eclipse.e4.ui.workbench.renderers.swt.MenuManagerRenderer
MenuManagerRender renderer = (MenuManagerRenderer)mainMenu.getRenderer();
renderer.getManager(mainMenu).update(true);

openoffice calc button mouse over event cursor change

I have created an OpenOffice Calc spreadsheet and I have inserted a button into it. I can successfully call a macro with the button. I want to have a cursor hand when I mouse-over the button.
First, I switched to 'Design' mode, then I right-clicked on my button, and using the pop-up menu which appears, I selected the 'Events' tab. Then I clicked on the 'Mouse inside' dot dot dot button. In the 'Assign action' window that popped up, I clicked on the 'Macro..' button. I then got the 'Macro Selector' pop-up window, where I choose the 'OpenOffice Macros' folder in the left side pane. I expanded 'Tools'>'ModuleControls' from that 'Library' pane and then selected 'SwitchMousePointer' from the right side 'Macro name' pane, then clicked the 'Ok' 'Ok' buttons.
But now when I hover the mouse cursor over my button I get an OpenOffice Error window popping up "A Scripting Framework error occurred while running the Basic script Tools.ModuleControls.SwitchMousePointer."
I've very little OO experience but I have not found what I want here or on the OO Forum site. I would be very grateful to get some help, thanks, Nige.
The SwitchMousePointer needs attributes (oWindowPeer as Object, bDoEnable as Boolean). So you can't simply assign it to a event. And it will not do what you think.
You should better write you own Sub to change the Pointer. Also there is no need to do this on every mouse event. It should be done once on sheet activate for example.
For a introduce in OpenOffice.org BASIC Programming see https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide. And you will need a debugging tool, if you want to program your own BASIC macros. Therefore see especially: https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide/UNO_Tools
Assuming on the sheet is a button named "Push Button 1" then:
Sub ChangeMousePointer
oController = ThisComponent.CurrentController
oButtonModel = oController.ActiveSheet.DrawPage.Forms(0).getByName("Push Button 1")
oButtonControl = oController.getControl(oButtonModel)
oWindowPointer = CreateUnoService("com.sun.star.awt.Pointer")
oWindowPointer.SetType(com.sun.star.awt.SystemPointer.HAND)
oButtonControl.Peer.setPointer(oWindowPointer)
End Sub
And assign this to the sheet event Activate Document.
Right click the sheet tab and select Sheet Events from the context menu. Then:
If, and only if, the button is on Sheets(0) then the sheet event Activate Document not occurs while opening the document and this sheet is in front. In this case we have to call the Sub ChangeMousePointer also on document event Open Document.
Sub Document_Open
oController = ThisComponent.CurrentController
oSheet = oController.ActiveSheet
if oSheet.Name = ThisComponent.Sheets(0).Name then
if oController.ActiveSheet.DrawPage.Forms.Count > 0 then
if oController.ActiveSheet.DrawPage.Forms(0).hasByName("Push Button 1") then
call ChangeMousePointer
end if
end if
end if
End Sub
To assign the macro to the document event Open Document select Tools - Customize from the menu and:

How to remove or set help content to the Help menu available in WizardDialog in eclipse plugin

I am calling WizardDialog dialog = new WizardDialog and a new window is opening with a help icon in the bottom button tray in extreme left corner. I don't need that button.
How to remove that or is there any way to add help content to it.
According to bug 330206:
To hide the "?" you need to call setHelpAvailable(false) on your WizardDialog.
If you don't control/create the dialog, you can add the following method to
your wizard:
public void setContainer(IWizardContainer wizardContainer) {
super.setContainer(wizardContainer);
if (getContainer() instanceof TrayDialog)
((TrayDialog)getContainer()).setHelpAvailable(false);
}
To add Help, you can see the general idea in this thread, but take into account bug 3827:
if you are opening the wizard in a WizardDialog you create, you have to set help on the dialog's shell: ex.
dialog.create();
WorkbenchHelp.setHelp(dialog.getShell(), new Object[]{IHelpContextIds.NEW_WIZARD});
dialog.open();