@Override
public void commitChildChanges(ValueNode oldChild, ValueNode newChild) {
// Get the copy of the current value node, type switched if necessary.
ListValueNode oldValueNode = (ListValueNode)getValueNode();
ListValueNode newValueNode;
if (!oldChild.getTypeExpr().sameType(newChild.getTypeExpr())) {
Map<ValueNode, TypeExpr> valueNodeToUnconstrainedTypeMap = getValueNodeToUnconstrainedTypeMap();
Map<ValueNode, ValueNode> commitValueMap = valueEditorManager.getValueNodeCommitHelper().getCommitValues(oldChild, newChild, valueNodeToUnconstrainedTypeMap);
PreludeTypeConstants typeConstants = valueEditorManager.getPreludeTypeConstants();
TypeExpr charType = typeConstants.getCharType();
ValueNode newValueNodeFromMap = commitValueMap.get(oldValueNode);
// HACK: if it's a ListOfCharValueNode, convert it to a ListValueNode.
// What we need is a way to guarantee the type of value node that is returned by getCommitValues().
// This is not possible with the current form of transmuteValueNode() though.
if (newValueNodeFromMap instanceof ListOfCharValueNode) {
ListOfCharValueNode charListValueNode = (ListOfCharValueNode)newValueNodeFromMap;
char[] charListValueArray = charListValueNode.getStringValue().toCharArray();
ArrayList<ValueNode> newListValue = new ArrayList<ValueNode>(charListValueArray.length);
for (final char charListValue : charListValueArray) {
newListValue.add(new LiteralValueNode(Character.valueOf(charListValue), charType));
}
newValueNode = new ListValueNode(newListValue, typeConstants.getCharListType(), new LiteralValueNode(new Character('a'), charType));
replaceValueNode(newValueNode, true);
return;
} else {
newValueNode = (ListValueNode)newValueNodeFromMap;
}
} else {
newValueNode = (ListValueNode)oldValueNode.copyValueNode();
}
// Modify the new value node so that the old child is replaced by the new child.
// Note that the cell editor may now be editing a different row so we have to search for the row that changed
// This can happen if one clicks from an editor for one cell to another cell
// (eg. in a list of colours, from a colour value editor for one cell, onto the cell editor for another cell.)
List<ValueNode> oldChildrenList = oldValueNode.getValue();
List<ValueNode> newChildrenList = newValueNode.getValue();
boolean isListRecord = getListElementType().rootRecordType() != null;
if (consolidateColumns || !isListRecord) {
for (int i = 0, listSize = newValueNode.getNElements(); i < listSize; i++) {
if (oldChildrenList.get(i) == oldChild) {
TypeExpr childType = (newChildrenList.get(i)).getTypeExpr();
newValueNode.setValueNodeAt(i, newChild.copyValueNode(childType));
break;
}
}
} else {
// isListRecord == true, ie. must be a list of records (or tuples).
AbstractRecordValueNode firstChild = (AbstractRecordValueNode)oldChildrenList.get(0);
// To find the child that changed, for each value in the list, have to iterate through the tuple/record.
int listSize = newValueNode.getNElements();
int childrenSize = firstChild.getNFieldNames();
// TODO: Note that new record value nodes may have more/less fields than the old nodes,
// because of type switch