/* (non-Javadoc)
   * @see org.eclipse.jface.action.IContributionItem#fill(org.eclipse.swt.widgets.Menu, int)
   */
  public void fill(Menu parent, int index) {
    if (menuItem == null || menuItem.isDisposed()) {
      if (index >= 0) {
        menuItem = new MenuItem(parent, SWT.CASCADE, index);
      } else {
        menuItem = new MenuItem(parent, SWT.CASCADE);
      }

      String text = getMenuText();
      if (text != null) {
        menuItem.setText(text);
      }

      if (image != null) {
        LocalResourceManager localManager = new LocalResourceManager(JFaceResources.getResources());
        menuItem.setImage(localManager.createImage(image));
        disposeOldImages();
        imageManager = localManager;
      }

      if (!menuExist()) {
        menu = new Menu(parent);
        menu.setData(MANAGER_KEY, this);
      }

      menuItem.setMenu(menu);

      initializeMenu();

      setDirty(true);
    }
  }
 /**
  * Creates and returns an SWT menu bar control for this menu, for use in the given <code>
  * Decorations</code>, and installs all registered contributions. Does not create a new control if
  * one already exists.
  *
  * @param parent the parent decorations
  * @return the menu control
  * @since 2.1
  */
 public Menu createMenuBar(Decorations parent) {
   if (!menuExist()) {
     menu = new Menu(parent, SWT.BAR);
     menu.setData(MANAGER_KEY, this);
     update(false);
   }
   return menu;
 }
 /**
  * Creates and returns an SWT context menu control for this menu, and installs all registered
  * contributions. Does not create a new control if one already exists.
  *
  * <p>Note that the menu is not expected to be dynamic.
  *
  * @param parent the parent control
  * @return the menu control
  */
 public Menu createContextMenu(Control parent) {
   if (!menuExist()) {
     menu = new Menu(parent);
     menu.setData(MANAGER_KEY, this);
     initializeMenu();
   }
   return menu;
 }
 private void styleMenuItems(final Menu menu) {
   MenuItem[] items = menu.getItems();
   if (items != null && items.length > 0) {
     for (int i = 0; i < items.length; i++) {
       items[i].setData(WidgetUtil.CUSTOM_VARIANT, MENU_BAR_VARIANT);
       Menu subMenu = items[i].getMenu();
       if (subMenu != null) {
         subMenu.setData(WidgetUtil.CUSTOM_VARIANT, MENU_BAR_VARIANT);
         styleMenuItems(subMenu);
       }
     }
   }
 }
  private void createMenu(final MenuManager manager, final ToolItem toolItem) {
    final Menu menu = new Menu(menuParent);
    toolItem.setData(menu);
    menu.setData(WidgetUtil.CUSTOM_VARIANT, MENU_BAR_VARIANT);
    toolItem.addSelectionListener(
        new SelectionAdapter() {
          public void widgetSelected(final SelectionEvent e) {
            // cleanup the menu
            MenuItem[] menuItems = menu.getItems();
            for (int i = 0; i < menuItems.length; i++) {
              menuItems[i].dispose();
            }
            hookMenuToToolItem(manager, menu);
            // set the menu position
            Display display = toolItem.getDisplay();
            Rectangle bounds = toolItem.getBounds();
            int leftIndent = bounds.x;
            int topIndent = bounds.y + bounds.height;
            Point indent = new Point(leftIndent, topIndent);
            Point menuLocation = display.map(toolbar, toolbar.getShell(), indent);
            menu.setLocation(menuLocation);
            // style the menuitems and show the menu
            menu.setData(WidgetUtil.CUSTOM_VARIANT, MENU_BAR_VARIANT);
            styleMenuItems(menu);
            menu.setVisible(true);
          }

          private void hookMenuToToolItem(final MenuManager manager, final Menu menu) {
            IContributionItem[] contribItems = manager.getItems();
            if (contribItems != null && contribItems.length > 0) {
              for (int i = 0; i < contribItems.length; i++) {
                if (i > 0 || !(contribItems[i] instanceof Separator)) {
                  contribItems[i].fill(menu, -1);
                }
              }
            }
          };
        });
  }
  private void openMenuFor(ToolItem item, MPerspective persp) {
    final Menu menu = new Menu(psTB);
    menu.setData(persp);
    if (persp.getParent().getSelectedElement() == persp) {
      addSaveAsItem(menu);
      addResetItem(menu);
    }

    if (persp.isVisible()) {
      addCloseItem(menu);
    }

    new MenuItem(menu, SWT.SEPARATOR);
    // addDockOnSubMenu(menu);
    addShowTextItem(menu);

    Rectangle bounds = item.getBounds();
    Point point = psTB.toDisplay(bounds.x, bounds.y + bounds.height);
    menu.setLocation(point.x, point.y);
    menu.setVisible(true);
    menu.addMenuListener(
        new MenuListener() {

          public void menuHidden(MenuEvent e) {
            psTB.getDisplay()
                .asyncExec(
                    new Runnable() {

                      public void run() {
                        menu.dispose();
                      }
                    });
          }

          public void menuShown(MenuEvent e) {
            // Nothing to do
          }
        });
  }
  /*
   * (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;
  }