forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_internal_node.go
175 lines (152 loc) · 4.05 KB
/
tree_internal_node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mixins
import (
"fmt"
"github.com/google/gxui"
)
type TreeInternalNode struct {
adapterNode gxui.TreeAdapterNode
childCount int
children []*TreeInternalNode
isExpanded bool
id gxui.AdapterItemId
parent *TreeInternalNode
onExpandChanged gxui.Event
}
func (n *TreeInternalNode) findByIndex(absIdx int, d int) (parent *TreeInternalNode, relIndex int, depth int) {
i := absIdx
for j, c := range n.children {
switch {
case i == 0:
return n, j, d
case i <= c.childCount:
return c.findByIndex(i-1, d+1)
default:
i -= c.childCount + 1
}
}
panic(fmt.Errorf("Node does not contain index %d", absIdx))
return nil, 0, 0
}
func (n *TreeInternalNode) findById(id gxui.AdapterItemId, baseIdx, d int) (parent *TreeInternalNode, relIdx, absIdx, depth int) {
relIdx = n.adapterNode.ItemIndex(id)
if relIdx < 0 {
panic(fmt.Errorf("Node does not contain id %d", id))
}
if relIdx < 0 || relIdx >= len(n.children) {
panic(fmt.Errorf(
"%T.ItemIndex(%v) returned out of bounds index %v. Acceptable range: [%d - %d]",
n.adapterNode, id, relIdx, 0, len(n.children)-1))
}
absIdx = baseIdx + 1 // +1 for n
for i := 0; i < relIdx; i++ {
absIdx += n.children[i].childCount + 1
}
if n.adapterNode.ItemId(relIdx) == id {
return n, relIdx, absIdx, d
}
if n.children[relIdx].IsExpanded() {
return n.children[relIdx].findById(id, absIdx, d+1)
} else {
return n, relIdx, absIdx, d
}
}
func CreateTreeInternalRoot(adapterNode gxui.TreeAdapterNode) *TreeInternalNode {
root := &TreeInternalNode{
adapterNode: adapterNode,
id: gxui.InvalidAdapterItemId,
}
root.Expand()
return root
}
func (n *TreeInternalNode) OnExpandedChanged(f func(bool)) gxui.EventSubscription {
if n.onExpandChanged == nil {
n.onExpandChanged = gxui.CreateEvent(func(bool) {})
}
return n.onExpandChanged.Listen(f)
}
func (n *TreeInternalNode) Id() gxui.AdapterItemId {
return n.id
}
func (n *TreeInternalNode) IsExpanded() bool {
return n.isExpanded
}
func (n *TreeInternalNode) IsLeaf() bool {
return n.adapterNode == nil
}
func (n *TreeInternalNode) Expand() bool {
if n.isExpanded || n.IsLeaf() {
return false
}
n.isExpanded = true
n.childCount = n.adapterNode.Count()
if n.childCount < 0 {
panic(fmt.Errorf("%T.Count() returned a negative value %d", n.adapterNode, n.childCount))
}
n.children = make([]*TreeInternalNode, n.childCount)
for i := range n.children {
n.children[i] = &TreeInternalNode{
adapterNode: n.adapterNode.CreateNode(i),
id: n.adapterNode.ItemId(i),
parent: n,
}
}
p := n.parent
for p != nil {
p.childCount += n.childCount
p = p.parent
}
if n.onExpandChanged != nil {
n.onExpandChanged.Fire(true)
}
return true
}
func (n *TreeInternalNode) Collapse() bool {
if !n.isExpanded || n.IsLeaf() {
return false
}
n.isExpanded = false
p := n.parent
for p != nil {
p.childCount -= n.childCount
p = p.parent
}
n.childCount = 0
n.children = nil
if n.onExpandChanged != nil {
n.onExpandChanged.Fire(false)
}
return true
}
func (n *TreeInternalNode) ExpandAll() {
n.Expand()
for _, c := range n.children {
c.ExpandAll()
}
}
func (n *TreeInternalNode) CollapseAll() {
n.Collapse()
for _, c := range n.children {
c.CollapseAll()
}
}
func (n *TreeInternalNode) Child(i int) (parent *TreeInternalNode) {
return n.children[i]
}
func (n *TreeInternalNode) FindByIndex(idx int) (parent *TreeInternalNode, childIndex int, depth int) {
return n.findByIndex(idx, 0)
}
func (n *TreeInternalNode) FindById(id gxui.AdapterItemId) (parent *TreeInternalNode, childIndex int, depth int) {
c, relIdx, _, depth := n.findById(id, -1, 0)
return c, relIdx, depth
}
func (n *TreeInternalNode) ItemId(idx int) gxui.AdapterItemId {
p, i, _ := n.FindByIndex(idx)
return p.adapterNode.ItemId(i)
}
func (n *TreeInternalNode) ItemIndex(id gxui.AdapterItemId) int {
_, _, absIdx, _ := n.findById(id, -1, 0)
return absIdx
}