Search code examples
javaeclipseeclipse-rcprcpe4

How to programatically create a ViewMenu in Eclipse RCP 4


    @Inject
    public Widget(MPart part)
    {
        viewMenu = MMenuFactory.INSTANCE.createMenu();
        part.getMenus().add(viewMenu);
    }

When I create a Menu or ViewMenu through the .e4xmi file and check the class of the objects, they both return the same classes. The MMenuFactory only allows me to create an MMenu object. How would I created a ViewMenu or convert an MMenu object to a ViewMenu?

NOTE: The above code does not provide me with a view menu. Though, when I create a ViewMenu through the .e4xmi file, it shows up in list returned from the getMenus() method.


Solution

  • Just add the tag ViewMenu to the menu:

    viewMenu = MMenuFactory.INSTANCE.createMenu();
    
    viewMenu.getTags().add("ViewMenu");
    
    // TODO add menu items
    
    part.getMenus().add(viewMenu);
    

    Note: The view will not be shown if it does not contain some menu items