public PictogramElement add(IAddContext context) {
    IAddConnectionContext addConContext = (IAddConnectionContext) context;
    EActionLink addedEReference = (EActionLink) context.getNewObject();
    IPeCreateService peCreateService = Graphiti.getPeCreateService();

    // CONNECTION WITH POLYLINE
    Connection connection = peCreateService.createFreeFormConnection(getDiagram());
    connection.setStart(addConContext.getSourceAnchor());
    connection.setEnd(addConContext.getTargetAnchor());

    IGaService gaService = Graphiti.getGaService();
    Polyline polyline = gaService.createPolyline(connection);
    polyline.setLineWidth(3);
    polyline.setForeground(manageColor(IColorConstant.LIGHT_ORANGE));

    // create link and wire it
    link(connection, addedEReference);

    // add dynamic text decorator for the association name
    ConnectionDecorator textDecorator =
        peCreateService.createConnectionDecorator(connection, true, 0.5, true);
    Text text = gaService.createText(textDecorator);
    // createDefaultText(textDecorator);
    text.setForeground(manageColor(IColorConstant.BLACK));
    gaService.setLocation(text, 10, 0);
    text.setValue("has the action");

    return connection;
  }
  public static void setContainerChildrenVisible(
      IFeatureProvider fp, ContainerShape container, boolean visible) {
    List<PictogramElement> list = new ArrayList<PictogramElement>();
    list.addAll(container.getChildren());
    for (PictogramElement pe : list) {
      if (ShapeDecoratorUtil.isActivityBorder(pe)) continue;

      if (ShapeDecoratorUtil.isEventSubProcessDecorator(pe)) {
        pe.setVisible(!visible);
      } else pe.setVisible(visible);

      if (visible) FeatureSupport.updateLabel(fp, pe, null);
      if (pe instanceof AnchorContainer) {
        AnchorContainer ac = (AnchorContainer) pe;
        for (Anchor a : ac.getAnchors()) {
          for (Connection c : a.getOutgoingConnections()) {
            c.setVisible(visible);
            if (visible) FeatureSupport.updateLabel(fp, c, null);
            for (ConnectionDecorator decorator : c.getConnectionDecorators()) {
              decorator.setVisible(visible);
            }
          }
        }
      }
    }
  }
 private Set<Contract> getExistingConnections(AnchorContainer container) {
   Set<Contract> existingConnections = new LinkedHashSet<Contract>();
   for (Anchor anchor : container.getAnchors()) {
     for (Connection connection : anchor.getIncomingConnections()) {
       Object bo = getBusinessObjectForPictogramElement(connection.getStart());
       if (bo instanceof Contract || bo == null) {
         existingConnections.add((Contract) bo);
       }
     }
   }
   return existingConnections;
 }
  public static void updateDIEdge(Diagram diagram, Connection connection, Class clazz) {
    try {
      ModelHandler modelHandler =
          ModelHandlerLocator.getModelHandler(
              connection.getLink().getBusinessObjects().get(0).eResource());

      EObject be = BusinessObjectUtil.getFirstElementOfType(connection, clazz);
      BPMNEdge edge = (BPMNEdge) modelHandler.findDIElement(diagram, (BaseElement) be);
      Point point = DcFactory.eINSTANCE.createPoint();

      List<Point> waypoint = edge.getWaypoint();
      waypoint.clear();

      GraphicsAlgorithm graphicsAlgorithm = connection.getStart().getGraphicsAlgorithm();
      // FIXME connections must create anchors!!!
      if (graphicsAlgorithm != null) {
        point.setX(graphicsAlgorithm.getX());
        point.setY(graphicsAlgorithm.getY());
      } else {
        point.setX(connection.getStart().getParent().getGraphicsAlgorithm().getX());
        point.setY(connection.getStart().getParent().getGraphicsAlgorithm().getY());
      }
      waypoint.add(point);

      if (connection instanceof FreeFormConnectionImpl) {
        FreeFormConnectionImpl freeForm = (FreeFormConnectionImpl) connection;
        EList<org.eclipse.graphiti.mm.algorithms.styles.Point> bendpoints =
            freeForm.getBendpoints();
        for (org.eclipse.graphiti.mm.algorithms.styles.Point bp : bendpoints) {
          addBendPoint(freeForm, point);
        }
      }

      point = DcFactory.eINSTANCE.createPoint();
      graphicsAlgorithm = connection.getEnd().getGraphicsAlgorithm();
      if (graphicsAlgorithm != null) {
        point.setX(graphicsAlgorithm.getX());
        point.setY(graphicsAlgorithm.getY());
      } else {
        point.setX(connection.getEnd().getParent().getGraphicsAlgorithm().getX());
        point.setY(connection.getEnd().getParent().getGraphicsAlgorithm().getY());
      }
      waypoint.add(point);

    } catch (IOException e) {
      Activator.logError(e);
    }
  }
  @Override
  public PictogramElement add(IAddContext context) {
    IPeService peService = Graphiti.getPeService();
    IGaService gaService = Graphiti.getGaService();

    BaseElement element = (BaseElement) context.getNewObject();
    IAddConnectionContext addConContext = (IAddConnectionContext) context;

    Connection connection = peService.createFreeFormConnection(getDiagram());

    Object importProp = context.getProperty(DIImport.IMPORT_PROPERTY);
    if (importProp != null && (Boolean) importProp) {
      connection.setStart(addConContext.getSourceAnchor());
      connection.setEnd(addConContext.getTargetAnchor());
    } else {
      ContainerShape sourceContainer =
          (ContainerShape) addConContext.getSourceAnchor().eContainer();
      ContainerShape targetContainer =
          (ContainerShape) addConContext.getTargetAnchor().eContainer();
      Tuple<FixPointAnchor, FixPointAnchor> anchors =
          AnchorUtil.getSourceAndTargetBoundaryAnchors(
              sourceContainer, targetContainer, connection);

      connection.setStart(anchors.getFirst());
      connection.setEnd(anchors.getSecond());
    }

    if (ModelUtil.hasName(element)) {
      ConnectionDecorator labelDecorator =
          Graphiti.getPeService().createConnectionDecorator(connection, true, 0.5, true);
      Text text = gaService.createText(labelDecorator, ModelUtil.getName(element));
      peService.setPropertyValue(
          labelDecorator, UpdateBaseElementNameFeature.TEXT_ELEMENT, Boolean.toString(true));
      text.setStyle(StyleUtil.getStyleForText(getDiagram()));
    }

    Polyline connectionLine = gaService.createPolyline(connection);
    connectionLine.setForeground(manageColor(StyleUtil.CLASS_FOREGROUND));

    decorateConnectionLine(connectionLine);

    createDIEdge(connection, element);
    createConnectionDecorators(connection);
    hook(addConContext, connection, element);

    return connection;
  }
  @Override
  public PictogramElement add(IAddContext context) {

    IAddConnectionContext addConContext = (IAddConnectionContext) context;
    IPeCreateService peCreateService = Graphiti.getPeCreateService();
    IGaService gaService = Graphiti.getGaService();

    Connection connection = peCreateService.createFreeFormConnection(getDiagram());
    connection.setStart(addConContext.getSourceAnchor());
    connection.setEnd(addConContext.getTargetAnchor());

    Polyline polyline = gaService.createPlainPolyline(connection);
    polyline.setForeground(manageColor(IColorConstant.BLACK));

    // TODO: enable the link to the domain object
    // Object addedDomainObjectConnection = context.getNewObject();
    // link(connection, addedDomainObjectConnection);

    return connection;
  }
  @Override
  public PictogramElement add(IAddContext context) {
    IPeService peService = Graphiti.getPeService();

    BaseElement element = (BaseElement) context.getNewObject();
    IAddConnectionContext addConContext = (IAddConnectionContext) context;

    Connection connection = peService.createFreeFormConnection(getDiagram());

    Object importProp = context.getProperty(DIImport.IMPORT_PROPERTY);
    if (importProp != null && (Boolean) importProp) {
      connection.setStart(addConContext.getSourceAnchor());
      connection.setEnd(addConContext.getTargetAnchor());
    } else {
      ContainerShape sourceContainer =
          (ContainerShape) addConContext.getSourceAnchor().eContainer();
      ContainerShape targetContainer =
          (ContainerShape) addConContext.getTargetAnchor().eContainer();
      Tuple<FixPointAnchor, FixPointAnchor> anchors =
          AnchorUtil.getSourceAndTargetBoundaryAnchors(
              sourceContainer, targetContainer, connection);

      connection.setStart(anchors.getFirst());
      connection.setEnd(anchors.getSecond());
    }

    IGaService gaService = Graphiti.getGaService();
    Polyline connectionLine = gaService.createPolyline(connection);
    connectionLine.setForeground(manageColor(StyleUtil.CLASS_FOREGROUND));

    decorateConnectionLine(connectionLine);

    createDIEdge(connection, element);
    createConnectionDecorators(connection);
    hook(addConContext, connection, element);

    return connection;
  }
  @Override
  public PictogramElement add(IAddContext context) {
    // TODO Auto-generated method stub
    IAddConnectionContext addConContext = (IAddConnectionContext) context;
    AdviceEdge edge = (AdviceEdge) context.getNewObject();
    IPeCreateService peCreateService = Graphiti.getPeCreateService();

    Connection connection = peCreateService.createFreeFormConnection(getDiagram());
    connection.setStart(addConContext.getSourceAnchor());
    connection.setEnd(addConContext.getTargetAnchor());

    IGaService gaService = Graphiti.getGaService();
    Polyline polyline = gaService.createPlainPolyline(connection);
    polyline.setStyle(StyleUtil.getStyleForElement(getDiagram()));
    polyline.setLineStyle(LineStyle.DOT);

    link(connection, edge);

    // add dynamic text decorator for the reference name
    ConnectionDecorator textDecorator =
        peCreateService.createConnectionDecorator(connection, true, 0.5, true);
    Text text = gaService.createPlainText(textDecorator);
    text.setStyle(StyleUtil.getStyleForTextDecorator((getDiagram())));
    gaService.setLocation(text, 10, 0);

    text.setValue(edge.getAdvicetype().toString());

    ConnectionDecorator cd;
    cd = peCreateService.createConnectionDecorator(connection, false, 1.0, true);
    int[] xy = {getPointCutR() * 2, 0, 0, -getPointCutR() * 2, 0, 0, 0, getPointCutR() * 2};
    Polygon polygon = Graphiti.getGaCreateService().createPolygon(cd, xy);
    polygon.setStyle(StyleUtil.getStyleForElement(getDiagram()));
    polygon.setLineStyle(LineStyle.DOT);

    return connection;
  }
  @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);
      }
    }