/*
  * (non-Javadoc)
  *
  * @see org.eclipse.papyrus.commands.ICreationCommand#getCreateDiagramCommand(org.eclipse.papyrus.infra.core.resource.ModelSet,
  * org.eclipse.emf.ecore.EObject, java.lang.String)
  */
 @Override
 public final ICommand getCreateDiagramCommand(ModelSet modelSet, EObject owner, String name) {
   ViewPrototype proto = ViewPrototype.get(getCreatedDiagramType(), owner, owner);
   if (proto == null) {
     return null;
   }
   return getCreateDiagramCommand(modelSet, owner, owner, proto, name);
 }
 /**
  * Overridable method that effectively create the diagram with the given validated parameters
  *
  * @param diagramResource the diagram resource
  * @param owner the diagram's owner
  * @param element the diagram's model element
  * @param prototype the diagram's prototype
  * @param name the diagram's name
  * @return the created diagram, or <code>null</code> if the creation failed
  */
 protected Diagram doCreateDiagram(
     Resource diagramResource,
     EObject owner,
     EObject element,
     ViewPrototype prototype,
     String name) {
   // create diagram
   Diagram diagram =
       ViewService.createDiagram(element, getDiagramNotationID(), getPreferenceHint());
   if (diagram != null) {
     diagram.setName(name);
     diagram.setElement(element);
     DiagramUtils.setOwner(diagram, owner);
     if (!prototype.isNatural()) {
       DiagramUtils.setPrototype(diagram, prototype);
     }
     initializeDiagram(diagram);
     diagramResource.getContents().add(diagram);
   }
   return diagram;
 }
    private CommandResult createDiagram() throws ServiceException {
      Resource modelResource = UmlUtils.getUmlResource(modelSet);
      Resource notationResource = getNotationResource(modelSet, owner, element);
      if (notationResource == null) {
        return CommandResult.newErrorCommandResult(
            "Cannot create a diagram on the selected element (ReadOnly?)");
      }
      Resource diResource = DiModelUtils.getDiResource(modelSet);

      if (owner == null) {
        owner = getRootElement(modelResource);
        attachModelToResource(owner, modelResource);
      }

      service = ElementEditServiceUtils.getCommandProvider(owner);
      if (service == null) {
        // Something isn't right ...
        return null;
      }

      try {
        clientContext = TypeContext.getContext();
      } catch (ServiceException e) {
        Activator.log.error(e);
      }
      if (clientContext == null) {
        // Something isn't right ...
        return null;
      }

      rule = PolicyChecker.getCurrent().getOwningRuleFor(prototype, owner);
      if (rule == null) {
        // Something isn't right ...
        return null;
      }

      element = owner;
      if (rule.getNewModelPath() != null) {
        // We have a path for the root auto-creation
        for (ModelAutoCreate auto : rule.getNewModelPath()) {
          EReference ref = auto.getFeature();
          String type = auto.getCreationType();
          if (ref.isMany()) {
            element = create(element, ref, type);
          } else {
            EObject temp = (EObject) element.eGet(ref);
            if (temp != null) {
              element = temp;
            } else {
              element = create(element, ref, type);
            }
          }
        }
      }

      if (rule.getSelectDiagramRoot() != null) {
        // We have a path for the root auto-selection
        for (RootAutoSelect auto : rule.getSelectDiagramRoot()) {
          EReference ref = auto.getFeature();
          element = (EObject) element.eGet(ref);
        }
      }

      if (name == null) {
        name =
            openDiagramNameDialog(
                prototype.isNatural()
                    ? getDefaultDiagramName()
                    : "New" + prototype.getLabel().replace(" ", ""));
      }
      // canceled
      if (name == null) {
        return CommandResult.newCancelledCommandResult();
      }

      Diagram diagram = doCreateDiagram(notationResource, owner, element, prototype, name);

      if (diagram != null) {
        IPageManager pageManager =
            ServiceUtilsForResource.getInstance().getIPageManager(diResource);
        pageManager.addPage(diagram);
        return CommandResult.newOKCommandResult(diagram);
      }

      return CommandResult.newCancelledCommandResult();
    }