/*
   * (non-Javadoc)
   *
   * @see org.eclipse.papyrus.commands.ICreationCommand#createDiagram(org.eclipse.emf.ecore.resource.Resource, org.eclipse.emf.ecore.EObject,
   * org.eclipse.emf.ecore.EObject, org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype, java.lang.String)
   */
  @Override
  public final Diagram createDiagram(
      ModelSet modelSet, EObject owner, EObject element, ViewPrototype prototype, String name) {
    ICommand createCmd = getCreateDiagramCommand(modelSet, owner, element, prototype, name);
    TransactionalEditingDomain dom = modelSet.getTransactionalEditingDomain();
    CompositeCommand cmd = new CompositeCommand("Create diagram");
    cmd.add(createCmd);
    cmd.add(new OpenDiagramCommand(dom, createCmd));

    try {

      IStatus status =
          CheckedOperationHistory.getInstance().execute(cmd, new NullProgressMonitor(), null);
      if (status.isOK()) {
        CommandResult result = cmd.getCommandResult();
        Object returnValue = result.getReturnValue();

        // CompositeCommands should always return a collection
        if (returnValue instanceof Collection<?>) {
          for (Object returnElement : (Collection<?>) returnValue) {
            if (returnElement instanceof Diagram) {
              return (Diagram) returnElement;
            }
          }
        }
      } else if (status.getSeverity() != IStatus.CANCEL) {
        StatusManager.getManager().handle(status, StatusManager.SHOW);
      }
    } catch (ExecutionException ex) {
      Activator.log.error(ex);
    }

    return null;
  }
  public Object execute(ExecutionEvent event) throws ExecutionException {
    Element selectedElement = getSelection();
    ServicesRegistry registry = null;
    try {
      registry = ServiceUtilsForHandlers.getInstance().getServiceRegistry(event);
    } catch (ServiceException e1) {
      e1.printStackTrace();
    }
    ModelSet modelSet;
    try {
      modelSet = registry.getService(ModelSet.class);
    } catch (ServiceException e) {
      throw new ExecutionException("Can't get ModelSet", e);
    }
    FeatureArchitectureWizard bWizard = new FeatureArchitectureWizard(false);
    WizardDialog wizardDialog = new WizardDialog(new Shell(), bWizard);
    if (wizardDialog.open() == Window.OK) {
      TransactionalEditingDomain dom = modelSet.getTransactionalEditingDomain();
      if (selectedElement instanceof Package) {
        CompleteFeaturesArchitectureSnapshotCommand comd =
            new CompleteFeaturesArchitectureSnapshotCommand(
                dom, (Package) selectedElement, bWizard.getSelectedBundle());
        dom.getCommandStack().execute(comd);
      }
    }

    return null;
  }
  /*
   * (non-Javadoc)
   *
   * @see org.eclipse.papyrus.commands.ICreationCommand#getCreateDiagramCommand(org.eclipse.papyrus.infra.core.resource.ModelSet,
   * org.eclipse.emf.ecore.EObject, org.eclipse.emf.ecore.EObject, org.eclipse.papyrus.infra.viewpoints.policy.ViewPrototype,
   * java.lang.String)
   */
  @Override
  public final ICommand getCreateDiagramCommand(
      final ModelSet modelSet,
      final EObject owner,
      final EObject element,
      final ViewPrototype prototype,
      final String name) {
    // Diagram creation should not change the semantic resource
    final Resource notationResource = NotationUtils.getNotationResource(modelSet);
    final Resource diResource = DiModelUtils.getDiResource(modelSet);

    ArrayList<IFile> modifiedFiles = new ArrayList<IFile>();
    if (notationResource.getURI().isPlatformResource()) {
      modifiedFiles.add(
          ResourcesPlugin.getWorkspace()
              .getRoot()
              .getFile(new Path(notationResource.getURI().toPlatformString(true))));
    }
    if (diResource.getURI().isPlatformResource()) {
      modifiedFiles.add(
          ResourcesPlugin.getWorkspace()
              .getRoot()
              .getFile(new Path(diResource.getURI().toPlatformString(true))));
    }

    return new AbstractTransactionalCommand(
        modelSet.getTransactionalEditingDomain(),
        Messages.AbstractPapyrusGmfCreateDiagramCommandHandler_CreateDiagramCommandLabel,
        modifiedFiles) {

      private Diagram diagram = null;

      private EObject diagramElement = null;

      private EObject diagramOwner = null;

      @Override
      protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info)
          throws ExecutionException {
        Creator creator = new Creator(modelSet, owner, element, prototype, name);
        try {
          CommandResult commandResult = creator.createDiagram();
          if (!commandResult.getStatus().isOK()) {
            return commandResult;
          }

          diagram = (Diagram) commandResult.getReturnValue();
          diagramElement = diagram.getElement();
          diagramOwner = DiagramUtils.getOwner(diagram);
          return commandResult;
        } catch (ServiceException e) {
          Activator.log.error(e);
        }
        return CommandResult.newErrorCommandResult("Error during diagram creation");
      }

      @Override
      protected IStatus doUndo(IProgressMonitor monitor, IAdaptable info)
          throws ExecutionException {
        // the undo corresponds to a destroy diagram command
        // during diagram creation no adapters are set to the diagram so the setElement is not
        // registered
        // to remove the cross reference using the element reference it is better to use the destroy
        // element command
        // DestroyElementPapyrusCommand depc = (diagram != null) ? new
        // DestroyElementPapyrusCommand(new DestroyElementRequest(diagram, false)) : null;
        IStatus status = super.doUndo(monitor, info);
        diagram.setElement(null);
        return status;
      }

      @Override
      protected IStatus doRedo(IProgressMonitor monitor, IAdaptable info)
          throws ExecutionException {
        diagram.setElement(diagramElement);
        DiagramUtils.setOwner(diagram, diagramOwner);
        IStatus status = super.doRedo(monitor, info);
        return status;
      }
    };
  }