private static void addMenuItemsNew(
      final Menu parentMenu,
      Class<? extends Datatype>[] possibleChildrenTypes,
      MasterDetailTreeNode treeNode,
      TreeViewer treeViewer,
      EngineMaintenanceMultiPageEditViewModelHandlerImpl modelHandler) {
    // Produce elements
    for (Class<? extends Datatype> clazz : possibleChildrenTypes) {

      // create menu entries for each allowed class
      Datatype datatype = EngineElementFactory.create(clazz);
      String imagePath =
          EngineMaintenanceMasterDetailLabelProvider.getInstance().getImage(datatype);
      Image image = ImageProvider.createImage(imagePath);

      new NewDatatypeMenuItem(
          parentMenu,
          treeNode,
          modelHandler,
          datatype,
          treeViewer,
          EngineMaintenanceMultiPageEditViewModelHandlerImpl.ID + "." + clazz.getSimpleName(),
          null,
          image);
    }
  }
 private static Menu createMenu(final Menu parentMenu, final String label, String imagePath) {
   MenuItem newElementMenuIten = new MenuItem(parentMenu, SWT.CASCADE);
   newElementMenuIten.setImage(ImageProvider.createImage(imagePath));
   newElementMenuIten.setText(I18N.i18n(label));
   Menu newElementMenu = new Menu(parentMenu);
   newElementMenuIten.setMenu(newElementMenu);
   return newElementMenu;
 }
  private static void addMenuItemsClone(
      final Menu parentMenu,
      Class<? extends Datatype>[] possibleChildrenTypes,
      MasterDetailTreeNode treeNode,
      TreeViewer treeViewer,
      EngineMaintenanceMultiPageEditViewModelHandlerImpl modelHandler) {
    // Produce elements
    for (Class<? extends Datatype> clazz : possibleChildrenTypes) {

      // create menu entries for each allowed class
      Datatype datatype = EngineElementFactory.create(clazz);
      String imagePath =
          EngineMaintenanceMasterDetailLabelProvider.getInstance().getImage(datatype);
      Image image = ImageProvider.createImage(imagePath);

      HashMap<String, String> variableTextMap = new HashMap<String, String>();
      variableTextMap.put("name", clazz.getSimpleName());

      EngineCloneContentProvider contentProvider = new EngineCloneContentProvider(clazz);
      ILabelProvider inputFieldLabelProvider = null;

      NabuccoTableColumnInfo[] tableColumnInfo =
          new NabuccoTableColumnInfo[] {
            new NabuccoTableColumnInfo(
                "org.nabucco.testautomation.config.ui.rcp.multipage.config.maintainance.model.selectclone.dialog.column.name.name",
                "org.nabucco.testautomation.config.ui.rcp.multipage.config.maintainance.model.selectclone.dialog.column.name.tooltip",
                150,
                SWT.LEFT,
                SWT.LEFT,
                new EngineCloneNameColumnDialogLabelProvider())
          };

      NabuccoTableSorter tableSorter = null;
      EngineCloneDialogTableFilter tableFilter = new EngineCloneDialogTableFilter();
      ElementPickerParameter elementPickerParameter =
          new ElementPickerParameter(
              tableSorter, tableFilter, inputFieldLabelProvider, contentProvider, tableColumnInfo);

      LabelForDialog labelForDialog =
          new LabelForDialog(
              "org.nabucco.testautomation.config.ui.rcp.multipage.config.maintainance.model.title",
              "org.nabucco.testautomation.config.ui.rcp.multipage.config.maintainance.model.message",
              "org.nabucco.testautomation.config.ui.rcp.multipage.config.maintainance.model.shellTitle",
              "org.nabucco.testautomation.config.ui.rcp.multipage.config.maintainance.model.messageTable",
              "org.nabucco.testautomation.config.ui.rcp.multipage.config.maintainance.model.messageCombo",
              "org.nabucco.testautomation.config.ui.rcp.multipage.config.maintainance.model.pathLabel");

      // Clone
      new CloneDatatypeMenuItem(
          parentMenu,
          treeNode,
          modelHandler,
          treeViewer,
          EngineMaintenanceMultiPageEditViewModelHandlerImpl.ID + "." + clazz.getSimpleName(),
          elementPickerParameter,
          labelForDialog,
          variableTextMap,
          image,
          new EngineCloner());
    }
  }
  public static Menu getContextMenu(
      ISelection selection,
      TreeViewer treeViewer,
      EngineMaintenanceMultiPageEditViewModelHandlerImpl modelHandler) {
    Menu result = new Menu(treeViewer.getTree());
    TreeSelection treeSelection = (TreeSelection) selection;
    Object firstElement = treeSelection.getFirstElement();
    if (firstElement instanceof MasterDetailTreeNode) {
      MasterDetailTreeNode treeNode = (MasterDetailTreeNode) firstElement;
      Datatype datatype = treeNode.getDatatype();

      // Menu 'new'
      Menu newElementMenu =
          createMenu(
              result,
              EngineMaintenanceMultiPageEditViewModelHandlerImpl.ID + NEW_ELEMENT,
              "icons/add.png");
      if (datatype instanceof TestEngineConfiguration) {
        Class<? extends Datatype>[] possibleChildrenTypes =
            getPossibleChildrenTypes((TestEngineConfiguration) datatype);

        addMenuItemsNew(newElementMenu, possibleChildrenTypes, treeNode, treeViewer, modelHandler);

        // Menu 'clone'
        Menu cloneElementMenu =
            createMenu(
                result,
                EngineMaintenanceMultiPageEditViewModelHandlerImpl.ID + CLONE_ELEMENT,
                "icons/copy.png");

        addMenuItemsClone(
            cloneElementMenu, possibleChildrenTypes, treeNode, treeViewer, modelHandler);

      } else if (datatype instanceof ProxyConfiguration) {
        Class<? extends Datatype>[] possibleChildrenTypes =
            getPossibleChildrenTypes((ProxyConfiguration) datatype);
        addMenuItemsNew(newElementMenu, possibleChildrenTypes, treeNode, treeViewer, modelHandler);
      } else if (datatype instanceof ConfigurationProperty) {
      } else {
        Activator.getDefault()
            .logError(
                "Error. No children-mapping for type '"
                    + datatype.getClass()
                    + "' in "
                    + EngineDataModelManager.class.getCanonicalName()
                    + " found.");
      }
      if (newElementMenu.getItemCount() == 0) {
        newElementMenu.getParentItem().setEnabled(false);
      }
      // If not root case
      if (treeNode.getParent() != null) {
        // Separator
        new MenuItem(result, SWT.SEPARATOR);

        // Remove
        Image image = ImageProvider.createImage("icons/delete.png");
        new RemoveDatatypeMenuItem(
            result,
            treeNode,
            modelHandler,
            treeViewer,
            EngineMaintenanceMultiPageEditViewModelHandlerImpl.ID + REMOVE,
            null,
            image);
      }
    }
    return result;
  }