ItEr16S09RFComportamentoGraficoPlanificadorItEr15S12: Specifying behaviour for some situations in MutableTreeModel

This commit is contained in:
Óscar González Fernández 2009-07-12 21:31:25 +02:00 committed by Javier Moran Rua
parent 348ba7b7d8
commit 2d8d742a38
2 changed files with 40 additions and 4 deletions

View file

@ -32,6 +32,9 @@ public class MutableTreeModel<T> extends AbstractTreeModel {
private void until(LinkedList<Integer> result, Node<T> parent) {
if (parent.equals(this)) {
return;
} else if (isRoot()) {
// final reached, but parent not found
result.clear();
} else {
result.add(0, this.parentNode.getIndexOf(this));
this.parentNode.until(result, parent);
@ -39,6 +42,10 @@ public class MutableTreeModel<T> extends AbstractTreeModel {
}
private boolean isRoot() {
return parentNode == null;
}
private int getIndexOf(Node<T> child) {
return children.indexOf(child);
}
@ -61,10 +68,7 @@ public class MutableTreeModel<T> extends AbstractTreeModel {
}
private Node<T> find(Object domainObject) {
Node<T> result = nodesByDomainObject.get(domainObject);
if (result == null)
throw new RuntimeException("not found " + domainObject);
return result;
return nodesByDomainObject.get(domainObject);
}
private static <T> T unwrap(Node<T> node) {
@ -92,6 +96,8 @@ public class MutableTreeModel<T> extends AbstractTreeModel {
public int[] getPath(Object parent, Object last) {
Node<T> parentNode = find(parent);
Node<T> lastNode = find(last);
if (parentNode == null || lastNode == null)
return new int[0];
List<Integer> path = lastNode.until(parentNode);
return asIntArray(path);
}

View file

@ -82,6 +82,36 @@ public class MutableTreeModelTest {
assertThat(path[0], equalTo(0));
}
@Test
public void getPathReturnsEmptyArrayWhenParentNotFound() {
Prueba root = new Prueba();
MutableTreeModel<Prueba> model = MutableTreeModel.create(Prueba.class,
root);
Prueba child = new Prueba();
model.add(root, child);
assertThat(model.getPath(null, child), equalTo(new int[0]));
}
@Test
public void getPathReturnsEmptyArrayWhenChildNotFound() {
Prueba root = new Prueba();
MutableTreeModel<Prueba> model = MutableTreeModel.create(Prueba.class,
root);
Prueba child = new Prueba();
model.add(root, child);
assertThat(model.getPath(root, new Prueba()), equalTo(new int[0]));
}
@Test
public void ifThereisNotPathReturnEmptyArray() {
Prueba root = new Prueba();
MutableTreeModel<Prueba> model = MutableTreeModel.create(Prueba.class,
root);
Prueba child = new Prueba();
model.add(root, child);
assertThat(model.getPath(child, root), equalTo(new int[0]));
}
@Test
public void addingTriggersEvent() {
MutableTreeModel<Prueba> model = MutableTreeModel.create(Prueba.class);