I'm trying to populate a menu in an RCP application dynamically with radio menu items. The intention is that these menu items correspond to different "modes" for the application.
I'm creating the menu through a dynamic contribution:
@AboutToShow
public void aboutToShow(
final List<MMenuElement> items,
final EModelService modelService) {
// Code to get list of modes
for (Mode m : modeList) {
final MDirectMenuItem menuItem;
menuItem = modelService.createModelElement(MDirectMenuItem.class);
menuItem.setType(ItemType.RADIO);
menuItem.setLabel(m.getName());
/* Set the "handler" instance */
menuItem.setObject(new ModeMenuHandler(m));
items.add(menuItem);
}
When I click on the individual menu items, the corresponding handler is correctly being called. However, the menu items don't behave as Radio items.
How can dynamically created radio menu items be made part of a single radio-item-group?
The problem here is that each time the menu is shown the old menu items are destroyed and the @AboutToShow
method is called again to recreate the menu. So any old radio selection is lost.
So you would need to save the current selection each time it changes and call setSelected(true)
on the appropriate menu item when the menu is created again.
Alternatively if the number of items in the menu doesn't change (your modeList
is a fixed size) you could create the menu once using a processor (the processor
element of the org.eclipse.e4.workbench.model
extension point).