Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Remove E4 menu item programmatically
icon5.gif  Remove E4 menu item programmatically [message #1739428] Mon, 01 August 2016 08:53 Go to next message
Alexander Fichtinger is currently offline Alexander FichtingerFriend
Messages: 66
Registered: January 2013
Member
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 09:31 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph KeimelFriend
Messages: 482
Registered: December 2010
Location: Germany
Senior Member
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 10:43 Go to previous messageGo to next message
Edwin Bruckner is currently offline Edwin BrucknerFriend
Messages: 4
Registered: June 2013
Junior Member
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 22:34 Go to previous messageGo to next message
Denis Forveille is currently offline Denis ForveilleFriend
Messages: 10
Registered: July 2016
Junior Member
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 22:36]

Report message to a moderator

Re: Remove E4 menu item programmatically [message #1739466 is a reply to message #1739460] Tue, 02 August 2016 06:38 Go to previous messageGo to next message
Alexander Fichtinger is currently offline Alexander FichtingerFriend
Messages: 66
Registered: January 2013
Member
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 06:41]

Report message to a moderator

Re: Remove E4 menu item programmatically [message #1739498 is a reply to message #1739466] Tue, 02 August 2016 12:55 Go to previous message
Denis Forveille is currently offline Denis ForveilleFriend
Messages: 10
Registered: July 2016
Junior Member
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: Fri Apr 26 05:12:10 GMT 2024

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

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

Back to the top