if (relationName != null) {
Relation tmpRelation = context.getRelation(relationName);
_checkForNull(tmpRelation, "No relation named \"" + relationName
+ "\" in " + context.getFullName());
ComponentRelation relation = (ComponentRelation) tmpRelation;
// Ensure that derived objects aren't changed.
if (_isLinkInClass(context, port, relation)) {
throw new IllegalActionException(port,
"Cannot unlink a port from a relation when both"
+ " are part of the class definition.");
}
// Handle the undoable aspect.
// Generate a link in the undo only if one or the other relation is
// not derived. If they are both derived, then the link belongs to
// the class definition and should not be recreated in undo.
if (_undoEnabled
&& (port.getDerivedLevel() == Integer.MAX_VALUE || relation
.getDerivedLevel() == Integer.MAX_VALUE)) {
// Get the relation at the given index
List linkedRelations = port.linkedRelationList();
int index = linkedRelations.indexOf(tmpRelation);
if (index != -1) {
// Linked on the outside...
_undoContext.appendUndoMoML("<link port=\"" + portName
+ "\" insertAt=\"" + index + "\" relation=\""
+ relationName + "\" />\n");
} else {
List insideLinkedRelations = port.insideRelationList();
index = insideLinkedRelations.indexOf(tmpRelation);
// Linked on the inside.
_undoContext.appendUndoMoML("<link port=\"" + portName
+ "\" insertInsideAt=\"" + index + "\" relation=\""
+ relationName + "\" />\n");
}
}
// Propagate. Get the derived list for the relation,
// then use its container as the context in which to
// find the port.
Iterator derivedObjects = relation.getDerivedList().iterator();
while (derivedObjects.hasNext()) {
ComponentRelation derivedRelation = (ComponentRelation) derivedObjects
.next();
CompositeEntity derivedContext = (CompositeEntity) derivedRelation
.getContainer();
ComponentPort derivedPort = _getPort(portName, derivedContext);
derivedPort.unlink(derivedRelation);
}