/**
   * Add I/O blocks and reconnect them
   *
   * @param childGraph the child graph
   * @param childModel the child graph model
   * @param broken the broken entry
   */
  private void connectChild(
      final XcosDiagram childGraph, final mxGraphModel childModel, Broken broken) {
    childGraph.addCell(broken.getChildBlock());
    childGraph.addCell(broken.getChildLink());

    final mxICell source = broken.getChildTerminal(true);
    final mxICell target = broken.getChildTerminal(false);

    // then connect the link
    mxGraphModel.setTerminals(childModel, broken.getChildLink(), source, target);
  }
  /**
   * Add I/O port and reconnect them
   *
   * @param parentGraph the parent graph
   * @param parentModel the parent graph model
   * @param superBlock the super block
   * @param broken the broken entry
   */
  private void connectParent(
      final XcosDiagram parentGraph,
      final mxGraphModel parentModel,
      final SuperBlock superBlock,
      Broken broken) {
    parentGraph.addCell(broken.getParentPort(), superBlock);
    parentGraph.addCell(broken.getParentLink());

    final mxICell source = broken.getParentTerminal(true);
    final mxICell target = broken.getParentTerminal(false);

    // then connect the link
    mxGraphModel.setTerminals(parentModel, broken.getParentLink(), source, target);
  }
  /**
   * Create child cells and add them to the parent diagram. All links are also reconnected
   *
   * @param parentGraph the parent diagram
   * @param superBlock the superblock
   * @param inSelectionCells the cells in the selection
   * @return the broken descriptor set
   */
  private Collection<Broken> updateParent(
      final XcosDiagram parentGraph,
      final SuperBlock superBlock,
      final Set<Object> inSelectionCells) {
    final Collection<Broken> brokenLinks;
    final mxGraphModel parentModel = (mxGraphModel) parentGraph.getModel();

    parentModel.beginUpdate();
    try {
      /*
       * Add the internal links and fill border links Sort the broken
       * links by position (to perform a good numbering order) and keep
       * only one occurrence of a broken link.
       */
      brokenLinks = new TreeSet<Broken>();
      fillLinks(parentModel, inSelectionCells, brokenLinks);

      /*
       * Disconnect the broken links
       */
      for (Broken broken : brokenLinks) {
        mxGraphModel.setTerminals(parentModel, broken.getParentLink(), null, null);
      }

      /*
       * Add the super block
       */
      parentGraph.addCell(superBlock);

      /*
       * Main broken loop
       */
      // ordering access is : IN, OUT, e_IN, e_OUT
      final int[] ordering = {0, 0, 0, 0};
      for (Broken broken : brokenLinks) {

        // set the ordering
        incrementOrdering(ordering, broken);

        connectParent(parentGraph, parentModel, superBlock, broken);
        connectChild(parentGraph, parentModel, broken);

        /*
         * Update the view
         */
        BlockPositioning.updateBlockView(broken.getChildBlock());
      }
    } finally {
      parentModel.endUpdate();
    }

    return brokenLinks;
  }