public class GTEntityUtils {
public static void updateAppearance(final GTEntity entity,
GTIngredientsAttribute attribute) {
Workspace workspace = ((NamedObj) entity).workspace();
try {
workspace.getWriteAccess();
Set<String> preservedPortNames = new HashSet<String>();
boolean isIconSet = false;
int i = 1;
GTIngredientList list = attribute.getIngredientList();
for (GTIngredient ingredient : list) {
if (ingredient instanceof PortCriterion) {
PortCriterion criterion = (PortCriterion) ingredient;
String portID = criterion.getPortID(list);
preservedPortNames.add(portID);
TypedIOPort port = (TypedIOPort)
((ComponentEntity) entity).getPort(portID);
boolean isInput = criterion.isInput();
boolean isOutput = criterion.isOutput();
boolean isMultiport = !criterion.isMultiportEnabled()
|| criterion.isMultiport();
if (port != null) {
if (port instanceof PortMatcher) {
port.setInput(isInput);
port.setOutput(isOutput);
} else {
MoMLChangeRequest request =
new MoMLChangeRequest(entity, (NamedObj) entity,
"<deletePort name=\"" + port.getName()
+ "\"/>");
request.setUndoable(true);
request.setMergeWithPreviousUndo(true);
request.execute();
port = new PortMatcher(criterion,
(ComponentEntity) entity, portID, isInput,
isOutput);
port.setPersistent(false);
}
} else {
port = new PortMatcher(criterion,
(ComponentEntity) entity, portID, isInput,
isOutput);
port.setPersistent(false);
}
port.setMultiport(isMultiport);
} else if (ingredient instanceof SubclassCriterion
&& !isIconSet) {
SubclassCriterion criterion = (SubclassCriterion) ingredient;
final String superclass = criterion.getSuperclass();
((NamedObj) entity).requestChange(new ChangeRequest(entity,
"Deferred load actor icon action.") {
protected void _execute() throws Exception {
_loadActorIcon(entity, superclass);
}
});
isIconSet = true;
}
i++;
}
if (!isIconSet) {
((NamedObj) entity).requestChange(
new RestoreAppearanceChangeRequest(entity));
}
List<?> portList = new LinkedList<Object>((List<?>)
((ComponentEntity) entity).portList());
for (i = 0; i < portList.size(); i++) {
Port port = (Port) portList.get(i);
if (port instanceof PortMatcher
&& !preservedPortNames.contains(port.getName())) {
((PortMatcher) port)._setPortCriterion(null);
port.setContainer(null);
}
}
} catch (KernelException e) {
throw new KernelRuntimeException(e, "Cannot update appearance for "
+ "actor " + entity.getName() + ".");
} finally {
workspace.doneWriting();
}
}