ItEr27S06CUAsignacionGrupoRecursosAPlanificacionItEr26S07: Adding method to replace a node for other

This commit is contained in:
Óscar González Fernández 2009-09-23 16:52:53 +02:00
parent 010dee857d
commit 60ae3f1e9d
2 changed files with 27 additions and 0 deletions

View file

@ -233,4 +233,14 @@ public class MutableTreeModel<T> extends AbstractTreeModel {
return associatedNode.isRoot();
}
public void replace(T nodeToRemove, T nodeToAdd) {
T parent = getParent(nodeToRemove);
Node<T> parentNode = find(parent);
final int insertionPosition = parentNode.getIndexOf(find(nodeToRemove));
remove(nodeToRemove);
List<T> toAdd = new ArrayList<T>();
toAdd.add(nodeToAdd);
add(parent, insertionPosition, toAdd);
}
}

View file

@ -317,6 +317,23 @@ public class MutableTreeModelTest {
TreeDataEvent.INTERVAL_REMOVED, model.getRoot(), 0);
}
@Test
public void aNodeCanBeReplacedByOther() {
final MutableTreeModel<Prueba> model = MutableTreeModel
.create(Prueba.class);
Prueba toRemove = new Prueba();
Prueba prueba2 = new Prueba();
Prueba grandChild = new Prueba();
model.add(model.getRoot(), toRemove);
model.add(model.getRoot(), prueba2);
model.add(toRemove, grandChild);
Prueba substitution = new Prueba();
model.replace(toRemove, substitution);
assertThat(model.getChildCount(substitution), equalTo(0));
assertThat(model.getChild(model.getRoot(), 0), equalTo(substitution));
}
private void checkIsValid(TreeDataEvent event, int type,
Prueba expectedParent, int expectedPosition) {
checkIsValid(event, type, expectedParent, expectedPosition,