scoop:scoop.ui.component:0.1.0
src/tree/checks_cascade.scoop
Selected production source · src/tree/checks_cascade.scoop
1
package scoop.ui.component3
import scoop.collections.HashMap4
import scoop.ui.ViewKey6
internal class TreeCascadeSnapshot<T>(7
val data: TreeData<T>,8
var input: Array<ViewKey>,9
val states: HashMap<ViewKey, CheckState>,10
var value: TreeCheckValue11
) {12
fun state(key: ViewKey): CheckState = states[key] ?: CheckState.Unchecked13
}15
internal fun <T> treeCascadeSnapshot(data: TreeData<T>, input: Array<ViewKey>): TreeCascadeSnapshot<16
T17
> {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.Checked23
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 = true33
case None -> {}34
}35
states[row.node.key] = if (checked) CheckState.Checked else CheckState.Unchecked36
}37
var cursor = data.count38
while (cursor > 0L) {39
cursor -= 1L40
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
)62
}64
internal fun <T> treeAggregateCheck(65
data: TreeData<T>,66
index: Long,67
state: (ViewKey) -> CheckState68
): CheckState {69
val row = data.records[index]70
var participating = 0L71
var checked = 0L72
var partial = false73
for (child in row.children) {74
val node = data.records[child].node75
if (!node.canCheck) continue76
participating += 1L77
when (state(node.key)) {78
case Checked -> checked += 1L79
case Indeterminate -> partial = true80
case Unchecked -> {}81
}82
}83
if (participating == 0L) return state(row.node.key)84
if (checked == participating) return CheckState.Checked85
return if (checked > 0L || partial) CheckState.Indeterminate else CheckState.Unchecked86
}88
/** Stages only the affected branch and its conducting ancestors until the binding accepts it. */89
internal class TreeCascadeChange<T>(90
val previous: TreeCascadeSnapshot<T>,91
index: Long,92
checked: Boolean93
) {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.data99
val end = data.records[index].end100
var at = index101
while (at < end) {102
val row = data.records[at]103
if (!row.node.canCheck) {104
at = row.end105
continue106
}107
change(row.node.key, if (checked) CheckState.Checked else CheckState.Unchecked)108
at += 1L109
}110
var parent = data.records[index].parent111
while (true) {112
val ancestor = parent ?: break113
val row = data.records[ancestor]114
if (!row.node.canCheck) break115
change(row.node.key, treeAggregateCheck(data, ancestor, { key -> current(key) }))116
parent = row.parent117
}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] = state127
}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 = input141
previous.value = value142
}143
}