// Have to convert a list of NTuples to a list of records.
// Note: It might be better to move some of this code to nTupleValueNode.transmute(),
// or ValueNodeTransformer.transform(). This duplicates some work though.
ValueNodeBuilderHelper valueNodeBuilderHelper =
valueEditorHierarchyManager.getValueEditorManager().getValueNodeBuilderHelper();
RecordValueNode.RecordValueNodeProvider recordVNProvider =
new RecordValueNode.RecordValueNodeProvider(valueNodeBuilderHelper);
// Get the new record type.
TypeExpr recordTypeExpr = AbstractRecordValueNode.getRecordTypeForRenamedField(getListElementType(), oldName, newName);
// Iterate through the list and convert tuple nodes to record nodes.
for (int i = 0; i < listSize; i++) {
// Get the tuple node.
NTupleValueNode nTupleValueNode = (NTupleValueNode)listValueNode.getValueAt(i);
// Get the list of values.
List<ValueNode> fieldValueList = new ArrayList<ValueNode>(nTupleValueNode.getValue());
// Create and set the record value node with the list of values.
ValueNode recordValueNode = recordVNProvider.getNodeInstance(fieldValueList, null, recordTypeExpr);
((ListValueNode)getValueNode()).setValueNodeAt(i, recordValueNode);
}
} else {
// No conversion is necessary.
}
} else {
// Empty list, or list of record value nodes.
// Iterate through the list of record nodes and change their type expressions
for (int i = 0; i < listSize; i++) {
AbstractRecordValueNode recordValueNode = (AbstractRecordValueNode)((ListValueNode)getValueNode()).getValueAt(i);
recordValueNode = recordValueNode.renameField(oldName, newName, valueEditorManager.getValueNodeBuilderHelper(), valueEditorManager.getValueNodeTransformer());
((ListValueNode)getValueNode()).setValueNodeAt(i, recordValueNode);
}
}
// Now change the list element type expression and update the value node
Map<FieldName, TypeExpr> fieldNamesToTypeMap = new HashMap<FieldName, TypeExpr>();
List<FieldName> existingFields = getListElementType().rootRecordType().getHasFieldNames();
for (final FieldName existingFieldName : existingFields) {
TypeExpr existingFieldType = getListElementType().rootRecordType().getHasFieldType(existingFieldName);
fieldNamesToTypeMap.put(existingFieldName, existingFieldType);
}
TypeExpr fieldTypeExpr = fieldNamesToTypeMap.remove(oldName);
fieldNamesToTypeMap.put(newName, fieldTypeExpr);
RecordType newRecordType = TypeExpr.makeNonPolymorphicRecordType(fieldNamesToTypeMap);
ValueNodeBuilderHelper valueNodeBuilderHelper = valueEditorManager.getValueNodeBuilderHelper();
replaceValueNode(getValueNode().transmuteValueNode(valueNodeBuilderHelper, valueEditorManager.getValueNodeTransformer(),
valueNodeBuilderHelper.getPreludeTypeConstants().makeListType(newRecordType)), true);
// Select the column of the new field name
int fieldIndex = getListElementType().rootRecordType().getHasFieldNames().indexOf(newName);
if (getRowCount() > 0) {