1 package pl.polidea.treeview;
3 import android.util.Log;
6 * Allows to build tree easily in sequential mode (you have to know levels of
7 * all the tree elements upfront). You should rather use this class rather than
8 * manager if you build initial tree from some external data source.
12 public class TreeBuilder<T> {
13 private static final String TAG = TreeBuilder.class.getSimpleName();
15 private final TreeStateManager<T> manager;
17 private T lastAddedId = null;
18 private int lastLevel = -1;
20 public TreeBuilder(final TreeStateManager<T> manager) {
21 this.manager = manager;
29 * Adds new relation to existing tree. Child is set as the last child of the
30 * parent node. Parent has to already exist in the tree, child cannot yet
31 * exist. This method is mostly useful in case you add entries layer by
32 * layer - i.e. first top level entries, then children for all parents, then
33 * grand-children and so on.
40 public synchronized void addRelation(final T parent, final T child) {
41 Log.d(TAG, "Adding relation parent:" + parent + " -> child: " + child);
42 manager.addAfterChild(parent, child, null);
44 lastLevel = manager.getLevel(child);
48 * Adds sequentially new node. Using this method is the simplest way of
49 * building tree - if you have all the elements in the sequence as they
50 * should be displayed in fully-expanded tree. You can combine it with add
51 * relation - for example you can add information about few levels using
52 * {@link addRelation} and then after the right level is added as parent,
53 * you can continue adding them using sequential operation.
60 public synchronized void sequentiallyAddNextNode(final T id, final int level) {
61 Log.d(TAG, "Adding sequentiall node " + id + " at level " + level);
62 if (lastAddedId == null) {
63 addNodeToParentOneLevelDown(null, id, level);
65 if (level <= lastLevel) {
66 final T parent = findParentAtLevel(lastAddedId, level - 1);
67 addNodeToParentOneLevelDown(parent, id, level);
69 addNodeToParentOneLevelDown(lastAddedId, id, level);
75 * Find parent of the node at the level specified.
78 * node from which we start
80 * level which we are looking for
81 * @return the node found (null if it is topmost node).
83 private T findParentAtLevel(final T node, final int levelToFind) {
84 T parent = manager.getParent(node);
85 while (parent != null) {
86 if (manager.getLevel(parent) == levelToFind) {
89 parent = manager.getParent(parent);
95 * Adds note to parent at the level specified. But it verifies that the
96 * level is one level down than the parent!
103 * should always be parent's level + 1
105 private void addNodeToParentOneLevelDown(final T parent, final T id,
107 if (parent == null && level != 0) {
108 throw new TreeConfigurationException("Trying to add new id " + id
109 + " to top level with level != 0 (" + level + ")");
111 if (parent != null && manager.getLevel(parent) != level - 1) {
112 throw new TreeConfigurationException("Trying to add new id " + id
113 + " <" + level + "> to " + parent + " <"
114 + manager.getLevel(parent)
115 + ">. The difference in levels up is bigger than 1.");
117 manager.addAfterChild(parent, id, null);
118 setLastAdded(id, level);
121 private void setLastAdded(final T id, final int level) {