/**
  * Creates a top-level menu and adds it to the Riena menu bar.
  *
  * @param menuManager
  * @param listener
  */
 private ToolItem createAndAddMenu(
     final MenuManager menuManager, final ToolBarMenuListener listener) {
   if (menuManager.isVisible()) {
     final ToolItem toolItem = new ToolItem(menuToolBar, SWT.CHECK);
     toolItem.setText(menuManager.getMenuText());
     final MenuManagerHelper helper = new MenuManagerHelper();
     final Menu menu = helper.createMenu(menuToolBar, toolItem, menuManager);
     toolItem.setData(MENU_DATA_KEY, menu);
     calcSize(menuCoolItem);
     toolItem.addSelectionListener(listener);
     return toolItem;
   }
   return null;
 }
  /**
   * @param menuManager
   * @param menuModel
   */
  public void reconcileManagerToModel(MenuManager menuManager, MMenu menuModel) {
    List<MMenuElement> modelChildren = menuModel.getChildren();

    HashSet<MOpaqueMenuItem> oldModelItems = new HashSet<MOpaqueMenuItem>();
    HashSet<MOpaqueMenu> oldMenus = new HashSet<MOpaqueMenu>();
    HashSet<MOpaqueMenuSeparator> oldSeps = new HashSet<MOpaqueMenuSeparator>();
    for (MMenuElement itemModel : modelChildren) {
      if (itemModel instanceof MOpaqueMenuSeparator) {
        oldSeps.add((MOpaqueMenuSeparator) itemModel);
      } else if (itemModel instanceof MOpaqueMenuItem) {
        oldModelItems.add((MOpaqueMenuItem) itemModel);
      } else if (itemModel instanceof MOpaqueMenu) {
        oldMenus.add((MOpaqueMenu) itemModel);
      }
    }

    IContributionItem[] items = menuManager.getItems();
    for (int src = 0, dest = 0; src < items.length; src++, dest++) {
      IContributionItem item = items[src];
      if (item instanceof MenuManager) {
        MenuManager childManager = (MenuManager) item;
        MMenu childModel = getMenuModel(childManager);
        if (childModel == null) {
          MMenu legacyModel = MenuFactoryImpl.eINSTANCE.createOpaqueMenu();
          legacyModel.setElementId(childManager.getId());
          legacyModel.setVisible(childManager.isVisible());
          linkModelToManager(legacyModel, childManager);
          modelChildren.add(dest, legacyModel);
          reconcileManagerToModel(childManager, legacyModel);
        } else {
          if (childModel instanceof MOpaqueMenu) {
            oldMenus.remove(childModel);
          }
          if (modelChildren.size() > dest) {
            if (modelChildren.get(dest) != childModel) {
              modelChildren.remove(childModel);
              modelChildren.add(dest, childModel);
            }
          } else {
            modelChildren.add(childModel);
          }
        }
      } else if (item.isSeparator() || item.isGroupMarker()) {
        MMenuElement menuElement = getMenuElement(item);
        if (menuElement == null) {
          MOpaqueMenuSeparator legacySep = MenuFactoryImpl.eINSTANCE.createOpaqueMenuSeparator();
          legacySep.setElementId(item.getId());
          legacySep.setVisible(item.isVisible());
          legacySep.setOpaqueItem(item);
          linkModelToContribution(legacySep, item);
          modelChildren.add(dest, legacySep);
        } else if (menuElement instanceof MOpaqueMenuSeparator) {
          MOpaqueMenuSeparator legacySep = (MOpaqueMenuSeparator) menuElement;
          oldSeps.remove(legacySep);
          if (modelChildren.size() > dest) {
            if (modelChildren.get(dest) != legacySep) {
              modelChildren.remove(legacySep);
              modelChildren.add(dest, legacySep);
            }
          } else {
            modelChildren.add(legacySep);
          }
        }
      } else {
        MMenuElement menuElement = getMenuElement(item);
        if (menuElement == null) {
          MOpaqueMenuItem legacyItem = MenuFactoryImpl.eINSTANCE.createOpaqueMenuItem();
          legacyItem.setElementId(item.getId());
          legacyItem.setVisible(item.isVisible());
          legacyItem.setOpaqueItem(item);
          linkModelToContribution(legacyItem, item);
          modelChildren.add(dest, legacyItem);
        } else if (menuElement instanceof MOpaqueMenuItem) {
          MOpaqueMenuItem legacyItem = (MOpaqueMenuItem) menuElement;
          oldModelItems.remove(legacyItem);
          if (modelChildren.size() > dest) {
            if (modelChildren.get(dest) != legacyItem) {
              modelChildren.remove(legacyItem);
              modelChildren.add(dest, legacyItem);
            }
          } else {
            modelChildren.add(legacyItem);
          }
        }
      }
    }
    if (!oldModelItems.isEmpty()) {
      modelChildren.removeAll(oldModelItems);
      for (MOpaqueMenuItem model : oldModelItems) {
        clearModelToContribution(model, (IContributionItem) model.getOpaqueItem());
      }
    }
    if (!oldMenus.isEmpty()) {
      modelChildren.removeAll(oldMenus);
      for (MOpaqueMenu oldMenu : oldMenus) {
        MenuManager oldManager = getManager(oldMenu);
        clearModelToManager(oldMenu, oldManager);
      }
    }
    if (!oldSeps.isEmpty()) {
      modelChildren.removeAll(oldSeps);
      for (MOpaqueMenuSeparator model : oldSeps) {
        clearModelToContribution(model, (IContributionItem) model.getOpaqueItem());
      }
    }
  }