/**
   * Specify the container entity, adding the relation to the list of relations in the container. If
   * the container already contains a relation with the same name, then throw an exception and do
   * not make any changes. Similarly, if the container is not in the same workspace as this
   * relation, throw an exception. If the relation is a class element and the proposed container
   * does not match the current container, then also throw an exception. If the relation is already
   * contained by the container, do nothing. If this relation already has a container, remove it
   * from that container first. Otherwise, remove it from the workspace directory, if it is present.
   * If the argument is null, then unlink the ports from the relation and remove it from its
   * container. It is not added to the workspace directory, so this could result in this relation
   * being garbage collected. Derived classes may further constrain the class of the container to a
   * subclass of CompositeEntity. This method validates all deeply contained instances of Settable,
   * since they may no longer be valid in the new context. This method is write-synchronized on the
   * workspace and increments its version number.
   *
   * @param container The proposed container.
   * @exception IllegalActionException If this entity and the container are not in the same
   *     workspace, or if a contained Settable becomes invalid and the error handler throws it.
   * @exception NameDuplicationException If the name collides with a name already on the contents
   *     list of the container.
   * @see #getContainer()
   */
  public void setContainer(CompositeEntity container)
      throws IllegalActionException, NameDuplicationException {
    if ((container != null) && (_workspace != container.workspace())) {
      throw new IllegalActionException(
          this, container, "Cannot set container because workspaces are different.");
    }

    try {
      _workspace.getWriteAccess();
      _checkContainer(container);

      CompositeEntity previousContainer = (CompositeEntity) getContainer();

      if (previousContainer == container) {
        return;
      }

      // Do this first, because it may throw an exception.
      if (container != null) {
        container._addRelation(this);

        if (previousContainer == null) {
          _workspace.remove(this);
        }
      }

      _container = container;

      if (previousContainer != null) {
        previousContainer._removeRelation(this);
      }

      if (container == null) {
        unlinkAll();
      } else {
        // We have successfully set a new container for this
        // object. Mark it modified to ensure MoML export.
        // Transfer any queued change requests to the
        // new container.  There could be queued change
        // requests if this component is deferring change
        // requests.
        if (_changeRequests != null) {
          Iterator requests = _changeRequests.iterator();

          while (requests.hasNext()) {
            ChangeRequest request = (ChangeRequest) requests.next();
            container.requestChange(request);
          }

          _changeRequests = null;
        }
      }

      // Validate all deeply contained settables, since
      // they may no longer be valid in the new context.
      validateSettables();
    } finally {
      _workspace.doneWriting();
    }
  }