Scoop API

scoop:scoop.ui.component:0.1.0

src/tree/checks_cascade.scoop

Selected production source · src/tree/checks_cascade.scoop

1package scoop.ui.component
3import scoop.collections.HashMap
4import scoop.ui.ViewKey
6internal class TreeCascadeSnapshot<T>(
7 val data: TreeData<T>,
8 var input: Array<ViewKey>,
9 val states: HashMap<ViewKey, CheckState>,
10 var value: TreeCheckValue
11) {
12 fun state(key: ViewKey): CheckState = states[key] ?: CheckState.Unchecked
15internal fun <T> treeCascadeSnapshot(data: TreeData<T>, input: Array<ViewKey>): TreeCascadeSnapshot<
16 T
17> {
18 val keys = treeUniqueKeys(input)
19 val seeds = treeKeySet(keys)
20 val states = HashMap<ViewKey, CheckState>()
21 for (key in keys)
22 states[key] = CheckState.Checked
23 for (row in data.records) {
24 var checked = seeds.contains(row.node.key)
25 if (row.node.canCheck)
26 when (row.parent) {
27 case Some(val parent) ->
28 if (data.records[parent].node.canCheck &&
29 states[data.records[parent].node.key] ==
30 Some(CheckState.Checked)
31 )
32 checked = true
33 case None -> {}
34 }
35 states[row.node.key] = if (checked) CheckState.Checked else CheckState.Unchecked
36 }
37 var cursor = data.count
38 while (cursor > 0L) {
39 cursor -= 1L
40 val row = data.records[cursor]
41 if (row.node.canCheck)
42 states[row.node.key] = treeAggregateCheck(
43 data, cursor, { key -> states[key] ?: CheckState.Unchecked }
44 )
45 }
46 val checked = ArrayList<ViewKey>()
47 val half = ArrayList<ViewKey>()
48 for (key in keys)
49 if (data.indexOfKey(key) == None) checked.add(key)
50 for (row in data.records)
51 when (states[row.node.key]!!) {
52 case Checked -> checked.add(row.node.key)
53 case Indeterminate -> half.add(row.node.key)
54 case Unchecked -> {}
55 }
56 return TreeCascadeSnapshot(
57 data,
58 input,
59 states,
60 TreeCheckValue(checked.toArray(), half.toArray())
61 )
64internal fun <T> treeAggregateCheck(
65 data: TreeData<T>,
66 index: Long,
67 state: (ViewKey) -> CheckState
68): CheckState {
69 val row = data.records[index]
70 var participating = 0L
71 var checked = 0L
72 var partial = false
73 for (child in row.children) {
74 val node = data.records[child].node
75 if (!node.canCheck) continue
76 participating += 1L
77 when (state(node.key)) {
78 case Checked -> checked += 1L
79 case Indeterminate -> partial = true
80 case Unchecked -> {}
81 }
82 }
83 if (participating == 0L) return state(row.node.key)
84 if (checked == participating) return CheckState.Checked
85 return if (checked > 0L || partial) CheckState.Indeterminate else CheckState.Unchecked
88/** Stages only the affected branch and its conducting ancestors until the binding accepts it. */
89internal class TreeCascadeChange<T>(
90 val previous: TreeCascadeSnapshot<T>,
91 index: Long,
92 checked: Boolean
93) {
94 private val changes = HashMap<ViewKey, CheckState>()
95 private val order = ArrayList<ViewKey>()
96 val value = compute(index, checked)
97 private fun compute(index: Long, checked: Boolean): TreeCheckValue {
98 val data = previous.data
99 val end = data.records[index].end
100 var at = index
101 while (at < end) {
102 val row = data.records[at]
103 if (!row.node.canCheck) {
104 at = row.end
105 continue
106 }
107 change(row.node.key, if (checked) CheckState.Checked else CheckState.Unchecked)
108 at += 1L
109 }
110 var parent = data.records[index].parent
111 while (true) {
112 val ancestor = parent ?: break
113 val row = data.records[ancestor]
114 if (!row.node.canCheck) break
115 change(row.node.key, treeAggregateCheck(data, ancestor, { key -> current(key) }))
116 parent = row.parent
117 }
118 return TreeCheckValue(
119 updated(previous.value.checkedKeys, CheckState.Checked),
120 updated(previous.value.halfCheckedKeys, CheckState.Indeterminate)
121 )
122 }
123 private fun current(key: ViewKey): CheckState = changes[key] ?: previous.state(key)
124 private fun change(key: ViewKey, state: CheckState) {
125 if (changes[key] == None) order.add(key)
126 changes[key] = state
127 }
128 private fun updated(keys: Array<ViewKey>, state: CheckState): Array<ViewKey> {
129 val result = ArrayList<ViewKey>()
130 val retained = treeKeySet([])
131 for (key in keys)
132 if (current(key) == state && retained.add(key)) result.add(key)
133 for (key in order)
134 if (current(key) == state && retained.add(key)) result.add(key)
135 return result.toArray()
136 }
137 fun accept(input: Array<ViewKey>) {
138 for (key in order)
139 previous.states[key] = changes[key]!!
140 previous.input = input
141 previous.value = value
142 }