/**
   * Layouts all combined fragments of this view. The width is determined by the position of the
   * covered lifelines.
   */
  private void layoutCombinedFragments() {
    for (Entry<CombinedFragment, CombinedFragmentView> entry : combinedFragments.entrySet()) {
      CombinedFragmentView combinedFragmentView = entry.getValue();

      float minX = Float.MAX_VALUE;
      float maxX = -1;

      for (Lifeline lifeline : entry.getKey().getCovered()) {
        LifelineView lifelineView = lifelines.get(lifeline);

        Vector3D position = lifelineView.getPosition(TransformSpace.GLOBAL);
        minX = Math.min(minX, position.getX());
        maxX = Math.max(maxX, position.getX() + lifelineView.getWidth() + LIFEINE_SPACING);

        /**
         * Take into consideration the size of other fragments as well, since they could be quite
         * wide.
         */
        for (InteractionFragment fragment : lifeline.getCoveredBy()) {
          /**
           * Check whether the fragment is part of this combined fragment in its containment
           * hierarchy.
           */
          if (EcoreUtil.isAncestor(entry.getKey(), fragment)) {
            RamRectangleComponent fragmentView = fragments.get(fragment);
            if (fragmentView != null) {
              Vector3D fragmentPosition = fragmentView.getPosition(TransformSpace.GLOBAL);
              maxX =
                  Math.max(
                      maxX, fragmentPosition.getX() + fragmentView.getWidth() + LIFEINE_SPACING);
            }
          }
        }
      }

      Vector3D position = combinedFragmentView.getPosition(TransformSpace.GLOBAL);
      position.setX(minX);
      combinedFragmentView.setPositionGlobal(position);
      combinedFragmentView.setMinimumWidth(maxX - minX);
    }
  }
  /**
   * Layouts all fragments along this view. Can be called recursively for different fragment
   * containers.
   *
   * @param container the {@link FragmentContainer} for which fragments should be layouted
   * @param nextfragmentY the y-position for the next fragment
   * @return the y-position for the next fragment
   */
  private float layoutFragments(FragmentContainer container, float nextfragmentY) {
    float previousFragmentY = nextfragmentY;
    float currentFragmentY = nextfragmentY;
    boolean receiveEventNext = false;

    // Make sure that all messages are updated, since this might be necessary due to moving the
    // lifelines around.
    for (InteractionFragment fragment : container.getFragments()) {
      int index = fragment.getContainer().getFragments().indexOf(fragment);

      for (Lifeline lifeline : fragment.getCovered()) {
        LifelineView lifelineView = lifelines.get(lifeline);

        // In case of a create message the lifeline has to be moved down depending on the name
        // height.
        if (fragment instanceof MessageOccurrenceSpecification) {
          MessageOccurrenceSpecification messageEnd = (MessageOccurrenceSpecification) fragment;

          // If the message was removed, but the message ends are still there,
          // ignore them.
          if (!messages.containsKey(messageEnd.getMessage())) {
            continue;
          }

          if (messageEnd.getMessage().getMessageSort() == MessageSort.CREATE_MESSAGE
              && messageEnd.getMessage().getReceiveEvent() == messageEnd) {
            float lifelineNameHeight = lifelineView.getNameHeight();
            float difference = lifelineNameHeight - BOX_HEIGHT;
            float lifelineY = currentFragmentY - (difference / 2f);
            /** Convert global position to relative one. */
            lifelineY =
                getGlobalVecToParentRelativeSpace(lifelineView, new Vector3D(0, lifelineY)).y;
            lifelineView.getLayoutElement().setY(lifelineY);
            currentFragmentY += lifelineNameHeight;
            receiveEventNext = false;
            continue;
          }
        }

        RamRectangleComponent fragmentView = lifelineView.getFragmentView(fragment, index);
        // view is null when message was just removed
        if (fragmentView != null) {
          RamRectangleComponent spacer = lifelineView.getSpacerForFragmentAt(fragment, index);

          // Start with the position of the first spacer.
          if (currentFragmentY == LIFELINE_Y) {
            currentFragmentY = spacer.getPosition(TransformSpace.GLOBAL).getY();
          }

          if (!receiveEventNext) {
            currentFragmentY += BOX_HEIGHT;
          }

          Vector3D currentPosition = fragmentView.getPosition(TransformSpace.GLOBAL);

          if (currentPosition.getY() != currentFragmentY) {
            float difference = currentFragmentY - currentPosition.getY();
            float height = spacer.getHeight() + difference;
            // Fix the height to not go below the minimum size.
            if (height < BOX_HEIGHT) {
              height = BOX_HEIGHT;
            }

            spacer.setMinimumHeight(height);
            // Need to inform parent manually.
            spacer.updateParent();
          }

          if (fragment instanceof MessageOccurrenceSpecification) {
            MessageOccurrenceSpecification messageEnd = (MessageOccurrenceSpecification) fragment;
            Message message = messageEnd.getMessage();

            /**
             * Prevent moving downwards if the next event will be a receive event. However, if it is
             * a self message we need to move downwards. If it is the send event of a reply that
             * ends in a gate, there will be no next event.
             */
            if (!message.isSelfMessage()
                && message.getSendEvent() == messageEnd
                && message.getReceiveEvent().eClass() != RamPackage.Literals.GATE) {
              receiveEventNext = true;
            } else {
              receiveEventNext = false;
            }
          }

          if (fragment instanceof CombinedFragment) {
            CombinedFragment combinedFragment = (CombinedFragment) fragment;
            CombinedFragmentView combinedFragmentView = combinedFragments.get(combinedFragment);

            if (combinedFragment.getCovered().indexOf(lifeline) == 0) {
              Vector3D position = combinedFragmentView.getPosition(TransformSpace.GLOBAL);
              position.setY(currentFragmentY);
              combinedFragmentView.setPositionGlobal(position);

              receiveEventNext = true;
            }

            // Only increase y if it is the last lifeline.
            int lastIndex = combinedFragment.getCovered().size() - 1;
            if (combinedFragment.getCovered().indexOf(lifeline) == lastIndex) {
              for (InteractionOperand operand : combinedFragment.getOperands()) {
                float operandHeight = combinedFragmentView.getOperandMinimumHeight(operand);

                if (operand.getFragments().size() > 0) {
                  operandHeight += layoutFragments(operand, currentFragmentY + operandHeight);
                }

                // Add an additional space due to the additional spacer in the operand.
                // This also supports empty operands.
                operandHeight += BOX_HEIGHT;
                combinedFragmentView.setOperandHeight(operand, operandHeight);

                currentFragmentY += operandHeight;
              }

              combinedFragmentView.updateLayout();
              receiveEventNext = false;
            }
          } else if (!receiveEventNext) {
            currentFragmentY += fragmentView.getHeight();
          }
        }
      }
    }

    return currentFragmentY - previousFragmentY;
  }
  /**
   * Adds a new view for the given message to this view. A message is represented by a view on each
   * end and a view representing the actual call. The end views are added to the corresponding
   * lifelines. In case an end is a gate and not placed on a lifeline, a {@link GateView} is used
   * instead and added to this view at the left side.
   *
   * @param message the {@link Message} to add
   */
  private void addMessageView(Message message) {
    LifelineView toView = null;
    LifelineView fromView = null;
    RamRectangleComponent sendEventView = null;
    RamRectangleComponent receiveEventView = null;
    MessageEnd sendEvent = message.getSendEvent();
    MessageEnd receiveEvent = message.getReceiveEvent();

    Operation signature = message.getSignature();
    boolean messageViewDefined = false;

    if (signature != null && specifies != signature) {
      Aspect aspect = EMFModelUtil.getRootContainerOfType(signature, RamPackage.Literals.ASPECT);
      /**
       * Make sure that the signature still exists in the aspect. It could have been deleted and
       * would then cause problems here.
       */
      if (aspect != null) {
        messageViewDefined = RAMModelUtil.isMessageViewDefined(aspect, signature);
      }
    }

    if (sendEvent instanceof MessageOccurrenceSpecification) {
      MessageOccurrenceSpecification event = (MessageOccurrenceSpecification) sendEvent;

      fromView = lifelines.get(event.getCovered().get(0));
      int modelIndex = event.getContainer().getFragments().indexOf(sendEvent);

      if (fromView != null) {
        boolean allowMessageCreation = false;
        // Allow creation after sending a message where no return is expected.
        if (signature != null && signature.getReturnType() != null) {
          Type returnType = signature.getReturnType();
          allowMessageCreation =
              message.getMessageSort() != MessageSort.REPLY
                  && !message.isSelfMessage()
                  && (returnType.eClass() == RamPackage.Literals.RVOID
                      || message.getMessageSort() == MessageSort.CREATE_MESSAGE
                      || signature.eContainer() instanceof ImplementationClass
                      // When a message view is defined, there will be no reply, so we need to allow
                      // it.
                      || messageViewDefined);
        }

        sendEventView = fromView.addMessageEnd(event, modelIndex, allowMessageCreation);
      }
    }

    if (receiveEvent instanceof MessageOccurrenceSpecification) {
      MessageOccurrenceSpecification event = (MessageOccurrenceSpecification) receiveEvent;

      toView = lifelines.get(event.getCovered().get(0));
      int modelIndex = event.getContainer().getFragments().indexOf(receiveEvent);

      if (toView != null) {
        if (message.getMessageSort() == MessageSort.CREATE_MESSAGE) {
          receiveEventView = toView;
        } else {
          boolean allowMessageCreation = message.getMessageSort() != MessageSort.DELETE_MESSAGE;
          if (signature != null) {
            // Don't allow message creation if the receiving end is on an implementation class.
            // If it is a self message it cannot be on an implementation class and is allowed.
            allowMessageCreation =
                allowMessageCreation
                    && !(signature.eContainer() instanceof ImplementationClass)
                    && (!messageViewDefined || message.isSelfMessage());
          }

          receiveEventView = toView.addMessageEnd(event, modelIndex, allowMessageCreation);
        }
      }
    }

    if (sendEventView == null) {
      Vector3D oppositePosition = receiveEventView.getPosition(TransformSpace.GLOBAL);
      sendEventView = new GateView(0, oppositePosition.getY(), BOX_WIDTH, BOX_HEIGHT);
      addChild(sendEventView);
    } else if (receiveEventView == null) {
      Vector3D oppositePosition = sendEventView.getPosition(TransformSpace.GLOBAL);
      receiveEventView = new GateView(0, oppositePosition.getY(), BOX_WIDTH, BOX_HEIGHT);
      addChild(receiveEventView);
    }

    MessageCallView messageCallView = new MessageCallView(message, sendEventView, receiveEventView);
    addChild(messageCallView);
    messageCallView.setHandler(HandlerFactoryMessageView.INSTANCE.getMessageHandler());
    messages.put(message, messageCallView);

    layoutMessageView();

    messageCallView.updateLines();
  }