query.addLine("Name", "Name", defaultName);
// See whether the configuration offers state refinements.
Configuration configuration = ((FSMGraphController) getController())
.getConfiguration();
Entity refinements = configuration.getEntity("_stateRefinements");
// Default choices.
String[] choiceClasses = { "ptolemy.domains.fsm.modal.Refinement",
"ptolemy.domains.fsm.modal.ModalController" };
String[] choiceNames = { "Default Refinement",
"State Machine Refinement" };
// Check the configuration to see whether the default is overridden.
if (refinements instanceof CompositeEntity) {
// There is a specification.
List refinementList = ((CompositeEntity) refinements)
.entityList();
choiceNames = new String[refinementList.size()];
choiceClasses = new String[refinementList.size()];
Iterator iterator = refinementList.iterator();
int count = 0;
while (iterator.hasNext()) {
Entity entity = (Entity) iterator.next();
choiceNames[count] = entity.getName();
choiceClasses[count++] = entity.getClass().getName();
}
}
query
.addChoice("Class", "Class", choiceNames, choiceNames[0],
true);
// FIXME: Need a frame owner for first arg.
// Perhaps calling getController(), which returns a GraphController
// will be a good start.
ComponentDialog dialog = new ComponentDialog(null,
"Specify Refinement", query);
if (!dialog.buttonPressed().equals("OK")) {
return;
}
final String newName = query.getStringValue("Name");
if (container.getEntity(newName) != null) {
MessageHandler.error("There is already a refinement with name "
+ newName + ".");
return;
}
int choiceIndex = query.getIntValue("Class");
String newClass = choiceClasses[choiceIndex];
String currentRefinements = state.refinementName.getExpression();
if ((currentRefinements == null) || currentRefinements.equals("")) {
currentRefinements = newName;
} else {
currentRefinements = currentRefinements.trim() + ", " + newName;
}
String moml;
// The MoML we create depends on whether the configuration
// specified a set of prototype refinements.
if (refinements instanceof CompositeEntity) {
String choiceName = choiceNames[choiceIndex];
Entity template = ((CompositeEntity) refinements)
.getEntity(choiceName);
String templateDescription = template.exportMoML(newName);
moml = "<group>" + templateDescription + "<entity name=\""
+ state.getName(container)
+ "\"><property name=\"refinementName\" value=\""
+ currentRefinements + "\"/></entity></group>";
} else {
moml = "<group><entity name=\"" + newName + "\" class=\""
+ newClass + "\"/>" + "<entity name=\""
+ state.getName(container)
+ "\"><property name=\"refinementName\" value=\""
+ currentRefinements + "\"/></entity></group>";
}
MoMLChangeRequest change = new MoMLChangeRequest(this, container,
moml) {
protected void _execute() throws Exception {
super._execute();
// Mirror the ports of the container in the refinement.
// Note that this is done here rather than as part of
// the MoML because we have set protected variables
// in the refinement to prevent it from trying to again
// mirror the changes in the container.
Entity entity = container.getEntity(newName);
// Get the initial port configuration from the container.
Iterator ports = container.portList().iterator();
while (ports.hasNext()) {
Port port = (Port) ports.next();
try {
// NOTE: This is awkward.
if (entity instanceof Refinement) {
((Refinement) entity).setMirrorDisable(true);
} else if (entity instanceof ModalController) {
((ModalController) entity)
.setMirrorDisable(true);
}
Port newPort = entity.newPort(port.getName());
if (newPort instanceof RefinementPort
&& port instanceof IOPort) {
try {
((RefinementPort) newPort)