@Override
  public Connection create(ICreateConnectionContext context) {
    Connection newConnection = null;
    // get EClasses which should be connected
    StandardNode source = getStandardNode(context.getSourcePictogramElement());
    StandardNode target = getStandardNode(context.getTargetPictogramElement());

    if (source != null && target != null && target != source) {
      // create new business object

      Link l = createLink(source, target);
      if (l == null) {
        return null;
      }

      // add connection for business object
      AddConnectionContext addContext =
          new AddConnectionContext(context.getSourceAnchor(), context.getTargetAnchor());
      addContext.setNewObject(l);
      newConnection = (Connection) getFeatureProvider().addIfPossible(addContext);

      PictogramElement pes = context.getSourcePictogramElement();
      PictogramElement pet = context.getTargetPictogramElement();

      if (source.getEdge().getChildNode().size() == 0) {
        pet.getGraphicsAlgorithm().setX(pes.getGraphicsAlgorithm().getX());
        pet.getGraphicsAlgorithm()
            .setY(pes.getGraphicsAlgorithm().getY() + pes.getGraphicsAlgorithm().getHeight() + 45);
      }
    }

    return newConnection;
  }
Пример #2
0
  /**
   * Initialize a ready to use AddConnectionContext object from a network connection. This method
   * does not add the given connection to the network.
   *
   * <p>The returned IAddConnectionContext is ready to use. The business object has been set to the
   * given connection and the target container has been set to the given diagram.
   *
   * @param fp The IFeatureProviderWithPatterns instance
   * @param diagram The diagram where the connection will be added
   * @param connection The connection
   * @return
   */
  public static IAddConnectionContext getAddConnectionContext(
      final IFeatureProviderWithPatterns fp, final Diagram diagram, final Connection connection) {

    final ILinkService linkServ = Graphiti.getLinkService();

    // retrieve the source and target PictogramElements
    final List<PictogramElement> sourcesPE =
        linkServ.getPictogramElements(diagram, connection.getSource());
    if (sourcesPE == null || sourcesPE.isEmpty()) {
      OrccLogger.warnln(
          "[getAddConnectionContext] Unable to "
              + "retrieve the PictogramElement corresponding to the source "
              + connection.getSource()
              + ".");
      return null;
    }
    final List<PictogramElement> targetsPE =
        linkServ.getPictogramElements(diagram, connection.getTarget());
    if (targetsPE == null || targetsPE.isEmpty()) {
      OrccLogger.warnln(
          "[getAddConnectionContext] Unable to "
              + "retrieve the PictogramElement corresponding to the target "
              + connection.getTarget()
              + ".");
      return null;
    }

    // source/target PictogramElement
    final PictogramElement sourcePe = sourcesPE.get(0);
    final PictogramElement targetPe = targetsPE.get(0);

    final Anchor sourceAnchor, targetAnchor;
    if (PropsUtil.isInputPort(sourcePe)) {
      // Connection from a network port
      final InputNetworkPortPattern spattern =
          (InputNetworkPortPattern) fp.getPatternForPictogramElement(sourcePe);
      sourceAnchor = spattern.getAnchor((AnchorContainer) sourcePe);
    } else {
      // Connection from an instance port
      final InstancePattern spattern = (InstancePattern) fp.getPatternForPictogramElement(sourcePe);
      sourceAnchor = spattern.getAnchorForPort(sourcePe, connection.getSourcePort());
    }

    if (PropsUtil.isOutputPort(targetPe)) {
      // Connection to a network port
      final OutputNetworkPortPattern tpattern =
          (OutputNetworkPortPattern) fp.getPatternForPictogramElement(targetPe);
      targetAnchor = tpattern.getAnchor((AnchorContainer) targetPe);
    } else {
      // Connection to an instance port
      final InstancePattern tpattern = (InstancePattern) fp.getPatternForPictogramElement(targetPe);
      targetAnchor = tpattern.getAnchorForPort(targetPe, connection.getTargetPort());
    }

    final AddConnectionContext result = new AddConnectionContext(sourceAnchor, targetAnchor);
    result.setTargetContainer(diagram);
    result.setNewObject(connection);
    return result;
  }
 @Override
 public Connection create(ICreateConnectionContext context) {
   try {
     A source = getSourceBo(context);
     B target = getTargetBo(context);
     ModelHandler mh = ModelHandler.getInstance(getDiagram());
     AddConnectionContext addContext =
         new AddConnectionContext(context.getSourceAnchor(), context.getTargetAnchor());
     BaseElement flow = createFlow(mh, source, target);
     //			flow.setId(EcoreUtil.generateUUID());
     addContext.setNewObject(flow);
     Connection connection = (Connection) getFeatureProvider().addIfPossible(addContext);
     ModelUtil.setID(flow);
     return connection;
   } catch (IOException e) {
     Activator.logError(e);
   }
   return null;
 }
  // Creates the business object for the relationship
  @Override
  public Connection create(ICreateConnectionContext context) {
    Connection newConnection = null;
    // get TNodeTemplates which should be connected
    TNodeTemplate source = getTNodeTemplate(context.getSourceAnchor());
    TNodeTemplate target = getTNodeTemplate(context.getTargetAnchor());
    if (source != null && target != null) {
      // create new business object
      TRelationshipTemplate newClass = ToscaFactory.eINSTANCE.createTRelationshipTemplate();
      newClass.setName("Relation");
      newClass.setId(("R" + (Integer) newClass.hashCode()).toString());
      newClass.setType(new QName("Bidirected"));
      //      newClass.setType( new QName("Peer - Peer") );
      SourceElementType se = ToscaFactory.eINSTANCE.createSourceElementType();
      se.setRef(source.getId());
      newClass.setSourceElement(se);
      TargetElementType te = ToscaFactory.eINSTANCE.createTargetElementType();
      te.setRef(target.getId());
      newClass.setTargetElement(te);

      ContainerShape sourceContainer = (ContainerShape) context.getSourcePictogramElement();
      Object parentObject =
          getFeatureProvider().getBusinessObjectForPictogramElement(sourceContainer.getContainer());
      TServiceTemplate serviceTemplate = null;
      if (parentObject == null) return null;
      if (parentObject instanceof TServiceTemplate) {
        serviceTemplate = (TServiceTemplate) parentObject;
      }
      TTopologyTemplate topology = null;
      topology = serviceTemplate.getTopologyTemplate();
      topology.getRelationshipTemplate().add(newClass);
      // add connection for business object
      AddConnectionContext addContext =
          new AddConnectionContext(context.getSourceAnchor(), context.getTargetAnchor());
      addContext.setNewObject(newClass);
      newConnection = (Connection) getFeatureProvider().addIfPossible(addContext);
    }
    return newConnection;
  }
  protected Connection createNewConnection(
      ModelHandler mh, ContainerShape oldShape, ContainerShape newShape) {

    Anchor sourceAnchor = LayoutUtil.getCenterAnchor(oldShape);
    Anchor targetAnchor = LayoutUtil.getCenterAnchor(newShape);

    // TODO: Use create features to create connection
    CreateConnectionContext createConnectionContext = new CreateConnectionContext();
    createConnectionContext.setSourcePictogramElement(oldShape);
    createConnectionContext.setTargetPictogramElement(newShape);
    createConnectionContext.setSourceAnchor(sourceAnchor);
    createConnectionContext.setTargetAnchor(targetAnchor);

    FlowNode oldObject = BusinessObjectUtil.getFirstElementOfType(oldShape, FlowNode.class);
    FlowNode newObject = BusinessObjectUtil.getFirstElementOfType(newShape, FlowNode.class);

    AddConnectionContext acc = new AddConnectionContext(sourceAnchor, targetAnchor);
    SequenceFlow flow = mh.createSequenceFlow(oldObject, newObject);
    acc.setNewObject(flow);

    Connection connection = (Connection) getFeatureProvider().addIfPossible(acc);

    return connection;
  }
  private void drawAssociation(Association association, BpmnMemoryModel model) {

    Anchor sourceAnchor = null;
    Anchor targetAnchor = null;
    BaseElement sourceElement = model.getFlowElement(association.getSourceRef());
    if (sourceElement == null) {
      sourceElement = model.getArtifact(association.getSourceRef());
    }
    if (sourceElement == null) {
      return;
    }
    ContainerShape sourceShape =
        (ContainerShape)
            getDiagramTypeProvider()
                .getFeatureProvider()
                .getPictogramElementForBusinessObject(sourceElement);

    if (sourceShape == null) {
      return;
    }

    EList<Anchor> anchorList = sourceShape.getAnchors();
    for (Anchor anchor : anchorList) {
      if (anchor instanceof ChopboxAnchor) {
        sourceAnchor = anchor;
        break;
      }
    }

    BaseElement targetElement = model.getFlowElement(association.getTargetRef());
    if (targetElement == null) {
      targetElement = model.getArtifact(association.getTargetRef());
    }
    if (targetElement == null) {
      return;
    }
    ContainerShape targetShape =
        (ContainerShape)
            getDiagramTypeProvider()
                .getFeatureProvider()
                .getPictogramElementForBusinessObject(targetElement);

    if (targetShape == null) {
      return;
    }

    anchorList = targetShape.getAnchors();
    for (Anchor anchor : anchorList) {
      if (anchor instanceof ChopboxAnchor) {
        targetAnchor = anchor;
        break;
      }
    }

    AddConnectionContext addContext = new AddConnectionContext(sourceAnchor, targetAnchor);

    List<GraphicInfo> bendpointList = new ArrayList<GraphicInfo>();
    if (model.getBpmnModel().getFlowLocationMap().containsKey(association.getId())) {
      List<GraphicInfo> pointList =
          model.getBpmnModel().getFlowLocationGraphicInfo(association.getId());
      if (pointList.size() > 2) {
        for (int i = 1; i < pointList.size() - 1; i++) {
          bendpointList.add(pointList.get(i));
        }
      }
    }

    addContext.putProperty("org.activiti.designer.bendpoints", bendpointList);

    addContext.setNewObject(association);
    getDiagramTypeProvider().getFeatureProvider().addIfPossible(addContext);
  }
  protected void drawMessageFlows(
      final Collection<MessageFlow> messageFlows, final BpmnMemoryModel model) {

    for (final MessageFlow messageFlow : messageFlows) {

      Anchor sourceAnchor = null;
      Anchor targetAnchor = null;
      ContainerShape sourceShape =
          (ContainerShape)
              getDiagramTypeProvider()
                  .getFeatureProvider()
                  .getPictogramElementForBusinessObject(
                      model.getFlowElement(messageFlow.getSourceRef()));

      if (sourceShape == null) {
        continue;
      }

      EList<Anchor> anchorList = sourceShape.getAnchors();
      for (Anchor anchor : anchorList) {
        if (anchor instanceof ChopboxAnchor) {
          sourceAnchor = anchor;
          break;
        }
      }

      ContainerShape targetShape =
          (ContainerShape)
              getDiagramTypeProvider()
                  .getFeatureProvider()
                  .getPictogramElementForBusinessObject(
                      model.getFlowElement(messageFlow.getTargetRef()));

      if (targetShape == null) {
        continue;
      }

      anchorList = targetShape.getAnchors();
      for (Anchor anchor : anchorList) {
        if (anchor instanceof ChopboxAnchor) {
          targetAnchor = anchor;
          break;
        }
      }

      AddConnectionContext addContext = new AddConnectionContext(sourceAnchor, targetAnchor);

      List<GraphicInfo> bendpointList = new ArrayList<GraphicInfo>();
      if (model.getBpmnModel().getFlowLocationMap().containsKey(messageFlow.getId())) {
        List<GraphicInfo> pointList =
            model.getBpmnModel().getFlowLocationGraphicInfo(messageFlow.getId());
        if (pointList.size() > 2) {
          for (int i = 1; i < pointList.size() - 1; i++) {
            bendpointList.add(pointList.get(i));
          }
        }
      }
      addContext.putProperty("org.activiti.designer.bendpoints", bendpointList);
      addContext.putProperty(
          "org.activiti.designer.connectionlabel",
          model.getBpmnModel().getLabelGraphicInfo(messageFlow.getId()));

      addContext.setNewObject(messageFlow);
      getDiagramTypeProvider().getFeatureProvider().addIfPossible(addContext);
    }
  }
  @Override
  public boolean update(IUpdateContext context) {
    _hasDoneChanges = false;

    // retrieve name from business model
    ContainerShape cs = (ContainerShape) context.getPictogramElement();
    Reference reference = (Reference) getBusinessObjectForPictogramElement(cs);

    // remove it if it's gone
    if (!GraphitiInternal.getEmfService().isObjectAlive(reference)) {
      IRemoveContext removeContext = new RemoveContext(context.getPictogramElement());
      final IRemoveFeature removeFeature = getFeatureProvider().getRemoveFeature(removeContext);
      if (removeFeature != null && removeFeature.canRemove(removeContext)) {
        removeFeature.remove(removeContext);
        _hasDoneChanges = removeFeature.hasDoneChanges();
        return true;
      }
    }

    // Set name in pictogram model
    String pictogramName = null;
    Text foundText = GraphitiUtil.findChildGA(cs.getGraphicsAlgorithm(), Text.class);
    if (foundText != null) {
      pictogramName = foundText.getValue();
    }
    String businessName = reference.getName();
    boolean updateNameNeeded =
        ((pictogramName == null && businessName != null)
            || (pictogramName != null && !pictogramName.contentEquals(businessName)));
    if (updateNameNeeded) {
      foundText.setValue(businessName);
      _hasDoneChanges = true;
    }

    // update the wires
    final Set<Contract> existingConnections = getExistingConnections(cs);
    final Anchor anchor = cs.getAnchors().get(0);
    for (ComponentReference promotedReference : reference.getPromote()) {
      if (promotedReference != null && !existingConnections.remove(promotedReference)) {
        for (PictogramElement pe :
            getFeatureProvider().getAllPictogramElementsForBusinessObject(promotedReference)) {
          if (pe instanceof Anchor) {
            AddConnectionContext addContext = new AddConnectionContext((Anchor) pe, anchor);
            addContext.setNewObject(reference);
            updatePictogramElement(getFeatureProvider().addIfPossible(addContext));
            _hasDoneChanges = true;
            break;
          }
        }
      }
    }

    for (Connection connection : new ArrayList<Connection>(anchor.getIncomingConnections())) {
      Object bo = getBusinessObjectForPictogramElement(connection.getStart());
      if (bo == null || existingConnections.remove(bo)) {
        RemoveContext removeContext = new RemoveContext(connection);
        IRemoveFeature removeFeature = getFeatureProvider().getRemoveFeature(removeContext);
        if (removeFeature.canExecute(removeContext)) {
          removeFeature.execute(removeContext);
          _hasDoneChanges = _hasDoneChanges || removeFeature.hasDoneChanges();
        }
      }
    }

    return _hasDoneChanges;
  }
    private void updateConnectionIfNeeded(DataAssociation association, ItemAwareElement value) {
      DiagramEditor diagramEditor = ModelUtil.getDiagramEditor(association);
      if (diagramEditor == null) return;
      boolean hasDoneChanges = false;
      Diagram diagram = diagramEditor.getDiagramTypeProvider().getDiagram();
      IFeatureProvider fp = diagramEditor.getDiagramTypeProvider().getFeatureProvider();
      Shape taskShape = null;
      EObject container = association.eContainer();
      if (container instanceof Activity || container instanceof Event) {
        for (PictogramElement pe :
            Graphiti.getLinkService().getPictogramElements(diagram, container)) {
          if (pe instanceof Shape
              && BusinessObjectUtil.getFirstElementOfType(pe, BPMNShape.class) != null) {
            taskShape = (Shape) pe;
            break;
          }
        }
      }

      Shape dataShape = null;
      if (value instanceof DataObject
          || value instanceof DataObjectReference
          || value instanceof DataStore
          || value instanceof DataStoreReference
          || value instanceof DataInput
          || value instanceof DataOutput) {
        List<PictogramElement> pes =
            Graphiti.getLinkService().getPictogramElements(diagram, (EObject) value);
        for (PictogramElement p : pes) {
          if (BusinessObjectUtil.getFirstElementOfType(p, BPMNShape.class) != null) {
            dataShape = (Shape) p;
            break;
          }
        }
      }

      Connection connection =
          DataAssociationFeatureContainer.findDataAssociation(diagram, association);
      if (connection != null) {
        // There's an existing DataAssociation connection which needs to
        // either be reconnected or deleted, depending on what the combobox
        // selection is.
        if (dataShape != null) {
          // need to reconnect the DataAssociation
          ReconnectionContext rc = null;
          if (association instanceof DataInputAssociation) {
            Point p = GraphicsUtil.createPoint(connection.getStart());
            Anchor a = AnchorUtil.createAnchor((AnchorContainer) dataShape, p);
            rc = new ReconnectionContext(connection, connection.getStart(), a, null);
            rc.setTargetPictogramElement(dataShape);
            rc.setTargetLocation(Graphiti.getPeService().getLocationRelativeToDiagram(a));
            rc.setReconnectType(ReconnectionContext.RECONNECT_SOURCE);
          } else {
            Point p = GraphicsUtil.createPoint(connection.getEnd());
            Anchor a = AnchorUtil.createAnchor(dataShape, p);
            rc = new ReconnectionContext(connection, a, connection.getEnd(), null);
            rc.setTargetPictogramElement(dataShape);
            rc.setTargetLocation(Graphiti.getPeService().getLocationRelativeToDiagram(a));
            rc.setReconnectType(ReconnectionContext.RECONNECT_TARGET);
          }
          IReconnectionFeature rf = fp.getReconnectionFeature(rc);
          if (rf.canReconnect(rc)) {
            rf.reconnect(rc);
            hasDoneChanges = true;
          }
        } else {
          // need to delete the DataAssociation connection
          DeleteContext dc = new DeleteContext(connection);
          connection.getLink().getBusinessObjects().remove(0);
          IDeleteFeature df = fp.getDeleteFeature(dc);
          df.delete(dc);
        }
      } else if (dataShape != null) {
        // There is no existing DataAssociation connection, but the newly selected source or target
        // is some kind of data object shape, so we need to create a connection between the Activity
        // (or Throw/Catch Event) that owns the DataAssociation, and the new data object shape.
        Point p = GraphicsUtil.createPoint((AnchorContainer) dataShape);
        Anchor ownerAnchor = AnchorUtil.createAnchor(taskShape, p);
        p = GraphicsUtil.createPoint(taskShape);
        Anchor peAnchor = AnchorUtil.createAnchor((AnchorContainer) dataShape, p);
        AddConnectionContext ac = null;
        if (association instanceof DataOutputAssociation) {
          ac = new AddConnectionContext(ownerAnchor, peAnchor);
        } else {
          ac = new AddConnectionContext(peAnchor, ownerAnchor);
        }
        ac.putProperty(GraphitiConstants.BUSINESS_OBJECT, association);
        ac.setNewObject(association);
        IAddFeature af = fp.getAddFeature(ac);
        if (af.canAdd(ac)) {
          PictogramElement pe = af.add(ac);
          if (pe instanceof Connection) {
            connection = (Connection) pe;
            hasDoneChanges = true;
          }
        }
      }
      if (hasDoneChanges) {
        FeatureSupport.updateConnection(
            diagramEditor.getDiagramTypeProvider().getFeatureProvider(), connection);
      }
    }