Search code examples
eclipsee4

Adding menu to In E4 MWindow


In our Eclipse E4 (pure e4) application I have to open a new window and show some views in a new window (which is completely different from the main window). In the new window I am trying to add menu (File->Import) pro-grammatically. I have written the below code but the new window is not showing the menus. Anything I am missing?

`

MTrimmedWindow window = MBasicFactory.INSTANCE.createTrimmedWindow();
....

MMenu menuBar = MenuFactoryImpl.eINSTANCE.createMenu();
menuBar.setLabel("Test");
window.setMainMenu(menuBar);

MMenu fileMenu = MenuFactoryImpl.eINSTANCE.createMenu();
fileMenu.setElementId("file");
fileMenu.setLabel("File");
menuBar.getChildren().add(fileMenu);

MMenuItem item1 = MenuFactoryImpl.eINSTANCE.createDirectMenuItem();
item1.setElementId("item1");
item1.setLabel("item1");
fileMenu.getChildren().add(item1);`

Solution

  • Don't add the window to the application children until after you have created and setup the main menu (and anything else you want to have in the window).

    When you add the window to the application child list it is rendered immediately. So if you have not set the main menu at that point it will not be shown.

    If your window design is fixed you can avoid all this code by designing the window in the Application.e4xmi. Just turn off the 'to be rendered' flag so it is not show initially and then do:

    MTrimmedWindow window = (MTrimmedWindow)modelService.find("window id", app);
    
    window.setToBeRendered(true);
    

    to show the window.