public Command getCreateLinkCommand(View source, View target, GenCommonBase linkType) {
      IElementType metamodelType = getElementType(linkType);
      CreateRelationshipRequest relationShipReq = new CreateRelationshipRequest(metamodelType);

      ConnectionViewAndElementDescriptor desc =
          new ConnectionViewAndElementDescriptor(
              new CreateElementRequestAdapter(relationShipReq),
              metamodelType instanceof IHintedType
                  ? ((IHintedType) metamodelType).getSemanticHint()
                  : "",
              getDefaultPreferencesHint());

      CreateConnectionViewAndElementRequest req = new CreateConnectionViewAndElementRequest(desc);
      req.setType(RequestConstants.REQ_CONNECTION_START);

      EditPart sourceEditPart = findEditPart(source);
      req.setSourceEditPart(sourceEditPart);
      // Note: initializes the sourceCommand in the request
      Command sourceCmd = sourceEditPart.getCommand(req);
      if (sourceCmd == null || !sourceCmd.canExecute()) {
        return null;
      }

      EditPart targetEditPart = target != null ? findEditPart(target) : null;
      if (targetEditPart != null) {
        req.setType(RequestConstants.REQ_CONNECTION_END);
        req.setTargetEditPart(targetEditPart);
        req.setLocation(new Point(0, 0));
        sourceEditPart.getCommand(req);
        Command targetCmd = targetEditPart.getCommand(req);
        return targetCmd;
      }
      return null;
    }
コード例 #2
0
  /**
   * return commands to transform a binary association to n-ary association or allow adding
   * branches.
   *
   * @param createConnectionViewAndElementRequest the create connection view and element request
   * @param command that will contains subcommands
   * @return the command
   */
  public Command getCommand(
      CreateConnectionViewAndElementRequest createConnectionViewAndElementRequest,
      Command command) {
    // 0. get source and target type
    command = new CompoundCommand();
    EditPart sourceEditPart = createConnectionViewAndElementRequest.getSourceEditPart();
    EditPart targetEditPart = createConnectionViewAndElementRequest.getTargetEditPart();

    // if the the source or the target is a node association the goal is
    // to create only one branch
    if ((((View) sourceEditPart.getModel()).getType() == "" + AssociationNodeEditPart.VISUAL_ID)) {
      return getBranchAssociationCommand(createConnectionViewAndElementRequest, command);
    }

    if ((((View) targetEditPart.getModel()).getType() == "" + AssociationNodeEditPart.VISUAL_ID)) {
      return UnexecutableCommand.INSTANCE;
    }
    // the source or the target has to be different of a dependency branch
    if (sourceEditPart instanceof AssociationBranchEditPart) {
      GraphicalEditPart associationNodeEditPart =
          lookForAssociationNodeEditPart((AssociationBranchEditPart) sourceEditPart);

      if (associationNodeEditPart != null) {
        createConnectionViewAndElementRequest.setSourceEditPart(associationNodeEditPart);
        return getBranchAssociationCommand(createConnectionViewAndElementRequest, command);

      } else {
        return UnexecutableCommand.INSTANCE;
      }
    }
    // the source or the target has to be different of a dependency branch
    if (targetEditPart instanceof AssociationBranchEditPart) {
      return UnexecutableCommand.INSTANCE;
    }

    // if not this a transformation of simple dependency to multiDependency
    return getAssociationToMultiAssociationCommand(createConnectionViewAndElementRequest, command);
  }