Skip to main content



      Home
Home » Eclipse Projects » Eclipse 4 » Remove E4 menu item programmatically
icon5.gif  Remove E4 menu item programmatically [message #1739428] Mon, 01 August 2016 04:53 Go to next message
Eclipse UserFriend
Hy guys,

we have added a menu item via Application.e4xmi.

Now, our customer wants a special version of our product, where this menu item is not visible.

So we created a bundle, where we implemented the following:

  @Execute
  public void execute(final MApplication application, final EModelService service, final Display display)
  {
    final IEclipseContext context = application.getContext();

    //here, the correct menuContribution is found
    final List<MMenuContribution> collect =
      application.getMenuContributions().stream()
        .filter(menuContribution -> menuContribution.getElementId().equals("ui.menucontribution.help"))
        .collect(Collectors.toList());
    for (final MMenuContribution menu : collect)
    {
     //the following 3 lines are executed, but have no effect
      application.getMenuContributions().remove(menu.getElementId());
      menu.setToBeRendered(false);
      menu.setVisible(false);
    }

    application.getMenuContributions().clear();
  }


This code is not working. Is there anything else which has to be done to get the application model updated?

Or is there another way to remove a menu item which has been added via Application.e4xmi?

Thanks for any help and kind regards!
Re: Remove E4 menu item programmatically [message #1739433 is a reply to message #1739428] Mon, 01 August 2016 05:31 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

I assume that you have a command with the ID "ui.menucontribution.help" and you want to remove all the UI-Elements (menu items and ttolbar items) that are associated with the command.
If this is correct, you could try something like this and pass in "ui.menucontribution.help" as commandId:

void removeCommand(String commandId, MApplication application) {
	for (MCommand command : application.getCommands().toArray(new MCommand[application.getCommands().size()])) {
		if (commandId.equals(command.getElementId())) {
			Collection<Setting> collection = EcoreUtil.UsageCrossReferencer.find((EObject) command, (EObject) application);
			for (Setting setting : collection) {
				if (setting.getEObject() instanceof MUIElement) {
					MUIElement uiElement = (MUIElement)setting.getEObject();
					uiElement.setToBeRendered(false);
				} 
			}
		}
	}
}


Hope this helps.

Cheers,
Christoph
Re: Remove E4 menu item programmatically [message #1739435 is a reply to message #1739433] Mon, 01 August 2016 06:43 Go to previous messageGo to next message
Eclipse UserFriend
It works. Really great!
Thank you for your incredibly fast support.
Re: Remove E4 menu item programmatically [message #1739460 is a reply to message #1739435] Mon, 01 August 2016 18:34 Go to previous messageGo to next message
Eclipse UserFriend
Another (easier?) way of doing it is to add a method anotated with @CanExecute in your handler linked to the command associated with the menuItem, to inject the menu item and disable it based on some logic. If the method annotated with "@CanExecute" returns false, the method annotated with "@Execute" will not be called

@Execute
public void execute(final MApplication application, final EModelService service, final Display display) {
 <logic associated to the menuItem>
}

@CanExecute
public boolean canExecute(MMenuItem menuItem) {
   menuItem.setVisible(false);
   return false;
}



[Updated on: Mon, 01 August 2016 18:36] by Moderator

Re: Remove E4 menu item programmatically [message #1739466 is a reply to message #1739460] Tue, 02 August 2016 02:38 Go to previous messageGo to next message
Eclipse UserFriend
Hy guys,

the second answer was also very helpful.

Now we developed the following solution (maybe this helps someone else too =) ):

- We are defining an Eclipse Activity
- Then we are checking the activityPatternBindings programmatically in the @CanExecute method
- If a certain binding is set we are enabling or disabling the menu item with the removeCommand() method.

The reason that we wanted to use Eclipse Activities is, that this is the easiest way for our customer to customize his own product version.

This works really fine!

Thanks again =) =)

[Updated on: Tue, 02 August 2016 02:41] by Moderator

Re: Remove E4 menu item programmatically [message #1739498 is a reply to message #1739466] Tue, 02 August 2016 08:55 Go to previous message
Eclipse UserFriend
Maybe it would be easier to have a preference page in your app that your users uses to check/uncheck what they want to see or not
Then test the values set in the preference in the @CanExecute annotated method to enable/disable menu items...The prefernce can be injected in the handler too..
Previous Topic:Add menu to scrolled Form Toolbar
Next Topic:Changing perspectives/tabs draws phantom controls for a split second.
Goto Forum:
  


Current Time: Mon Jul 28 15:40:31 EDT 2025

Powered by FUDForum. Page generated in 0.04716 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top