/** {@inheritDoc} */
  public Menu getMenu(final Control parent) {
    if (manager == null) {
      manager = new MenuManager();
    }
    Menu menu = manager.getMenu();
    if (menu != null) {
      menu.dispose();
      menu = null;
    }
    manager.removeAll();
    menu = manager.createContextMenu(parent);
    IAction removeAction =
        new Action(
            "Remove Section Set",
            Activator.imageDescriptorFromPlugin(Activator.ID, "/icons/delete.gif")) {

          /** {@inheritDoc} */
          @Override
          public void run() {
            // remove this section descriptor set state from its parent
            sectionSetDescriptorStates.remove(getSectionSetDescriptorState());
            metamodelViewer.refresh();
          }
        };
    manager.add(removeAction);
    manager.add(new Separator(ADD_GROUP));

    return menu;
  }
  /** {@inheritDoc} */
  public Menu getMenu(final Control parent) {
    if (manager == null) {
      manager = new MenuManager();
    }
    Menu menu = manager.getMenu();
    if (menu != null) {
      menu.dispose();
      menu = null;
    }
    manager.removeAll();
    menu = manager.createContextMenu(parent);
    IAction removeAction =
        new Action(
            "Remove Section",
            Activator.imageDescriptorFromPlugin(Activator.ID, "/icons/delete.gif")) {

          /** {@inheritDoc} */
          @Override
          public void run() {
            // remove this section descriptor state from its parent
            if (parent instanceof Tree) {
              TreeItem[] selectedItems = ((Tree) parent).getSelection();
              if (selectedItems.length < 1) {
                Activator.log.warn("Impossible to find the curret selection in the tree");
                return;
              }
              TreeItem selectedItem = selectedItems[0];
              TreeItem parentItem = selectedItem.getParentItem();
              // parent item should be the sectionsetdescriptor set (content holder exactly)
              if (parentItem == null) {
                Activator.log.warn(
                    "Impossible to find the parent for current selection in the tree ");
                return;
              }

              Object parent = parentItem.getData();
              // test the parent is a content holder
              if ((parent instanceof ContentHolder)) {
                SectionSetDescriptorState sectionSetDescriptorState =
                    ((ContentHolder) parent).getSectionSetDescriptorState();
                sectionSetDescriptorState.removeSectionDescriptorState(sectionDescriptorState);
              }
            }
          }
        };
    manager.add(removeAction);
    manager.add(new Separator(ADD_GROUP));

    Class<?> selectionClass = null;
    if (getCurrentMetaclass() != null) {
      selectionClass = getCurrentMetaclass().getInstanceClass();
    }
    final Class<?> finalSelectionClass = selectionClass;
    SectionSetDescriptorState currentSectionSetDescriptorState = getSectionSetDescriptorState();
    if (currentSectionSetDescriptorState == null) {
      return menu;
    }
    final List<ConstraintDescriptorState> constraintDescriptorStates =
        getSectionSetDescriptorState().getConstraintDescriptorStates();

    final int selectionSize = currentSectionSetDescriptorState.getSelectionSize();

    IAction addFragmentAction =
        new Action(
            "Add New Fragment",
            Activator.imageDescriptorFromPlugin(Activator.ID, "/icons/NewFragment.gif")) {

          /** {@inheritDoc} */
          @Override
          public void run() {
            // adds a fragment to the current element
            FragmentDescriptor fragmentDescriptor =
                new FragmentDescriptor(
                    getNewFragmentId(getCurrentMetaclass(), getCurrentStereotype(), selectionSize),
                    new ArrayList<IConstraintDescriptor>(),
                    new ArrayList<ContainerDescriptor>(),
                    getSectionSetDescriptorState().getSelectionSize());
            FragmentDescriptorState fragmentDescriptorState =
                new FragmentDescriptorState(fragmentDescriptor, false);

            // retrieve constraints from current section set
            for (ConstraintDescriptorState state : constraintDescriptorStates) {
              fragmentDescriptorState.addConstraintDescriptorState(state);
            }
            sectionDescriptorState.addFragmentDescriptorState(fragmentDescriptorState);
          }
        };
    manager.appendToGroup(ADD_GROUP, addFragmentAction);
    // adds a fragment to the current element

    IAction addReplacedSectionAction =
        new Action(
            "Add New replaced section",
            Activator.imageDescriptorFromPlugin(Activator.ID, "/icons/NewReplacedSection.gif")) {

          /** {@inheritDoc} */
          @Override
          public void run() {
            // adds a fragment to the current element
            sectionDescriptorState.addReplacedSectionState(
                sectionDescriptorState.new ReplacedSectionState(""));
          }
        };
    manager.appendToGroup(ADD_GROUP, addReplacedSectionAction);

    Map<String, FragmentDescriptor> availableFragmentDescriptors =
        PropertyViewService.getInstance().getAllFragmentDescriptors();

    for (final FragmentDescriptor descriptor : availableFragmentDescriptors.values()) {
      // check constraints
      for (IConstraintDescriptor constraintDescriptor : descriptor.getConstraintDescriptors()) {
        if (constraintDescriptor instanceof ObjectTypeConstraintDescriptor) {
          Class<?> elementClass =
              ((ObjectTypeConstraintDescriptor) constraintDescriptor).getElementClass();
          // check element class is compatible
          if (selectionClass != null && elementClass.isAssignableFrom(selectionClass)) {

            IAction addPredefinedFragmentAction =
                new Action(
                    "Add Predefined " + descriptor.getText(),
                    Activator.imageDescriptorFromPlugin(Activator.ID, "/icons/NewFragment.gif")) {

                  /** {@inheritDoc} */
                  @Override
                  public void run() {
                    // add this fragment descriptor state from its parent
                    if (parent instanceof Tree) {
                      TreeItem[] selectedItems = ((Tree) parent).getSelection();
                      if (selectedItems.length < 1) {
                        Activator.log.warn("Impossible to find the current selection in the tree");
                        return;
                      }
                      PredefinedFragmentDescriptor predefinedFragmentDescriptor =
                          new PredefinedFragmentDescriptor(descriptor.getId());
                      PredefinedFragmentDescriptorState state =
                          new PredefinedFragmentDescriptorState(
                              predefinedFragmentDescriptor, false);
                      sectionDescriptorState.addFragmentDescriptorState(state);
                    }
                  }
                };
            manager.appendToGroup(ADD_GROUP, addPredefinedFragmentAction);
          }
        }
      }
    }

    return menu;
  }