author | Claus Gittinger <cg@exept.de> |
Tue, 30 Apr 2019 20:07:57 +0200 | |
changeset 4258 | 5fc4985b0d74 |
parent 4257 | 2f858dbfd25b |
child 4260 | edc649421747 |
permissions | -rw-r--r-- |
4242 | 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 |
||
979
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
212 |
block:aBlock arguments:aCollectionOfArguments |
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 |
|
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
216 |
^ self with:aBlock arguments:aCollectionOfArguments |
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 |
|
155 | 221 |
forLogical:arg1 and:arg2 |
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]] |
227 |
arguments:(Array with:arg1 with:arg2) |
|
155 | 228 |
|
1326 | 229 |
"Created: / 16.12.1995 / 19:20:14 / cg" |
230 |
"Modified: / 30.1.2000 / 23:24:50 / cg" |
|
155 | 231 |
! |
232 |
||
2800 | 233 |
forLogical:arg1 and:arg2 and:arg3 |
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]]] |
|
239 |
arguments:(Array with:arg1 with:arg2 with:arg3) |
|
240 |
! |
|
241 |
||
155 | 242 |
forLogical:arg1 or:arg2 |
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]] |
248 |
arguments:(Array with:arg1 with:arg2) |
|
155 | 249 |
|
1326 | 250 |
"Created: / 16.12.1995 / 19:20:14 / cg" |
251 |
"Modified: / 30.1.2000 / 23:24:46 / cg" |
|
155 | 252 |
! |
253 |
||
2800 | 254 |
forLogical:arg1 or:arg2 or:arg3 |
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]]] |
|
260 |
arguments:(Array with:arg1 with:arg2 with:arg3) |
|
261 |
! |
|
262 |
||
382 | 263 |
forLogicalAndAll:argArray |
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) |
382 | 268 |
setBlock:[:a | (argArray findFirst:[:a | a value not]) == 0] |
911 | 269 |
argumentArray:argArray 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 |
||
1045 | 276 |
forLogicalNot:arg |
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) |
1045 | 281 |
setBlock:[:a | a value not] |
282 |
arguments:(Array with:arg) |
|
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 |
||
1045 | 299 |
"Created: / 17.8.1998 / 10:06:41 / cg" |
1326 | 300 |
"Modified: / 30.1.2000 / 23:24:18 / cg" |
1045 | 301 |
! |
302 |
||
382 | 303 |
forLogicalOrAll:argArray |
304 |
"return a new BlockValue computing the logical OR of all elements |
|
1326 | 305 |
in the passed argArray (which are usually other valueHolders)" |
382 | 306 |
|
1815 | 307 |
^ (self new) |
606 | 308 |
setBlock:[:arg | (arg findFirst:[:el | el value]) ~~ 0] |
911 | 309 |
argumentArray:argArray asArray |
382 | 310 |
|
911 | 311 |
"Created: / 22.1.1997 / 19:13:01 / cg" |
312 |
"Modified: / 28.4.1998 / 20:20:09 / ca" |
|
1326 | 313 |
"Modified: / 30.1.2000 / 23:24:34 / cg" |
382 | 314 |
! |
315 |
||
3349 | 316 |
forValue:someValue equalTo:someConstant |
317 |
"return a new BlockValue generating a true, |
|
318 |
if someValue (usually another holder) contains a value equal to someConstant. |
|
319 |
Useful, if the other valueHolder is a radioButton group model, |
|
320 |
and you want to automatically enable/disable other items based on the value |
|
321 |
(i.e. via the enableChannel)" |
|
322 |
||
323 |
^ (self new) |
|
324 |
setBlock:[:a | a = someConstant] |
|
325 |
arguments:(Array with:someValue) |
|
326 |
||
327 |
" |
|
328 |
|v1 v2| |
|
329 |
||
330 |
v1 := ValueHolder new. |
|
331 |
v1 value:'blah'. |
|
332 |
||
333 |
v2 := BlockValue forValue:v1 equalTo:'hello'. |
|
334 |
v2 onChangeEvaluate:[ Transcript showCR:v2 value]. |
|
335 |
Transcript showCR:v2 value. 'shows a false already'. |
|
336 |
||
337 |
v1 value:'blah'. 'no change signalled here'. |
|
338 |
v1 value:'oops'. 'remains false'. |
|
339 |
v1 value:'hello'. 'changes to true'. |
|
340 |
v2 value |
|
341 |
" |
|
342 |
! |
|
343 |
||
145 | 344 |
with:aBlock |
345 |
"return a new BlockValue computing aBlock" |
|
346 |
||
1815 | 347 |
^ (self new) setBlock:aBlock |
145 | 348 |
|
349 |
"Created: 16.12.1995 / 19:16:33 / cg" |
|
350 |
! |
|
351 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
352 |
with:aBlock argument:anArgument |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
353 |
"return a new BlockValue computing aBlock" |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
354 |
|
1815 | 355 |
^ (self new) |
1352 | 356 |
setBlock:aBlock |
357 |
arguments:(Array with:anArgument) |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
358 |
|
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
359 |
"Created: 16.12.1995 / 19:20:14 / cg" |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
360 |
! |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
361 |
|
1255 | 362 |
with:aBlock argument:anArgument1 argument:anArgument2 |
363 |
"return a new BlockValue computing aBlock" |
|
364 |
||
1815 | 365 |
^ (self new) |
1352 | 366 |
setBlock:aBlock |
367 |
arguments:(Array with:anArgument1 with:anArgument2) |
|
1255 | 368 |
|
369 |
"Created: 16.12.1995 / 19:20:14 / cg" |
|
370 |
! |
|
371 |
||
1352 | 372 |
with:aBlock argument:anArgument1 argument:anArgument2 argument:anArgument3 |
373 |
"return a new BlockValue computing aBlock" |
|
374 |
||
1815 | 375 |
^ (self new) |
1352 | 376 |
setBlock:aBlock |
377 |
arguments:(Array with:anArgument1 with:anArgument2 with:anArgument3) |
|
1815 | 378 |
! |
1352 | 379 |
|
1815 | 380 |
with:aBlock argument:anArgument1 argument:anArgument2 argument:anArgument3 argument:anArgument4 |
381 |
"return a new BlockValue computing aBlock" |
|
382 |
||
383 |
^ (self new) |
|
384 |
setBlock:aBlock |
|
385 |
arguments:(Array with:anArgument1 with:anArgument2 with:anArgument3 with:anArgument4) |
|
1352 | 386 |
! |
387 |
||
145 | 388 |
with:aBlock arguments:aCollectionOfArguments |
389 |
"return a new BlockValue computing aBlock" |
|
390 |
||
1815 | 391 |
^ (self new) |
1352 | 392 |
setBlock:aBlock |
393 |
arguments:aCollectionOfArguments |
|
145 | 394 |
|
395 |
"Created: 16.12.1995 / 19:20:14 / cg" |
|
396 |
! ! |
|
397 |
||
398 |
!BlockValue methodsFor:'accessing'! |
|
399 |
||
3945 | 400 |
setArguments:aCollectionOfArguments |
401 |
"set the receiver's arguments to be passed to it. |
|
402 |
A change in any of the arguments will force reevaluation of the action |
|
403 |
block - possibly generating another change from myself" |
|
404 |
||
405 |
arguments notNil ifTrue:[ |
|
3946 | 406 |
arguments do:[:arg | arg removeDependent:self]. |
3945 | 407 |
]. |
408 |
arguments := aCollectionOfArguments. |
|
409 |
arguments do:[:arg | |
|
410 |
"arg maybe some constant..." |
|
411 |
arg notNil ifTrue:[ |
|
412 |
arg addDependent:self |
|
413 |
]. |
|
414 |
]. |
|
415 |
self recomputeValue. |
|
416 |
||
417 |
"Created: / 30-03-2017 / 14:00:27 / stefan" |
|
3946 | 418 |
"Modified: / 31-03-2017 / 16:28:39 / stefan" |
3945 | 419 |
! |
420 |
||
145 | 421 |
setBlock:aBlock |
3613 | 422 |
"set the receiver's action block" |
382 | 423 |
|
145 | 424 |
block := aBlock. |
425 |
arguments notNil ifTrue:[ |
|
382 | 426 |
self release |
145 | 427 |
]. |
428 |
arguments := nil. |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
429 |
cachedValue := NeverComputed. |
145 | 430 |
|
431 |
"Created: 16.12.1995 / 19:16:59 / cg" |
|
382 | 432 |
"Modified: 22.1.1997 / 19:05:54 / cg" |
433 |
! |
|
434 |
||
435 |
setBlock:aBlock argumentArray:anArgumentCollection |
|
3613 | 436 |
"set the receiver's action block, and define an arguments collection |
382 | 437 |
to be passed to it. |
438 |
A change in any element of the collection will force reevaluation of the |
|
439 |
action block (passing the collection as a single argument) |
|
440 |
- possibly generating another change from myself" |
|
441 |
||
442 |
block := aBlock. |
|
443 |
arguments notNil ifTrue:[ |
|
444 |
self release |
|
445 |
]. |
|
446 |
arguments := Array with:anArgumentCollection. |
|
447 |
anArgumentCollection do:[:arg | |
|
2530 | 448 |
arg notNil ifTrue:[ |
449 |
arg addDependent:self |
|
450 |
]. |
|
382 | 451 |
]. |
452 |
cachedValue := NeverComputed. |
|
453 |
||
454 |
"Modified: 22.1.1997 / 19:08:51 / cg" |
|
455 |
"Created: 22.1.1997 / 19:22:13 / cg" |
|
145 | 456 |
! |
457 |
||
458 |
setBlock:aBlock arguments:aCollectionOfArguments |
|
3613 | 459 |
"set the receiver's action block, and define arguments to be passed to it. |
382 | 460 |
A change in any of the arguments will force reevaluation of the action |
461 |
block - possibly generating another change from myself" |
|
462 |
||
145 | 463 |
block := aBlock. |
464 |
arguments notNil ifTrue:[ |
|
382 | 465 |
self release |
145 | 466 |
]. |
467 |
arguments := aCollectionOfArguments. |
|
468 |
arguments do:[:arg | |
|
2530 | 469 |
"arg maybe some constant..." |
470 |
arg notNil ifTrue:[ |
|
471 |
arg addDependent:self |
|
472 |
]. |
|
145 | 473 |
]. |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
474 |
cachedValue := NeverComputed. |
145 | 475 |
|
476 |
"Created: 16.12.1995 / 19:21:41 / cg" |
|
382 | 477 |
"Modified: 22.1.1997 / 19:08:51 / cg" |
145 | 478 |
! |
479 |
||
155 | 480 |
setValue:newValue |
481 |
"physically set my value, without change notifications. |
|
482 |
This is a noop here, since my value is computed." |
|
483 |
||
484 |
^ self |
|
485 |
||
486 |
||
487 |
! |
|
488 |
||
145 | 489 |
value |
382 | 490 |
"retrieve my value - this does not always evaluate the action block, |
491 |
since the returned value is cached internally" |
|
492 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
493 |
cachedValue == NeverComputed ifTrue:[ |
491 | 494 |
cachedValue := self computeValue |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
495 |
]. |
145 | 496 |
^ cachedValue |
497 |
||
498 |
"Created: 16.12.1995 / 19:23:26 / cg" |
|
382 | 499 |
"Modified: 22.1.1997 / 19:06:59 / cg" |
145 | 500 |
! ! |
501 |
||
347 | 502 |
!BlockValue methodsFor:'change & update'! |
145 | 503 |
|
1788 | 504 |
recomputeValue |
4242 | 505 |
"force reevaluation of my actionBlock, |
506 |
and possibly send a change notification to my dependents" |
|
382 | 507 |
|
145 | 508 |
|oldValue| |
509 |
||
510 |
oldValue := cachedValue. |
|
491 | 511 |
cachedValue := self computeValue. |
145 | 512 |
oldValue ~~ cachedValue ifTrue:[ |
382 | 513 |
self changed:#value |
145 | 514 |
]. |
4242 | 515 |
|
516 |
"Modified (comment): / 05-03-2019 / 21:40:21 / Claus Gittinger" |
|
1788 | 517 |
! |
518 |
||
519 |
update:something with:aParameter from:someone |
|
520 |
"the one I depend on has changed - reevaluate my actionBlock, |
|
521 |
and possibly send a change notification to my dependents" |
|
522 |
||
523 |
self recomputeValue |
|
145 | 524 |
|
525 |
"Created: 16.12.1995 / 19:22:54 / cg" |
|
382 | 526 |
"Modified: 22.1.1997 / 19:07:39 / cg" |
145 | 527 |
! ! |
528 |
||
2985 | 529 |
!BlockValue methodsFor:'dependents access'! |
530 |
||
531 |
release |
|
532 |
"release any dependencies upon the arguments" |
|
533 |
||
534 |
arguments notNil ifTrue:[ |
|
535 |
arguments do:[:arg | arg removeDependent:self]. |
|
536 |
]. |
|
3291 | 537 |
super release |
2985 | 538 |
|
539 |
"Modified: 22.1.1997 / 19:08:06 / cg" |
|
540 |
! ! |
|
541 |
||
1788 | 542 |
!BlockValue methodsFor:'misc'! |
543 |
||
544 |
computeValue |
|
3613 | 545 |
"evaluate the receiver's action block" |
1788 | 546 |
|
547 |
|sz arg1 arg2 argValues| |
|
548 |
||
549 |
arguments isNil ifTrue:[ |
|
550 |
^ block value |
|
551 |
]. |
|
552 |
sz := arguments size. |
|
553 |
sz == 0 ifTrue:[ |
|
554 |
^ block value |
|
555 |
]. |
|
556 |
arg1 := (arguments at:1) value. |
|
557 |
sz == 1 ifTrue:[ |
|
558 |
^ block value:arg1 |
|
559 |
]. |
|
560 |
arg2 := (arguments at:2) value. |
|
561 |
sz == 2 ifTrue:[ |
|
562 |
^ block value:arg1 value:arg2 |
|
563 |
]. |
|
564 |
"/ do not evaluate arg[1-2] twice |
|
565 |
argValues := Array new:arguments size. |
|
566 |
argValues at:1 put:arg1. "/ do not evaluate twice |
|
567 |
argValues at:2 put:arg2. "/ do not evaluate twice |
|
568 |
3 to:arguments size do:[:i | argValues at:i put:(arguments at:i) value]. |
|
569 |
||
570 |
^ block valueWithArguments:argValues |
|
571 |
||
572 |
"Created: 16.12.1995 / 19:27:40 / cg" |
|
573 |
"Modified: 22.1.1997 / 19:05:57 / cg" |
|
574 |
! |
|
575 |
||
576 |
dependOn:someObject |
|
577 |
"arrange for the blockValue to be reevaluated, whenever someObject |
|
578 |
changes (i.e. sends a change notification)" |
|
579 |
||
580 |
arguments isNil ifTrue:[ |
|
581 |
arguments := Array with:someObject |
|
582 |
] ifFalse:[ |
|
583 |
arguments := arguments copyWith:someObject |
|
584 |
]. |
|
585 |
someObject addDependent:self |
|
586 |
||
587 |
"Modified: 22.1.1997 / 19:05:26 / cg" |
|
588 |
! |
|
589 |
||
590 |
resetValue |
|
3613 | 591 |
"evaluate the receiver's action block" |
1788 | 592 |
|
593 |
cachedValue := self computeValue |
|
594 |
! ! |
|
595 |
||
145 | 596 |
!BlockValue class methodsFor:'documentation'! |
597 |
||
598 |
version |
|
3613 | 599 |
^ '$Header$' |
2760 | 600 |
! |
601 |
||
602 |
version_CVS |
|
3613 | 603 |
^ '$Header$' |
145 | 604 |
! ! |
1665 | 605 |
|
3291 | 606 |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
607 |
BlockValue initialize! |