diff --git a/ganttzk/src/main/java/org/zkoss/ganttz/util/MutableTreeModel.java b/ganttzk/src/main/java/org/zkoss/ganttz/util/MutableTreeModel.java index 87ef09fdc..9ee57db92 100644 --- a/ganttzk/src/main/java/org/zkoss/ganttz/util/MutableTreeModel.java +++ b/ganttzk/src/main/java/org/zkoss/ganttz/util/MutableTreeModel.java @@ -192,6 +192,32 @@ public class MutableTreeModel extends AbstractTreeModel { return asIntArray(path); } + public int[] getPath(Object last) { + return getPath(getRoot(), last); + } + + public T findObjectAt(int... path) { + T current = getRoot(); + for (int i = 0; i < path.length; i++) { + int position = path[i]; + if (position >= getChildCount(current)) { + throw new IllegalArgumentException( + "Failure acessing the path at: " + + stringRepresentationUntil(path, i)); + } + current = getChild(current, position); + } + return current; + } + + private static String stringRepresentationUntil(int[] path, int endExclusive) { + String valid = Arrays.toString(Arrays + .copyOfRange(path, 0, endExclusive)); + String invalid = Arrays.toString(Arrays.copyOfRange(path, endExclusive, + path.length)); + return valid + "^" + invalid; + } + private int[] asIntArray(List path) { int[] result = new int[path.size()]; int i = 0; @@ -417,6 +443,4 @@ public class MutableTreeModel extends AbstractTreeModel { asList(each, result); } } - - } diff --git a/ganttzk/src/test/java/org/zkoss/ganttz/util/MutableTreeModelTest.java b/ganttzk/src/test/java/org/zkoss/ganttz/util/MutableTreeModelTest.java index fe375e423..bc6cda920 100644 --- a/ganttzk/src/test/java/org/zkoss/ganttz/util/MutableTreeModelTest.java +++ b/ganttzk/src/test/java/org/zkoss/ganttz/util/MutableTreeModelTest.java @@ -162,6 +162,47 @@ public class MutableTreeModelTest { assertThat(parents, equalTo(Arrays.asList(child, root))); } + @Test(expected = IllegalArgumentException.class) + public void findObjectAtInvalidPath() { + Prueba root = new Prueba(); + MutableTreeModel model = MutableTreeModel.create(Prueba.class, + root); + model.findObjectAt(0, 1); + } + + @Test(expected = IllegalArgumentException.class) + public void findObjectAtInvalidPathInTheMiddle() { + Prueba root = new Prueba(); + MutableTreeModel model = MutableTreeModel.create(Prueba.class, + root); + Prueba p1 = new Prueba(); + model.addToRoot(p1); + model.addToRoot(new Prueba()); + model.add(p1, new Prueba()); + model.findObjectAt(1, 1); + } + + @Test + public void findAtPathCanReceiveGetPathResult() { + Prueba root = new Prueba(); + MutableTreeModel model = MutableTreeModel.create(Prueba.class, + root); + assertTrue(canBeRetrievedWithGetPath(model, model.getRoot())); + model.addToRoot(new Prueba()); + Prueba p2 = new Prueba(); + model.addToRoot(p2); + assertTrue(canBeRetrievedWithGetPath(model, p2)); + Prueba grandChild = new Prueba(); + model.add(p2, grandChild); + assertTrue(canBeRetrievedWithGetPath(model, grandChild)); + } + + private static boolean canBeRetrievedWithGetPath( + final MutableTreeModel tree, T object) { + int[] path = tree.getPath(object); + return tree.findObjectAt(path).equals(object); + } + @Test(expected = IllegalArgumentException.class) public void getParentOfRootThrowsException() { Prueba root = new Prueba();