How can I add submenus for pop up menu programatically? - eclipse

In my plugin, I have a pop up menu with menu item 'X' and I want to add submenu to this menu item
and the number and labels of menu items in the submenu and their action will change.
I think I can';t do this from plugin.xml, so how to do this programatically?

In your plugin.xml, under org.eclipse.ui.menus, add a menuContribution that refers to the id of your "root" menu, i.e. the menu that you want to have your submenus attached to (in this case, menu:myDynamicMenuRoot):
<menuContribution
allPopups="true"
class="com.myCode.menus.MyDynamicMenuContributions"
locationURI="menu:myDynamicMenuRoot">
</menuContribution>
Note that allPopups="true" ensures that your submenus will be added to any menu with the id myDynamicMenuRoot that you add anywhere in your application.
Finally, create a class extending ExtensionContributionFactory, whose job it will be to create your dynamic submenu items. Here I add items based on commands I have defined in my plugin.xml:
public class MyDynamicMenuContributions extends ExtensionContributionFactory {
private static final ImageDescriptor GREEN_STAR = Plugin.getImageDescriptor("icons/green_star.png");
#Override
public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
// build a couple of command-based contribution parameters
CommandContributionItemParameter pAA = new CommandContributionItemParameter(
serviceLocator,
"Submenu_CommandAA",
"my.package.command.myCommandAA",
SWT.PUSH);
pAA.icon = GREEN_STAR;
pAA.label = "Command AA";
CommandContributionItemParameter pBB = new CommandContributionItemParameter(
serviceLocator,
"Submenu_CommandBB",
"my.package.command.myCommandBB",
SWT.PUSH);
pBB.icon = GREEN_STAR;
pBB.label = "Command BB";
// create actual contribution items and add them to the given additions reference
CommandContributionItem itemAA = new CommandContributionItem(pAA);
itemAA.setVisible(true);
additions.addContributionItem(itemAA, null);
CommandContributionItem itemBB = new CommandContributionItem(pBB);
itemBB.setVisible(true);
additions.addContributionItem(itemBB, null);
}
}

Related

How to disable a Dynamic Menu Contribution parent in Eclipse 4 RCP Application

This question stems from
How to disable or enable a MMenu (not MMenuItem) in an Eclipse E4 application
I have been attempting to grey-out/disable an entire Dynamic Menu Contribution in Eclipse 4 when a condition is met in the application. The Dynamic Menu Contribution is itself in the File Menu Model Element. My workaround has been to remove all options so the menu does not show anything, but is still active (not-grey) when the condition is met with the code below for clearing the menu.
items.clear();
if (checkMenuEnabled()) {
Fillthemenu();
}
This code below doesn't seem to disable the dynamic menu contribution like I want it to.
MenuImpl menu = (MenuImpl) modelService.find("menuID", application.getChildren().get(0).getMainMenu());
menu.setEnabled(checkMenuEnabled());
Here is an image of the model xmi UI items. The File->Submenu is what I am trying to grey out. Not the individual Dynamic Menu Contribution Items.
Model XMI
Thanks
So in your e4xmi file, you have a "Menu" with a "Dynamic Menu Contribution" and you want to gray out some items in the menu on some application condition, right?
The "Dynamic Menu Contribution" is attached to some "class", right?
In this class, when you generate a disabled "menu":
public class <the class referenced in e4xml> {
#Inject private EModelService modelService;
#AboutToShow
public void aboutToShow(List<MMenuElement> items, {...}) {
MDirectMenuItem dynamicItem = modelService.createModelElement(MDirectMenuItem.class);
dynamicItem.setLabel(<some label>);
dynamicItem.setIconURI(<some icon URI>);
dynamicItem.setContributorURI("platform:/plugin/platform:/plugin/<nom plugin>");
dynamicItem.setContributionURI(<menu item handler> "bundleclass://<plugin name>/<menu item handler class>");
--> dynamicItem.setEnabled(true/false); to enable/grey out the menu
--> dynamicItem.setvisible(true/false); to show/hide the menu
// add one or many MDirectMenuItems ...
items.add(dynamicItem);
}
}
In the menu item handler ("setContributionURI" class) where you implement the logic of the menu item, you can also show/hide/enable/disable the menu item:
public class <menu item handler class> {
#Execute
public void execute({...}) {
<code linked to the menu item selection here>
}
#CanExecute
public boolean canExecute(#Optional MMenuItem menuItem, {...}) {
// implement the logic to show/hide, enable/disable the menu item
menuItem.setVisible(true/false); // show/hide the menu item
return true/false; // enable/grey out the menu item
}
}

How to capture the value of a cell of a TableViewer where a contextual menu has been activated in eclipse e4?

In one of my eclipse e4 application JMSToolBox, some data is displayed in aTableViewer
A contextual menu is defined in the e4 model file (e4xmi) and linked to theTableViewer like this
menuService.registerContextMenu(tableViwere.getTable(), <name of the e4 part menu>);
Attached to the contextual menu in the e4 model, a "menu item" is linked to a"Dynamic Menu Contribution" class that dynamically add the menu items to the menu:
public class VisualizerShowPayloadAsMenu {
#Inject private EModelService modelService;
#AboutToShow
public void aboutToShow(EModelService modelService, List<MMenuElement> items) {
// Not the real code..., illustrate adding a dynamic menu item to the contextual menu
MDirectMenuItem dynamicItem = modelService.createModelElement(MDirectMenuItem.class);
dynamicItem.setLabel(<name..>);
dynamicItem.setContributorURI(Constants.BASE_CORE_PLUGIN);// "platform:/plugin/org.titou10.jtb.core");
dynamicItem.setContributionURI(Constants.VISUALIZER_MENU_URI);// "bundleclass://org.titou10.jtb.core/org.titou10.jtb.visualizer.ui.VisualizerShowPayloadAsHandler");
items.add(dynamicItem);
}
Now, what I want to do is to capture the data in the underlying cell where the contextual menu has been activated, and get that value back in the method annotated by"#AboutToShow" in order
to addMDirectMenuItementries to the contextual menu with a label containing that value
Q: how to do that with eclipse rcp e4?
In the attached picture, the right click happened in the cell with content="ID:414d5120514d41414544202020202020ee4bb25612666920". I would like to get this value back in the #AboutToShowmethod and add menu items to the"Open Payload as..."menu based on that value
Thanks
I found a way to do it!
I'm not sure it is the best way, but at least it works and it is quite simple
The following code is here to illustrate the idea, it is not valid Java.
In the part that manage theTableViewer:
TableViewer tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
{...}
new TableViewerFocusCellManager(tableViewer, new JTBFocusCellHighlighter(tableViewer, windowContext));
JTBFocusCellHighlighterclass:
public class JTBFocusCellHighlighter extends FocusCellHighlighter {
private IEclipseContext windowContext;
private Table table;
public JTBFocusCellHighlighter(ColumnViewer viewer, IEclipseContext windowContext) {
super(viewer);
this.windowContext = windowContext;
this.table = ((TableViewer) viewer).getTable();
}
#Override
protected void focusCellChanged(ViewerCell newCell, ViewerCell oldCell) {
super.focusCellChanged(newCell, oldCell);
// Capture the content of the cell (or other info..) and store it in Eclipse Context
windowContext.set("key", newCell.getText());
TableColumn tableColumn = table.getColumn(newCell.getColumnIndex());
}
}
Real code implementation: JTBSessionContentViewPart , JTBFocusCellHighlighter and FilterMenu

Adding new menu item to a menu bar defined in plugin.xml programmatically in RCP application

I have an eclipse rcp application, with menu extension.
There is one menu item "File"
Now I want to add a new menu item from one of the views (I know this is a wrong design, just want to test it)
in the method createPartControl of my class that extends ViewPar, I have:
Menu menuBar = parent.getShell().getMenuBar(); //I get the Menu that contains File
MenuItem editMenuItem = new MenuItem(menuBar, SWT.CASCADE);
editMenuItem.setText("Edit");
Menu editMenu = new Menu(parent.getShell(), SWT.DROP_DOWN);
editMenuItem.setMenu(editMenu);
When in Debug I watch the parent.getShell().getMenuBar()
I get:
Menu {File, Edit}
But in application window I see only File menu.
To do this programatically rather than via an extension point it looks like you have to use IMenuService and add a contribution factory:
IMenuService menuService = (IMenuService)PlatformUI.getWorkbench().getService(IMenuService.class);
menuService.addContributionFactory(factory);
factory is a class derived from AbstractContributionFactory which provides the menu items.
This example is from http://wiki.eclipse.org/Menu_Contributions/Search_Menu
AbstractContributionFactory searchContribution = new AbstractContributionFactory(
"menu:org.eclipse.ui.main.menu?after=navigate") {
public void createContributionItems(IMenuService menuService,
List additions) {
MenuManager search = new MenuManager("Se&arch",
"org.eclipse.search.menu");
search.add(new GroupMarker("internalDialogGroup"));
search.add(new GroupMarker("dialogGroup"));
search.add(new Separator("fileSearchContextMenuActionsGroup"));
search.add(new Separator("contextMenuActionsGroup"));
search.add(new Separator("occurencesActionsGroup"));
search.add(new Separator("extraSearchGroup"));
additions.add(search);
}
public void releaseContributionItems(IMenuService menuService,
List items) {
// nothing to do here
}
};
menuService.addContributionFactory(searchContribution);

Dynamic treegrid context menus

Is there a way using the treegrid in gwt-ext to have different context menus for different rows?
For example I would like my leaf rows to have different menu options then my non-leaf rows, or at least be able to disable menu options when they aren't revelent to the row that was right clicked.
I created a solution for this problem:
When you create the leafs, you should set the property "type" to "leaf", and the others to "non-leaf".
BaseTreeModel base = new BaseTreeModel();
base.set("type", "leaf");
So, in the selectionChanged event of your tree, you put the verification, and create the menu only for your leafs.
*treePanel.getSelectionModel().addListener(Events.SelectionChange, new SelectionChangedListener<ModelData>() {
#Override
public void selectionChanged(SelectionChangedEvent<ModelData> data) {
BaseTreeModel selected = (BaseTreeModel) data.getSelectedItem();
if ("leaf".equals(selected .get("type").toString())) {
// create the Menu and set it to contextMenu of your tree
} else {
treePanel.setContextMenu(null);
}*
André.

Add dropdown menu in a view's ToolBarManager

I want to add several dynamically created actions to a view. This works to add them to the View Menu in the top right corner:
private void fillActionBars() {
IActionBars bars = getViewSite().getActionBars();
IMenuManager manager = bars.getMenuManager();
IMenuManager myMenu = new MenuManager("Menu title", MY_MENU_ID);
// add actions to myMenu
manager.add(myMenu);
bars.updateActionBars();
}
This works fine. However, I want to add the actions to a dropdown menu in the toolbar instead (so the user can see them immediately). If I replace the third line with
IToolbarManager manager = bars.getToolBarManager();
the menu doesn't show up.
You're right, this doesn't work. A workaround that works fine, not using a MenuManager but a drop down action and a menu creator:
IActionBars bars = getViewSite().getActionBars();
IToolbarManager manager = bars.getToolBarManager();
Action act=new Action("Menu title",SWT.DROP_DOWN){};
act.setMenuCreator(new MyMenuCreator());
manager.add(act);
class MyMenuCreator implements IMenuCreator{
public Menu getMenu(Control ctrl){
...
}
}
You need to use IToolbarManager.add(IContributionItem) with a class that implements IContributionItem. See org.eclipse.ui.internal.FastViewBarContextMenuContribution as an example.