/**
   * If the selected Fig is a FigAssociation (an edge) then convert it to a FigNodeAssociation.
   *
   * @param fig the select end Fig
   * @return the fig converted to a FigNode
   */
  private FigNode convertToFigNode(Fig fig) {
    if (fig instanceof FigEdgePort) {
      fig = fig.getGroup();
    }
    if (!(fig instanceof FigAssociation)) {
      return (FigNode) fig;
    }
    final FigAssociation figAssociation = (FigAssociation) fig;
    final int x = figAssociation.getEdgePort().getX();
    final int y = figAssociation.getEdgePort().getY();
    final Object association = fig.getOwner();
    final FigNode originalEdgePort = figAssociation.getEdgePort();

    // Detach any edges (such as comment edges) already attached
    // to the FigAssociation before the FigAssociation is removed.
    // They'll later be re-attached to the new FigNodeAssociation
    final Collection<FigEdge> existingEdges = originalEdgePort.getEdges();
    for (FigEdge edge : existingEdges) {
      originalEdgePort.removeFigEdge(edge);
    }
    figAssociation.removeFromDiagram();

    // Create the new FigNodeAssociation and locate it.
    final MutableGraphModel gm = (MutableGraphModel) editor.getGraphModel();
    gm.addNode(association);
    final LayerPerspective lay = (LayerPerspective) editor.getLayerManager().getActiveLayer();
    final List associationFigs = lay.presentationsFor(association);
    associationFigs.remove(figAssociation);
    final FigNodeAssociation figNode = (FigNodeAssociation) associationFigs.get(0);

    figNode.setLocation(x - figNode.getWidth() / 2, y - figNode.getHeight() / 2);
    editor.add(figNode);
    editor.getSelectionManager().deselectAll();

    // Add the association ends to the graph model
    final Collection<Object> associationEnds = Model.getFacade().getConnections(association);
    for (Object associationEnd : associationEnds) {
      gm.addEdge(associationEnd);
    }
    // Add the edges (such as comment edges) that were on the old
    // FigAssociation to our new FigNodeAssociation and make sure they are
    // positioned correctly.
    for (FigEdge edge : existingEdges) {
      if (edge.getDestFigNode() == originalEdgePort) {
        edge.setDestFigNode(figNode);
        edge.setDestPortFig(figNode);
      }
      if (edge.getSourceFigNode() == originalEdgePort) {
        edge.setSourceFigNode(figNode);
        edge.setSourcePortFig(figNode);
      }
    }
    figNode.updateEdges();

    return figNode;
  }
  /**
   * Create an edge of the given type and connect it to the given nodes.
   *
   * @param graphModel the graph model in which to create the connection element
   * @param edgeType the UML object type of the connection
   * @param sourceFig the FigNode for the source element
   * @param destFig the FigNode for the destination element
   * @return The FigEdge representing the newly created model element
   */
  @Override
  protected FigEdge buildConnection(
      MutableGraphModel graphModel, Object edgeType, Fig sourceFig, Fig destFig) {
    try {
      Object associationEnd =
          Model.getUmlFactory()
              .buildConnection(
                  edgeType, sourceFig.getOwner(), null, destFig.getOwner(), null, null, null);

      final FigNode sourceFigNode = convertToFigNode(sourceFig);
      final FigNode destFigNode = convertToFigNode(destFig);

      graphModel.addEdge(associationEnd);

      setNewEdge(associationEnd);

      // Calling connect() will add the edge to the GraphModel and
      // any LayerPersectives on that GraphModel will get a
      // edgeAdded event and will add an appropriate FigEdge
      // (determined by the GraphEdgeRenderer).

      if (getNewEdge() != null) {
        sourceFigNode.damage();
        destFigNode.damage();
        Layer lay = editor.getLayerManager().getActiveLayer();
        FigEdge fe = (FigEdge) lay.presentationFor(getNewEdge());
        _newItem.setLineColor(Color.black);
        fe.setFig(_newItem);
        fe.setSourcePortFig(sourceFigNode);
        fe.setSourceFigNode(sourceFigNode);
        fe.setDestPortFig(destFigNode);
        fe.setDestFigNode(destFigNode);
        return fe;
      } else {
        return null;
      }
    } catch (IllegalModelElementConnectionException e) {
      // We have already confirmed the connection is valid
      return null;
    }
  }
 private void setSourcePort(FigEdge edge, FigNode source) {
   edge.setSourcePortFig(source);
   edge.setSourceFigNode(source);
 }