1. Tree Trace/Notify
A tree trace is used primarily to get control during
accesses to tree key values. A notify gets control
for other types tree operations. Following are some examples.
1.1 Readonly keys.
The following example can be used to make
a field read-only.
proc Readonly {t node key ops} {
if {[string match *c* $ops]} return; # CREATE
$t set $node $key [$t oldvalue]
return -code error "attempt to modify read only: $key"
}
set t [tree create]
$t trace create all a w Readonly
$t insert end -data {a 1 b 2}
catch {$t update 1 a 9} ;# kicks an error.
puts [$t get 1 a] ;# "1"
Note: if the trace is set after node data is initialized,
the first line in Readonly can be ommited.
1.2 Tag Trace
Tag uniqueness is a requirement
for single-node commands such as
get.
We can detect duplicate tags with a t trace,
forcing certain tag patterns
to be unique. eg,
proc ::TagTrace {t id key op} {
error "duplicate tag: $key"
}
$t trace create all .* t ::TagTrace
This trace generates an error for any
duplicate tag begining with a
period.
The check is very efficient, since the t trace fires only
when an exception occurs (ie. multiple nodes try to share a tag).
1.3 Unique Labels Without Tags
Reliable label addressing requires that
duplicate labels be avoided.
One way to prevent them is with a notifier
such as:
proc DupLab {t id ops} {
set l [$t label $id]
if {[string match {[0-9]*} $l]} {
return -code error "numeric label"
}
set labs [$t children -labels $id->parentnode]
if {[llength [lsearch -all -exact $labs $l]]>1} {
return -code error "label already exists: $l"
}
}
$t notifier create -create DupLab $t
1.4 Demand Loading Data
Data can be demand loaded into empty nodes upon access
with a -get notify.
This is invoked whenever a node with no
keys is accessed via get or with.
The notify call occurs before the read, eg.
set t [tree create]
$t insert end
$t insert end
$t insert end
proc FillMe {t id args} {
puts "FF [info level 0]"
$t set $id A 1 B 2
}
$t notify create -get FillMe $t
puts [$t get 1]
puts [$t get 2 B]
$t with p all {
parray p
}
Also see TreeView demand loading.
© 2008 Peter MacDonald