+ "insertAt and insertInsideAt, not both.",
_currentExternalEntity(), _getLineNumber(),
_getColumnNumber());
}
CompositeEntity context = (CompositeEntity) _current;
// Parse port
ComponentPort port = _getPort(portName, context);
// Save to help generate undo MoML
int origNumOutsideLinks = port.numLinks();
int origNumInsideLinks = port.numInsideLinks();
// Get relation if given
ComponentRelation relation = null;
if (relationName != null) {
Relation tmpRelation = context.getRelation(relationName);
_checkForNull(tmpRelation, "No relation named \"" + relationName
+ "\" in " + context.getFullName());
relation = (ComponentRelation) tmpRelation;
}
// Ensure that derived objects aren't changed.
// We have to prohibit adding links between class
// elements because this operation cannot be undone, and
// it will not be persistent.
if (_isLinkInClass(context, port, relation)) {
throw new IllegalActionException(port,
"Cannot link a port to a relation when both"
+ " are part of the class definition.");
}
// Get the index if given
int insertAt = -1;
if (insertAtSpec != null) {
insertAt = Integer.parseInt(insertAtSpec);
}
// Get the inside index if given
int insertInsideAt = -1;
if (insertInsideAtSpec != null) {
insertInsideAt = Integer.parseInt(insertInsideAtSpec);
}
if (insertAtSpec != null) {
port.insertLink(insertAt, relation);
} else if (insertInsideAtSpec != null) {
port.insertInsideLink(insertInsideAt, relation);
} else {
port.link(relation);
}
// Propagate. Get the derived list for the relation,
// then use its container as the context in which to
// find the port. NOTE: The relation can be null
// (to insert an empty link in a multiport), so
// we have two cases to consider.
if (relation != null) {
Iterator derivedObjects = relation.getDerivedList().iterator();
while (derivedObjects.hasNext()) {
ComponentRelation derivedRelation = (ComponentRelation) derivedObjects
.next();
CompositeEntity derivedContext = (CompositeEntity) derivedRelation
.getContainer();
ComponentPort derivedPort = _getPort(portName, derivedContext);
// NOTE: Duplicate the above logic exactly.
if (insertAtSpec != null) {