}
// Remove the old component, if there is one
if (!isInsertRow) {
TableCell cell = rowList.get(rowIndex).get(columnIndex);
AbstractComponent toBeRemoved = cell.getValue();
rootComponent.removeDelegateComponent(toBeRemoved);
}
rootComponent.addDelegateComponents(componentIndex, Collections.<AbstractComponent> singletonList(value));
} else {
if (isInsertRow) {
// Should insert new child. Create a collection of the same type as
// the root object.
int componentIndex;
if (rowIndex < rowList.size()) {
componentIndex = rowList.get(rowIndex).getIndex();
} else {
componentIndex = rootComponent.getComponents().size();
}
AbstractComponent newChild = ServiceAccess.getService(ComponentRegistry.class).newCollection(Collections.singleton(value));
rootComponent.addDelegateComponents(componentIndex, Collections.singleton(newChild));
} else { //Insert column, or overwrite cell
TableRow row = rowList.get(rowIndex);
int componentIndex;
AbstractComponent targetComponent;
if (!isInsertColumn && columnIndex < row.size()) {
}
if (columnIndex < row.size()) {
targetComponent = row.get(columnIndex).getParent();
componentIndex = row.get(columnIndex).getIndex();
if (!isInsertColumn) {
targetComponent.removeDelegateComponent(row.get(columnIndex).getValue());
}
} else {
targetComponent = row.get(row.size()-1).getParent();
componentIndex = targetComponent.getComponents().size();
}
targetComponent.addDelegateComponents(componentIndex, Collections.singleton(value));
}
updateColumnCount(); // in case we added a new column
}