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