I have a handler which is linked to both a menu item and a toolbar icon. If the menu item is selected, a checkmark will appear to the left of the menu item. If the toolbar button is pressed in the icon changes to a 'sunken' to look like it's pushed in.
If the menu item is checkmarked I would like to 'auto-push' the toolbar button in (without firing another toolbar button-pressed event).
If the button is pressed in I would like the menu item to 'auto-checkmark'
Is there a way to do this? I assume it would start here..;
@Execute
public void execute(
@Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
@Optional MToolItem toolItem,
@Optional MMenuItem menuItem) {
// Menu triggered coming into this method
if (menuItem != null ) {
if(menuItem.isSelected()){
...
}
}
// Button triggered coming into this method
if (toolItem != null ) {
if(toolItem.isSelected()){
...
}
}
}
You need both MToolItem and MMenuItem before the first execution. You can find MToolItem via EModelService in a @PostConstruct method.
private MMenuItem menuItem;
private MToolItem toolItem;
@PostConstruct
protected void initControls(MApplication app) {
setToolItem((MToolItem) modelService.find("tool.item.id", app));
}
As for menu item, it cannot be found in such a way via EModelService (in Eclipse 4.3 Kepler, not sure for Luna). That is how we do that in the same @PostConstruct method:
...
MUIElement window = modelService.find("main.window.id", app);
setMenuItem((MHandledMenuItem) findMenuElement("menu.item.id", window));
...
public static MMenuElement findMenuElement(String id, MUIElement searchRoot)
{
if (id == null) {
throw new IllegalArgumentException("id is null!");
}
if (id.length() == 0) {
throw new IllegalArgumentException("Empty string is not allowed in id.");
}
if (searchRoot instanceof MMenuElement && id.equals(searchRoot.getElementId()))
{
return (MMenuElement) searchRoot;
}
if (searchRoot instanceof MTrimmedWindow)
{
MMenuElement findMenu = findMenuElement(id, ((MTrimmedWindow) searchRoot).getMainMenu());
if (findMenu != null)
{
return findMenu;
}
}
else if (searchRoot instanceof MPart)
{
List<MMenu> menus = ((MPart) searchRoot).getMenus();
for (MMenu mm : menus)
{
MMenuElement findMenu = findMenuElement(id, mm);
if (findMenu != null)
{
return findMenu;
}
}
}
else if (searchRoot instanceof MMenu)
{
List<MMenuElement> children = ((MMenu) searchRoot).getChildren();
for (MMenuElement me : children)
{
MMenuElement findMenu = findMenuElement(id, me);
if (findMenu != null)
{
return findMenu;
}
}
}
return null;
}