author | Stefan Vogel <sv@exept.de> |
Wed, 29 Apr 2020 22:34:37 +0200 | |
changeset 4460 | e067415ddf46 |
parent 4457 | 8c328436fba9 |
permissions | -rw-r--r-- |
4457 | 1 |
"{ Encoding: utf8 }" |
2 |
||
145 | 3 |
" |
4 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
158 | 5 |
All Rights Reserved |
145 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
1665 | 14 |
"{ Package: 'stx:libview2' }" |
15 |
||
3474 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
145 | 18 |
ValueModel subclass:#BlockValue |
19 |
instanceVariableNames:'cachedValue arguments block' |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
20 |
classVariableNames:'NeverComputed' |
145 | 21 |
poolDictionaries:'' |
22 |
category:'Interface-Support-Models' |
|
23 |
! |
|
24 |
||
25 |
!BlockValue class methodsFor:'documentation'! |
|
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
158 | 30 |
All Rights Reserved |
145 | 31 |
|
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
||
40 |
! |
|
41 |
||
42 |
documentation |
|
43 |
" |
|
4257 | 44 |
BlockValues depend on one or more other objects (typically valueHolders) |
145 | 45 |
and recompute a value whenever one of them changes. |
4257 | 46 |
If the recomputed new value is different, it triggers itself a change to its dependents. |
145 | 47 |
|
4257 | 48 |
A typical example use is to base an enableChannel's value on multiple other |
49 |
boolean (or non-boolean) values. (See example for how this is done) |
|
223 | 50 |
|
4257 | 51 |
Notice, that a BlockValue can also be created without depending on any other |
52 |
object (i.e. BlockValue with:aBlock). In this case, a recomputation must be explicitly |
|
53 |
triggered by sending #recomputeValue to the blockValue instance. |
|
54 |
||
231 | 55 |
Notice: |
56 |
this class was implemented using protocol information |
|
57 |
from alpha testers - it may not be complete or compatible to |
|
58 |
the corresponding ST-80 class. |
|
59 |
If you encounter any incompatibilities, please forward a note |
|
60 |
describing the incompatibility verbal (i.e. no code) to the ST/X team. |
|
61 |
||
2760 | 62 |
Warning: |
63 |
BlockValues only work ine one direction; changing the blockValue's value |
|
64 |
does not affect the original model's value. |
|
65 |
||
223 | 66 |
[author:] |
67 |
Claus Gittinger |
|
145 | 68 |
" |
69 |
! |
|
70 |
||
71 |
examples |
|
72 |
" |
|
155 | 73 |
checkToggle 3 shows the value of toggle1 AND toggle2 |
231 | 74 |
[exBegin] |
75 |
|val1 val2 both box| |
|
155 | 76 |
|
231 | 77 |
val1 := false asValue. |
78 |
val2 := false asValue. |
|
79 |
both := BlockValue |
|
80 |
with:[:v1 :v2 | |
|
268 | 81 |
Transcript showCR:'evaluating ...'. |
231 | 82 |
v1 value and:[v2 value] |
83 |
] |
|
84 |
arguments:(Array with:val1 with:val2). |
|
155 | 85 |
|
231 | 86 |
box := Dialog new. |
87 |
box addCheckBox:'one' on:val1. |
|
88 |
box addCheckBox:'two' on:val2. |
|
89 |
box addHorizontalLine. |
|
90 |
box addCheckBox:'both' on:both. |
|
911 | 91 |
box addOkButton. |
231 | 92 |
box open |
93 |
[exEnd] |
|
155 | 94 |
|
95 |
the same, using a convenient instance creation message: |
|
231 | 96 |
[exBegin] |
97 |
|val1 val2 both box| |
|
155 | 98 |
|
231 | 99 |
val1 := false asValue. |
100 |
val2 := false asValue. |
|
101 |
both := BlockValue forLogical:val1 and:val2. |
|
145 | 102 |
|
231 | 103 |
box := Dialog new. |
104 |
box addCheckBox:'one' on:val1. |
|
105 |
box addCheckBox:'two' on:val2. |
|
106 |
box addHorizontalLine. |
|
911 | 107 |
(box addCheckBox:'both' on:both) disable. |
108 |
box addOkButton. |
|
231 | 109 |
box open |
110 |
[exEnd] |
|
155 | 111 |
|
112 |
logical or: |
|
231 | 113 |
[exBegin] |
114 |
|val1 val2 both box| |
|
155 | 115 |
|
231 | 116 |
val1 := false asValue. |
117 |
val2 := false asValue. |
|
118 |
both := BlockValue forLogical:val1 or:val2. |
|
145 | 119 |
|
231 | 120 |
box := Dialog new. |
121 |
box addCheckBox:'one' on:val1. |
|
122 |
box addCheckBox:'two' on:val2. |
|
123 |
box addHorizontalLine. |
|
124 |
box addCheckBox:'both' on:both. |
|
911 | 125 |
box addOkButton. |
231 | 126 |
box open |
127 |
[exEnd] |
|
155 | 128 |
|
129 |
example use: enabling an element depending on two others: |
|
231 | 130 |
[exBegin] |
131 |
|val1 val2 enabler val3 box| |
|
155 | 132 |
|
231 | 133 |
val1 := false asValue. |
134 |
val2 := false asValue. |
|
135 |
val3 := false asValue. |
|
136 |
enabler := BlockValue forLogical:val1 and:val2. |
|
155 | 137 |
|
231 | 138 |
box := Dialog new. |
139 |
box addCheckBox:'one' on:val1. |
|
140 |
box addCheckBox:'two' on:val2. |
|
141 |
box addHorizontalLine. |
|
911 | 142 |
(box addCheckBox:'three (both of the above)' on:val3) enableChannel:enabler. |
143 |
box addOkButton. |
|
231 | 144 |
box open |
145 |
[exEnd] |
|
382 | 146 |
|
147 |
||
148 |
like above, using a logical-or block: |
|
149 |
[exBegin] |
|
150 |
|val1 val2 enabler val3 box| |
|
151 |
||
152 |
val1 := false asValue. |
|
153 |
val2 := false asValue. |
|
154 |
val3 := false asValue. |
|
155 |
enabler := BlockValue forLogical:val1 or:val2. |
|
156 |
||
157 |
box := Dialog new. |
|
158 |
box addCheckBox:'one' on:val1. |
|
159 |
box addCheckBox:'two' on:val2. |
|
160 |
box addHorizontalLine. |
|
911 | 161 |
(box addCheckBox:'three (any of the above)' on:val3) enableChannel:enabler. |
162 |
box addOkButton. |
|
382 | 163 |
box open |
164 |
[exEnd] |
|
165 |
||
166 |
||
167 |
like above, using a bunch of toggles: |
|
168 |
[exBegin] |
|
169 |
|values anyValue box| |
|
170 |
||
171 |
values := (1 to:10) collect:[:i | false asValue]. |
|
172 |
anyValue := BlockValue forLogicalOrAll:values. |
|
173 |
anyValue onChangeSend:#value to:[Transcript showCR:'any is true']. |
|
174 |
||
175 |
box := Dialog new. |
|
176 |
values keysAndValuesDo:[:index :aValueHolder | |
|
177 |
box addCheckBox:index printString on:aValueHolder. |
|
178 |
]. |
|
179 |
box addHorizontalLine. |
|
911 | 180 |
(box addOkButton) enableChannel:anyValue. |
382 | 181 |
box open |
182 |
[exEnd] |
|
4257 | 183 |
a logical-not block: |
184 |
[exBegin] |
|
185 |
|val1 valNot box| |
|
186 |
||
187 |
val1 := false asValue. |
|
188 |
valNot := BlockValue forLogicalNot:val1. |
|
189 |
||
190 |
box := Dialog new. |
|
191 |
box addCheckBox:'one' on:val1. |
|
192 |
box addHorizontalLine. |
|
193 |
(box addCheckBox:'not one' on:valNot) enabled:false |
|
194 |
box addOkButton. |
|
195 |
box open |
|
196 |
[exEnd] |
|
197 |
||
198 |
||
145 | 199 |
" |
200 |
! ! |
|
201 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
202 |
!BlockValue class methodsFor:'initialization'! |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
203 |
|
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
204 |
initialize |
158 | 205 |
NeverComputed isNil ifTrue:[ |
206 |
NeverComputed := Object new. |
|
207 |
] |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
208 |
! ! |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
209 |
|
145 | 210 |
!BlockValue class methodsFor:'instance creation'! |
211 |
||
4457 | 212 |
block:aBlock arguments:aCollectionOfValueModels |
979
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
213 |
"return a new BlockValue computing aBlock. |
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
214 |
Same as #with:arguments: for ST80 compatibility" |
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
215 |
|
4457 | 216 |
^ self with:aBlock arguments:aCollectionOfValueModels |
979
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
217 |
|
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
218 |
"Created: / 20.6.1998 / 14:00:16 / cg" |
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
219 |
! |
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
220 |
|
4457 | 221 |
forLogical:valueModel1 and:valueModel2 |
1326 | 222 |
"return a new BlockValue computing the logical AND of its args |
223 |
(which are usually other valueHolders)" |
|
155 | 224 |
|
1815 | 225 |
^ (self new) |
1326 | 226 |
setBlock:[:a :b | a value and:[b value]] |
4457 | 227 |
arguments:(Array with:valueModel1 with:valueModel2) |
155 | 228 |
|
1326 | 229 |
"Created: / 16.12.1995 / 19:20:14 / cg" |
230 |
"Modified: / 30.1.2000 / 23:24:50 / cg" |
|
155 | 231 |
! |
232 |
||
4457 | 233 |
forLogical:valueModel1 and:valueModel2 and:valueModel3 |
2800 | 234 |
"return a new BlockValue computing the logical AND of its args |
235 |
(which are usually other valueHolders)" |
|
236 |
||
237 |
^ (self new) |
|
238 |
setBlock:[:a :b :c | a value and:[b value and:[c value]]] |
|
4457 | 239 |
arguments:(Array with:valueModel1 with:valueModel2 with:valueModel3) |
2800 | 240 |
! |
241 |
||
4457 | 242 |
forLogical:valueModel1 or:valueModel2 |
1326 | 243 |
"return a new BlockValue computing the logical OR of its args |
244 |
(which are usually other valueHolders)" |
|
155 | 245 |
|
1815 | 246 |
^ (self new) |
1326 | 247 |
setBlock:[:a :b | a value or:[b value]] |
4457 | 248 |
arguments:(Array with:valueModel1 with:valueModel2) |
155 | 249 |
|
1326 | 250 |
"Created: / 16.12.1995 / 19:20:14 / cg" |
251 |
"Modified: / 30.1.2000 / 23:24:46 / cg" |
|
155 | 252 |
! |
253 |
||
4457 | 254 |
forLogical:valueModel1 or:valueModel2 or:valueModel3 |
2800 | 255 |
"return a new BlockValue computing the logical OR of its args |
256 |
(which are usually other valueHolders)" |
|
257 |
||
258 |
^ (self new) |
|
259 |
setBlock:[:a :b :c | a value or:[b value or:[c value]]] |
|
4457 | 260 |
arguments:(Array with:valueModel1 with:valueModel2 with:valueModel3) |
2800 | 261 |
! |
262 |
||
4457 | 263 |
forLogicalAndAll:arrayOfValueModels |
3474 | 264 |
"return a new BlockValue computing the logical AND of all elements |
1326 | 265 |
in the passed argArray (which are usually other valueHolders)" |
382 | 266 |
|
1815 | 267 |
^ (self new) |
4457 | 268 |
setBlock:[:a | (arrayOfValueModels findFirst:[:a | a value not]) == 0] |
269 |
argumentArray:arrayOfValueModels asArray |
|
382 | 270 |
|
911 | 271 |
"Created: / 22.1.1997 / 19:12:44 / cg" |
272 |
"Modified: / 28.4.1998 / 20:16:38 / ca" |
|
1326 | 273 |
"Modified: / 30.1.2000 / 23:24:40 / cg" |
382 | 274 |
! |
275 |
||
4457 | 276 |
forLogicalNot:aValueModel |
1326 | 277 |
"return a new BlockValue computing the logical NOT of its arg |
278 |
(which is usually another valueHolder)" |
|
1045 | 279 |
|
1815 | 280 |
^ (self new) |
4260 | 281 |
setBlock:[:a | a not] |
4457 | 282 |
arguments:(Array with:aValueModel) |
1045 | 283 |
|
3349 | 284 |
" |
285 |
|v1 v2| |
|
286 |
||
287 |
v1 := ValueHolder new. |
|
288 |
v1 value:true. |
|
289 |
||
290 |
v2 := BlockValue forLogicalNot:v1. |
|
291 |
v2 onChangeEvaluate:[ Transcript showCR:v2 value]. |
|
292 |
Transcript showCR:v2 value. 'shows a false already'. |
|
293 |
||
294 |
v1 value:true. 'no change signalled here'. |
|
295 |
v1 value:false. |
|
296 |
v1 value:true. |
|
297 |
" |
|
298 |
||
4260 | 299 |
"Created: / 17-08-1998 / 10:06:41 / cg" |
300 |
"Modified: / 30-01-2000 / 23:24:18 / cg" |
|
301 |
"Modified: / 18-07-2018 / 20:33:18 / Claus Gittinger" |
|
1045 | 302 |
! |
303 |
||
4457 | 304 |
forLogicalOrAll:arrayOfValueModels |
382 | 305 |
"return a new BlockValue computing the logical OR of all elements |
1326 | 306 |
in the passed argArray (which are usually other valueHolders)" |
382 | 307 |
|
1815 | 308 |
^ (self new) |
606 | 309 |
setBlock:[:arg | (arg findFirst:[:el | el value]) ~~ 0] |
4457 | 310 |
argumentArray:arrayOfValueModels asArray |
382 | 311 |
|
911 | 312 |
"Created: / 22.1.1997 / 19:13:01 / cg" |
313 |
"Modified: / 28.4.1998 / 20:20:09 / ca" |
|
1326 | 314 |
"Modified: / 30.1.2000 / 23:24:34 / cg" |
382 | 315 |
! |
316 |
||
4457 | 317 |
forValue:someValueModel equalTo:someConstant |
3349 | 318 |
"return a new BlockValue generating a true, |
319 |
if someValue (usually another holder) contains a value equal to someConstant. |
|
320 |
Useful, if the other valueHolder is a radioButton group model, |
|
321 |
and you want to automatically enable/disable other items based on the value |
|
322 |
(i.e. via the enableChannel)" |
|
323 |
||
324 |
^ (self new) |
|
325 |
setBlock:[:a | a = someConstant] |
|
4457 | 326 |
arguments:(Array with:someValueModel) |
3349 | 327 |
|
328 |
" |
|
329 |
|v1 v2| |
|
330 |
||
331 |
v1 := ValueHolder new. |
|
332 |
v1 value:'blah'. |
|
333 |
||
334 |
v2 := BlockValue forValue:v1 equalTo:'hello'. |
|
335 |
v2 onChangeEvaluate:[ Transcript showCR:v2 value]. |
|
336 |
Transcript showCR:v2 value. 'shows a false already'. |
|
337 |
||
338 |
v1 value:'blah'. 'no change signalled here'. |
|
339 |
v1 value:'oops'. 'remains false'. |
|
340 |
v1 value:'hello'. 'changes to true'. |
|
341 |
v2 value |
|
342 |
" |
|
343 |
! |
|
344 |
||
145 | 345 |
with:aBlock |
346 |
"return a new BlockValue computing aBlock" |
|
347 |
||
1815 | 348 |
^ (self new) setBlock:aBlock |
145 | 349 |
|
350 |
"Created: 16.12.1995 / 19:16:33 / cg" |
|
351 |
! |
|
352 |
||
4457 | 353 |
with:aBlock argument:aValueModel |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
354 |
"return a new BlockValue computing aBlock" |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
355 |
|
1815 | 356 |
^ (self new) |
1352 | 357 |
setBlock:aBlock |
4457 | 358 |
arguments:(Array with:aValueModel) |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
359 |
|
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
360 |
"Created: 16.12.1995 / 19:20:14 / cg" |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
361 |
! |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
362 |
|
4457 | 363 |
with:aBlock argument:valueModel1 argument:valueModel2 |
1255 | 364 |
"return a new BlockValue computing aBlock" |
365 |
||
1815 | 366 |
^ (self new) |
1352 | 367 |
setBlock:aBlock |
4457 | 368 |
arguments:(Array with:valueModel1 with:valueModel2) |
1255 | 369 |
|
370 |
"Created: 16.12.1995 / 19:20:14 / cg" |
|
371 |
! |
|
372 |
||
4457 | 373 |
with:aBlock argument:valueModel1 argument:valueModel2 argument:valueModel3 |
1352 | 374 |
"return a new BlockValue computing aBlock" |
375 |
||
1815 | 376 |
^ (self new) |
1352 | 377 |
setBlock:aBlock |
4457 | 378 |
arguments:(Array with:valueModel1 with:valueModel2 with:valueModel3) |
1815 | 379 |
! |
1352 | 380 |
|
4457 | 381 |
with:aBlock argument:valueModel1 argument:valueModel2 argument:valueModel3 argument:valueModel4 |
1815 | 382 |
"return a new BlockValue computing aBlock" |
383 |
||
384 |
^ (self new) |
|
385 |
setBlock:aBlock |
|
4457 | 386 |
arguments:(Array with:valueModel1 with:valueModel2 with:valueModel3 with:valueModel4) |
1352 | 387 |
! |
388 |
||
4457 | 389 |
with:aBlock arguments:aCollectionOfValueModels |
145 | 390 |
"return a new BlockValue computing aBlock" |
391 |
||
1815 | 392 |
^ (self new) |
1352 | 393 |
setBlock:aBlock |
4457 | 394 |
arguments:aCollectionOfValueModels |
145 | 395 |
|
396 |
"Created: 16.12.1995 / 19:20:14 / cg" |
|
397 |
! ! |
|
398 |
||
399 |
!BlockValue methodsFor:'accessing'! |
|
400 |
||
3945 | 401 |
setArguments:aCollectionOfArguments |
402 |
"set the receiver's arguments to be passed to it. |
|
403 |
A change in any of the arguments will force reevaluation of the action |
|
404 |
block - possibly generating another change from myself" |
|
405 |
||
406 |
arguments notNil ifTrue:[ |
|
3946 | 407 |
arguments do:[:arg | arg removeDependent:self]. |
3945 | 408 |
]. |
409 |
arguments := aCollectionOfArguments. |
|
410 |
arguments do:[:arg | |
|
411 |
"arg maybe some constant..." |
|
412 |
arg notNil ifTrue:[ |
|
413 |
arg addDependent:self |
|
414 |
]. |
|
415 |
]. |
|
416 |
self recomputeValue. |
|
417 |
||
418 |
"Created: / 30-03-2017 / 14:00:27 / stefan" |
|
3946 | 419 |
"Modified: / 31-03-2017 / 16:28:39 / stefan" |
3945 | 420 |
! |
421 |
||
145 | 422 |
setBlock:aBlock |
3613 | 423 |
"set the receiver's action block" |
382 | 424 |
|
145 | 425 |
block := aBlock. |
426 |
arguments notNil ifTrue:[ |
|
382 | 427 |
self release |
145 | 428 |
]. |
429 |
arguments := nil. |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
430 |
cachedValue := NeverComputed. |
145 | 431 |
|
432 |
"Created: 16.12.1995 / 19:16:59 / cg" |
|
382 | 433 |
"Modified: 22.1.1997 / 19:05:54 / cg" |
434 |
! |
|
435 |
||
436 |
setBlock:aBlock argumentArray:anArgumentCollection |
|
3613 | 437 |
"set the receiver's action block, and define an arguments collection |
382 | 438 |
to be passed to it. |
439 |
A change in any element of the collection will force reevaluation of the |
|
440 |
action block (passing the collection as a single argument) |
|
441 |
- possibly generating another change from myself" |
|
442 |
||
443 |
block := aBlock. |
|
444 |
arguments notNil ifTrue:[ |
|
445 |
self release |
|
446 |
]. |
|
447 |
arguments := Array with:anArgumentCollection. |
|
448 |
anArgumentCollection do:[:arg | |
|
2530 | 449 |
arg notNil ifTrue:[ |
450 |
arg addDependent:self |
|
451 |
]. |
|
382 | 452 |
]. |
453 |
cachedValue := NeverComputed. |
|
454 |
||
455 |
"Modified: 22.1.1997 / 19:08:51 / cg" |
|
456 |
"Created: 22.1.1997 / 19:22:13 / cg" |
|
145 | 457 |
! |
458 |
||
459 |
setBlock:aBlock arguments:aCollectionOfArguments |
|
3613 | 460 |
"set the receiver's action block, and define arguments to be passed to it. |
382 | 461 |
A change in any of the arguments will force reevaluation of the action |
462 |
block - possibly generating another change from myself" |
|
463 |
||
145 | 464 |
block := aBlock. |
465 |
arguments notNil ifTrue:[ |
|
382 | 466 |
self release |
145 | 467 |
]. |
468 |
arguments := aCollectionOfArguments. |
|
469 |
arguments do:[:arg | |
|
2530 | 470 |
"arg maybe some constant..." |
471 |
arg notNil ifTrue:[ |
|
472 |
arg addDependent:self |
|
473 |
]. |
|
145 | 474 |
]. |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
475 |
cachedValue := NeverComputed. |
145 | 476 |
|
477 |
"Created: 16.12.1995 / 19:21:41 / cg" |
|
382 | 478 |
"Modified: 22.1.1997 / 19:08:51 / cg" |
145 | 479 |
! |
480 |
||
155 | 481 |
setValue:newValue |
482 |
"physically set my value, without change notifications. |
|
483 |
This is a noop here, since my value is computed." |
|
484 |
||
485 |
^ self |
|
486 |
||
487 |
||
488 |
! |
|
489 |
||
145 | 490 |
value |
382 | 491 |
"retrieve my value - this does not always evaluate the action block, |
492 |
since the returned value is cached internally" |
|
493 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
494 |
cachedValue == NeverComputed ifTrue:[ |
491 | 495 |
cachedValue := self computeValue |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
496 |
]. |
145 | 497 |
^ cachedValue |
498 |
||
499 |
"Created: 16.12.1995 / 19:23:26 / cg" |
|
382 | 500 |
"Modified: 22.1.1997 / 19:06:59 / cg" |
145 | 501 |
! ! |
502 |
||
347 | 503 |
!BlockValue methodsFor:'change & update'! |
145 | 504 |
|
1788 | 505 |
recomputeValue |
4242 | 506 |
"force reevaluation of my actionBlock, |
507 |
and possibly send a change notification to my dependents" |
|
382 | 508 |
|
145 | 509 |
|oldValue| |
510 |
||
511 |
oldValue := cachedValue. |
|
491 | 512 |
cachedValue := self computeValue. |
145 | 513 |
oldValue ~~ cachedValue ifTrue:[ |
382 | 514 |
self changed:#value |
145 | 515 |
]. |
4242 | 516 |
|
517 |
"Modified (comment): / 05-03-2019 / 21:40:21 / Claus Gittinger" |
|
1788 | 518 |
! |
519 |
||
520 |
update:something with:aParameter from:someone |
|
521 |
"the one I depend on has changed - reevaluate my actionBlock, |
|
522 |
and possibly send a change notification to my dependents" |
|
523 |
||
524 |
self recomputeValue |
|
145 | 525 |
|
526 |
"Created: 16.12.1995 / 19:22:54 / cg" |
|
382 | 527 |
"Modified: 22.1.1997 / 19:07:39 / cg" |
145 | 528 |
! ! |
529 |
||
2985 | 530 |
!BlockValue methodsFor:'dependents access'! |
531 |
||
532 |
release |
|
533 |
"release any dependencies upon the arguments" |
|
534 |
||
535 |
arguments notNil ifTrue:[ |
|
536 |
arguments do:[:arg | arg removeDependent:self]. |
|
537 |
]. |
|
3291 | 538 |
super release |
2985 | 539 |
|
540 |
"Modified: 22.1.1997 / 19:08:06 / cg" |
|
541 |
! ! |
|
542 |
||
1788 | 543 |
!BlockValue methodsFor:'misc'! |
544 |
||
545 |
computeValue |
|
3613 | 546 |
"evaluate the receiver's action block" |
1788 | 547 |
|
548 |
|sz arg1 arg2 argValues| |
|
549 |
||
550 |
arguments isNil ifTrue:[ |
|
551 |
^ block value |
|
552 |
]. |
|
553 |
sz := arguments size. |
|
554 |
sz == 0 ifTrue:[ |
|
555 |
^ block value |
|
556 |
]. |
|
557 |
arg1 := (arguments at:1) value. |
|
558 |
sz == 1 ifTrue:[ |
|
559 |
^ block value:arg1 |
|
560 |
]. |
|
561 |
arg2 := (arguments at:2) value. |
|
562 |
sz == 2 ifTrue:[ |
|
563 |
^ block value:arg1 value:arg2 |
|
564 |
]. |
|
565 |
"/ do not evaluate arg[1-2] twice |
|
566 |
argValues := Array new:arguments size. |
|
567 |
argValues at:1 put:arg1. "/ do not evaluate twice |
|
568 |
argValues at:2 put:arg2. "/ do not evaluate twice |
|
569 |
3 to:arguments size do:[:i | argValues at:i put:(arguments at:i) value]. |
|
570 |
||
571 |
^ block valueWithArguments:argValues |
|
572 |
||
573 |
"Created: 16.12.1995 / 19:27:40 / cg" |
|
574 |
"Modified: 22.1.1997 / 19:05:57 / cg" |
|
575 |
! |
|
576 |
||
577 |
dependOn:someObject |
|
578 |
"arrange for the blockValue to be reevaluated, whenever someObject |
|
579 |
changes (i.e. sends a change notification)" |
|
580 |
||
581 |
arguments isNil ifTrue:[ |
|
582 |
arguments := Array with:someObject |
|
583 |
] ifFalse:[ |
|
584 |
arguments := arguments copyWith:someObject |
|
585 |
]. |
|
586 |
someObject addDependent:self |
|
587 |
||
588 |
"Modified: 22.1.1997 / 19:05:26 / cg" |
|
589 |
! |
|
590 |
||
591 |
resetValue |
|
3613 | 592 |
"evaluate the receiver's action block" |
1788 | 593 |
|
594 |
cachedValue := self computeValue |
|
595 |
! ! |
|
596 |
||
145 | 597 |
!BlockValue class methodsFor:'documentation'! |
598 |
||
599 |
version |
|
3613 | 600 |
^ '$Header$' |
2760 | 601 |
! |
602 |
||
603 |
version_CVS |
|
3613 | 604 |
^ '$Header$' |
145 | 605 |
! ! |
1665 | 606 |
|
3291 | 607 |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
608 |
BlockValue initialize! |