/**
   * Construct a new menu extender.
   *
   * @param id the menu id
   * @param menu the menu to extend
   * @param prov the selection provider
   * @param part the part to extend
   * @param includeEditorInput Whether the editor input should be included when adding object
   *     contributions to this context menu.
   */
  public PopupMenuExtender(
      final String id,
      final MenuManager menu,
      final ISelectionProvider prov,
      final IWorkbenchPart part,
      final boolean includeEditorInput) {
    super();
    this.menu = menu;
    this.selProvider = prov;
    this.part = part;
    this.modelPart = (MPart) part.getSite().getService(MPart.class);
    if (includeEditorInput) {
      bitSet |= INCLUDE_EDITOR_INPUT;
    }
    menu.addMenuListener(this);
    if (!menu.getRemoveAllWhenShown()) {
      menuWrapper = new SubMenuManager(menu);
      menuWrapper.setVisible(true);
    }
    createModelFor(id);
    addMenuId(id);

    Platform.getExtensionRegistry().addRegistryChangeListener(this);
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.e4.ui.internal.workbench.swt.AbstractPartRenderer#createWidget
   * (org.eclipse.e4.ui.model.application.ui.MUIElement, java.lang.Object)
   */
  @Override
  public Object createWidget(MUIElement element, Object parent) {
    if (!(element instanceof MMenu)) return null;

    final MMenu menuModel = (MMenu) element;
    Menu newMenu = null;
    MenuManager menuManager = null;
    boolean menuBar = false;

    if (parent instanceof Decorations) {
      MUIElement container = (MUIElement) ((EObject) element).eContainer();
      if (container instanceof MWindow) {
        menuManager = getManager(menuModel);
        if (menuManager == null) {
          menuManager = new MenuManager(NO_LABEL, menuModel.getElementId());
          linkModelToManager(menuModel, menuManager);
        }
        newMenu = menuManager.createMenuBar((Decorations) parent);
        ((Decorations) parent).setMenuBar(newMenu);
        newMenu.setData(menuManager);
        menuBar = true;
      } else {
        menuManager = getManager(menuModel);
        if (menuManager == null) {
          menuManager = new MenuManager(NO_LABEL, menuModel.getElementId());
          linkModelToManager(menuModel, menuManager);
        }
        newMenu = menuManager.createContextMenu((Control) parent);
        // we can't be sure this is the correct parent.
        // ((Control) parent).setMenu(newMenu);
        newMenu.setData(menuManager);
      }
    } else if (parent instanceof Menu) {
      // Object data = ((Menu) parent).getData();
      logger.debug(
          new Exception(),
          "Trying to render a sub menu " //$NON-NLS-1$
              + menuModel
              + "\n\t"
              + parent); //$NON-NLS-1$
      return null;

    } else if (parent instanceof Control) {
      menuManager = getManager(menuModel);
      if (menuManager == null) {
        menuManager = new MenuManager(NO_LABEL, menuModel.getElementId());
        linkModelToManager(menuModel, menuManager);
      }
      newMenu = menuManager.createContextMenu((Control) parent);
      // we can't be sure this is the correct parent.
      // ((Control) parent).setMenu(newMenu);
      newMenu.setData(menuManager);
    }
    if (!menuManager.getRemoveAllWhenShown()) {
      processContributions(menuModel, menuBar, menuModel instanceof MPopupMenu);
    }
    if (newMenu != null) {
      newMenu.addDisposeListener(
          new DisposeListener() {
            public void widgetDisposed(DisposeEvent e) {
              cleanUp(menuModel);
            }
          });
    }
    return newMenu;
  }