// Determine which entity was selected for the create instance action.
super.actionPerformed(e);
// NOTE: This cast should be safe because this controller is
// used for actors.
InstantiableNamedObj object = (InstantiableNamedObj) getTarget();
NamedObj container = object.getContainer();
// Assumes MoML parser will convert to instance.
if (!object.isClassDefinition()) {
// Object is already an instance. Do nothing.
return;
}
// If the class has objects that defer to it, then
// refuse to convert.
boolean hasDeferrals = false;
List deferred = object.getChildren();
StringBuffer names = new StringBuffer();
if (deferred != null) {
// List contains weak references, so it's not
// sufficient to just check the length.
Iterator deferrers = deferred.iterator();
while (deferrers.hasNext()) {
WeakReference deferrer = (WeakReference) deferrers.next();
NamedObj deferrerObject = (NamedObj) deferrer.get();
if (deferrerObject != null) {
hasDeferrals = true;
if (names.length() > 0) {
names.append(", ");
}
names.append(deferrerObject.getFullName());
}
}
}
if (hasDeferrals) {
MessageHandler.error("Cannot convert to instance because "
+ "there are instances and/or subclasses:\n"
+ names.toString());
return;
}
String moml = "<entity name=\"" + object.getName() + "\"/>";
MoMLChangeRequest request = new MoMLChangeRequest(this, container,
moml);
container.requestChange(request);
}