Example #1
0
  @Override
  protected CommandResult doExecuteWithResult(
      IProgressMonitor progressMonitor, org.eclipse.core.runtime.IAdaptable info)
      throws ExecutionException {
    CommandResult cmdResult = super.doExecuteWithResult(progressMonitor, info);
    if (!cmdResult.getStatus().isOK()) {
      if (cmdResult.getStatus().getSeverity() != IStatus.CANCEL) {
        Activator.log.error(cmdResult.getStatus().getException());
      }
      return cmdResult;
    }

    Object returnValue = cmdResult.getReturnValue();
    if (returnValue instanceof List<?>) {
      _selectedCmd =
          (Command)
              ((List<?>) returnValue)
                  .get(((List<?>) returnValue).size() - 1); // Returns the last command
    } else {
      _selectedCmd = (Command) cmdResult.getReturnValue();
    }
    Assert.isTrue(_selectedCmd != null && _selectedCmd.canExecute());
    _selectedCmd.execute();

    return CommandResult.newOKCommandResult();
  }
  /*
   * (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;
  }
 private EObject create(EObject origin, EReference reference, String typeID) {
   IElementType itype = ElementTypeRegistry.getInstance().getType(typeID);
   CreateElementRequest request = new CreateElementRequest(origin, itype, reference);
   ICommand command = service.getEditCommand(request);
   IStatus status = null;
   try {
     status = command.execute(null, null);
   } catch (ExecutionException e) {
     return null;
   }
   if (!status.isOK()) {
     return null;
   }
   CommandResult result = command.getCommandResult();
   if (result == null) {
     return null;
   }
   return (EObject) result.getReturnValue();
 }