// Determine which ports have been removed. If a port exists on the
// target but is not represented by a row in the table then it needs
// to be removed.
Vector portsToBeRemoved = new Vector();
Iterator portIterator = getTarget().portList().iterator();
Port actualPort = null;
while (portIterator.hasNext()) {
Object candidate = portIterator.next();
if (candidate instanceof Port) {
boolean foundPort = false;
actualPort = (Port) candidate;
if (actualPort == null) {
throw new InternalErrorException(
"The target contains a null Port.");
}
for (int i = 0; i < _ports.size(); i++) {
Hashtable portInfo = (Hashtable) (_ports.elementAt(i));
if (actualPort == ((Port) portInfo
.get(ColumnNames.COL_ACTUAL_PORT))) {
foundPort = true;
break;
}
}
if (!foundPort) {
portsToBeRemoved.add(actualPort);
}
} else {
throw new InternalErrorException("The target portList contains"
+ " an object that is not of type Port.");
}
}
Iterator actualPorts = portsToBeRemoved.iterator();
while (actualPorts.hasNext()) {
StringBuffer moml = new StringBuffer();
actualPort = (Port) (actualPorts.next());
// The context for the MoML should be the first container
// above this port in the hierarchy that defers its MoML
// definition, or the immediate parent if there is none.
NamedObj container = actualPort.getContainer();
NamedObj composite = container.getContainer();
if (composite != null) {
moml.append("<deletePort name=\"" + actualPort.getName()
+ "\" entity=\"" + container.getName() + "\" />");
} else {
moml.append("<deletePort name=\""
+ actualPort.getName(container) + "\" />");
}
// NOTE: the context is the composite entity containing
// the entity if possible
MoMLChangeRequest request = null;
if (composite != null) {
request = new MoMLChangeRequest(this, composite, moml
.toString());
} else {
request = new MoMLChangeRequest(this, container, moml
.toString());
}
request.setUndoable(true);
container.addChangeListener(this);
container.requestChange(request);
}
// Iterate over the table rows that represent ports. If a row
// corresponds to an actual port then look to see if that row
// is different from the actual port. If it is, then update
// that actual port. If a row does not correspond to an
// actual port then that row represents a new actual port
// which must be created.
StringBuffer moml = new StringBuffer("<group>");
boolean haveSomeUpdate = false;
for (int i = 0; i < _ports.size(); i++) {
Hashtable portInfo = (Hashtable) (_ports.elementAt(i));
portIterator = getTarget().portList().iterator();
// actualPort will be the Port found on the _target, if
// there is one.
actualPort = (Port) (portInfo.get(ColumnNames.COL_ACTUAL_PORT));
Hashtable updates = new Hashtable();
// FIXME is it necessary to add unchanged fields to hashtable ?
if (actualPort != null) {
// actualPort is a Port found on the _target. Check to
// see if the actualPort is different and needs to be
// updated.
boolean havePortUpdate = false;
if (_columnNames.contains(ColumnNames.COL_NAME)) {
String tableValue = (String) portInfo
.get(ColumnNames.COL_NAME);
if (!(actualPort.getName().equals(tableValue))) {
havePortUpdate = true;
updates.put(ColumnNames.COL_NAME, Boolean.TRUE);
}
}
if (actualPort instanceof IOPort) {
IOPort iop = (IOPort) actualPort;
if (_columnNames.contains(ColumnNames.COL_INPUT)) {
Boolean tableValue = (Boolean) portInfo
.get(ColumnNames.COL_INPUT);
if (iop.isInput() != tableValue.booleanValue()) {
havePortUpdate = true;
updates.put(ColumnNames.COL_INPUT, Boolean.TRUE);
}
}
if (_columnNames.contains(ColumnNames.COL_OUTPUT)) {
Boolean tableValue = (Boolean) portInfo
.get(ColumnNames.COL_OUTPUT);
if (iop.isOutput() != tableValue.booleanValue()) {
havePortUpdate = true;
updates.put(ColumnNames.COL_OUTPUT, Boolean.TRUE);
}
}
if (_columnNames.contains(ColumnNames.COL_MULTIPORT)) {
Boolean tableValue = (Boolean) portInfo
.get(ColumnNames.COL_MULTIPORT);
if (iop.isMultiport() != tableValue.booleanValue()) {
havePortUpdate = true;
updates
.put(ColumnNames.COL_MULTIPORT,
Boolean.TRUE);
}
}
}
if (_columnNames.contains(ColumnNames.COL_SHOW_NAME)) {
boolean isShowSet = _isPropertySet(actualPort, "_showName");
Boolean tableValue = (Boolean) portInfo
.get(ColumnNames.COL_SHOW_NAME);
if (isShowSet != tableValue.booleanValue()) {
havePortUpdate = true;
updates.put(ColumnNames.COL_SHOW_NAME, Boolean.TRUE);
}
}
if (_columnNames.contains(ColumnNames.COL_HIDE)) {
boolean isHideSet = _isPropertySet(actualPort, "_hide");
Boolean tableValue = (Boolean) portInfo
.get(ColumnNames.COL_HIDE);
if (isHideSet != tableValue.booleanValue()) {
havePortUpdate = true;
updates.put(ColumnNames.COL_HIDE, Boolean.TRUE);
}
}
if (actualPort instanceof TypedIOPort) {
TypedIOPort tiop = (TypedIOPort) actualPort;
if (_columnNames.contains(ColumnNames.COL_TYPE)) {
String _type = null;
TypeAttribute _typeAttribute = (TypeAttribute) tiop
.getAttribute("_type");
if (_typeAttribute != null) {
_type = _typeAttribute.getExpression();
}
String tableValue = (String) portInfo
.get(ColumnNames.COL_TYPE);
if (((_type == null) && (!tableValue.equals("")))
|| ((_type != null) && (!tableValue
.equals(_type)))) {
havePortUpdate = true;
updates.put(ColumnNames.COL_TYPE, Boolean.TRUE);
}
}
}
if (_columnNames.contains(ColumnNames.COL_DIRECTION)) {
// Look for a change in direction
String _direction = null;
String direction = (String) portInfo
.get(ColumnNames.COL_DIRECTION);
StringAttribute _cardinal = (StringAttribute) actualPort
.getAttribute("_cardinal");
if (_cardinal != null) {
_direction = _cardinal.getExpression().toUpperCase();
}
if (((_direction == null) && !direction.equals("DEFAULT"))
|| ((_direction != null) && (!direction
.equals(_direction)))) {
havePortUpdate = true;
updates.put(ColumnNames.COL_DIRECTION, Boolean.TRUE);
}
}
if (_columnNames.contains(ColumnNames.COL_UNITS)) {
String units = null;
UnitAttribute _unitsAttribute = (UnitAttribute) actualPort
.getAttribute("_units");
if (_unitsAttribute != null) {
units = _unitsAttribute.getExpression();
}