1 package pl.polidea.treeview;
3 import net.sf.openrocket.android.util.AndroidLogWrapper;
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> {
14 private final TreeStateManager<T> manager;
16 private T lastAddedId = null;
17 private int lastLevel = -1;
19 public TreeBuilder(final TreeStateManager<T> manager) {
20 this.manager = manager;
28 * Adds new relation to existing tree. Child is set as the last child of the
29 * parent node. Parent has to already exist in the tree, child cannot yet
30 * exist. This method is mostly useful in case you add entries layer by
31 * layer - i.e. first top level entries, then children for all parents, then
32 * grand-children and so on.
39 public synchronized void addRelation(final T parent, final T child) {
40 AndroidLogWrapper.d(TreeBuilder.class, "Adding relation parent:" + parent + " -> child: " + child);
41 manager.addAfterChild(parent, child, null);
43 lastLevel = manager.getLevel(child);
47 * Adds sequentially new node. Using this method is the simplest way of
48 * building tree - if you have all the elements in the sequence as they
49 * should be displayed in fully-expanded tree. You can combine it with add
50 * relation - for example you can add information about few levels using
51 * {@link addRelation} and then after the right level is added as parent,
52 * you can continue adding them using sequential operation.
59 public synchronized void sequentiallyAddNextNode(final T id, final int level) {
60 AndroidLogWrapper.d(TreeBuilder.class, "Adding sequentiall node " + id + " at level " + level);
61 if (lastAddedId == null) {
62 addNodeToParentOneLevelDown(null, id, level);
64 if (level <= lastLevel) {
65 final T parent = findParentAtLevel(lastAddedId, level - 1);
66 addNodeToParentOneLevelDown(parent, id, level);
68 addNodeToParentOneLevelDown(lastAddedId, id, level);
74 * Find parent of the node at the level specified.
77 * node from which we start
79 * level which we are looking for
80 * @return the node found (null if it is topmost node).
82 private T findParentAtLevel(final T node, final int levelToFind) {
83 T parent = manager.getParent(node);
84 while (parent != null) {
85 if (manager.getLevel(parent) == levelToFind) {
88 parent = manager.getParent(parent);
94 * Adds note to parent at the level specified. But it verifies that the
95 * level is one level down than the parent!
102 * should always be parent's level + 1
104 private void addNodeToParentOneLevelDown(final T parent, final T id,
106 if (parent == null && level != 0) {
107 throw new TreeConfigurationException("Trying to add new id " + id
108 + " to top level with level != 0 (" + level + ")");
110 if (parent != null && manager.getLevel(parent) != level - 1) {
111 throw new TreeConfigurationException("Trying to add new id " + id
112 + " <" + level + "> to " + parent + " <"
113 + manager.getLevel(parent)
114 + ">. The difference in levels up is bigger than 1.");
116 manager.addAfterChild(parent, id, null);
117 setLastAdded(id, level);
120 private void setLastAdded(final T id, final int level) {