OLD | NEW |
| (Empty) |
1 class AbstractNode { | |
2 | |
3 // AbstractNode represents a node in a tree. | |
4 // The AbstractNode protocol is as follows: | |
5 // - When a subclass is changing the parent of a child, it should | |
6 // call either parent.adoptChild(child) or parent.dropChild(child) | |
7 // as appropriate. Subclasses should expose an API for | |
8 // manipulating the tree if you want to (e.g. a setter for a | |
9 // 'child' property, or an 'add()' method to manipulate a list). | |
10 // - You can see the current parent by querying 'parent'. | |
11 // - You can see the current attachment state by querying | |
12 // 'attached'. The root of any tree that is to be considered | |
13 // attached should be manually attached by calling 'attach()'. | |
14 // Other than that, don't call 'attach()' or 'detach()'. This is | |
15 // all managed automatically assuming you call the 'adoptChild()' | |
16 // and 'dropChild()' methods appropriately. | |
17 // - Subclasses that have children must override 'attach()' and | |
18 // 'detach()' as described below. | |
19 // - Nodes always have a 'depth' greater than their ancestors'. | |
20 // There's no guarantee regarding depth between siblings. The | |
21 // depth of a node is used to ensure that nodes are processed in | |
22 // depth order. The 'depth' of a child can be more than one | |
23 // greater than the 'depth' of the parent, because the 'depth' | |
24 // values are never decreased: all that matters is that it's | |
25 // greater than the parent. Consider a tree with a root node A, a | |
26 // child B, and a grandchild C. Initially, A will have 'depth' 0, | |
27 // B 'depth' 1, and C 'depth' 2. If C is moved to be a child of A, | |
28 // sibling of B, then the numbers won't change. C's 'depth' will | |
29 // still be 2. This is all managed automatically assuming you call | |
30 // 'adoptChild()' and 'dropChild()' appropriately. | |
31 | |
32 int _depth = 0; | |
33 int get depth => _depth; | |
34 void redepthChild(AbstractNode child) { // internal, do not call | |
35 assert(child._attached == _attached); | |
36 if (child._depth <= _depth) { | |
37 child._depth = _depth + 1; | |
38 child.redepthChildren(); | |
39 } | |
40 } | |
41 void redepthChildren() { // internal, do not call | |
42 // override this in subclasses with child nodes | |
43 // simply call redepthChild(child) for each child | |
44 } | |
45 | |
46 bool _attached = false; | |
47 bool get attached => _attached; | |
48 void attach() { | |
49 // override this in subclasses with child nodes | |
50 // simply call attach() for each child then call your superclass | |
51 _attached = true; | |
52 attachChildren(); | |
53 } | |
54 attachChildren() { } // workaround for lack of inter-class mixins in Dart | |
55 void detach() { | |
56 // override this in subclasses with child nodes | |
57 // simply call detach() for each child then call your superclass | |
58 _attached = false; | |
59 detachChildren(); | |
60 } | |
61 detachChildren() { } // workaround for lack of inter-class mixins in Dart | |
62 | |
63 AbstractNode _parent; | |
64 AbstractNode get parent => _parent; | |
65 void adoptChild(AbstractNode child) { // only for use by subclasses | |
66 assert(child != null); | |
67 assert(child._parent == null); | |
68 child._parent = this; | |
69 if (attached) | |
70 child.attach(); | |
71 redepthChild(child); | |
72 } | |
73 void dropChild(AbstractNode child) { // only for use by subclasses | |
74 assert(child != null); | |
75 assert(child._parent == this); | |
76 assert(child.attached == attached); | |
77 child._parent = null; | |
78 if (attached) | |
79 child.detach(); | |
80 } | |
81 | |
82 } | |
OLD | NEW |