author | Claus Gittinger <cg@exept.de> |
Thu, 24 Oct 2002 21:16:06 +0200 | |
changeset 1646 | da8d044c7c0d |
parent 1352 | cd83d6348a36 |
child 1665 | 096ca1b11ce1 |
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 |
" |
|
12 |
||
13 |
||
14 |
ValueModel subclass:#BlockValue |
|
15 |
instanceVariableNames:'cachedValue arguments block' |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
16 |
classVariableNames:'NeverComputed' |
145 | 17 |
poolDictionaries:'' |
18 |
category:'Interface-Support-Models' |
|
19 |
! |
|
20 |
||
21 |
!BlockValue class methodsFor:'documentation'! |
|
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
158 | 26 |
All Rights Reserved |
145 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
||
36 |
! |
|
37 |
||
38 |
documentation |
|
39 |
" |
|
40 |
BlockValues depend on multiple other objects (typically valueHolders) |
|
41 |
and recompute a value whenever one of them changes. |
|
42 |
If the new value is different, it triggers itself a change to its dependents. |
|
43 |
||
44 |
Example use is to base an enableChannels value on multiple other boolean values. |
|
45 |
(See example for how this is done) |
|
223 | 46 |
|
231 | 47 |
Notice: |
48 |
this class was implemented using protocol information |
|
49 |
from alpha testers - it may not be complete or compatible to |
|
50 |
the corresponding ST-80 class. |
|
51 |
If you encounter any incompatibilities, please forward a note |
|
52 |
describing the incompatibility verbal (i.e. no code) to the ST/X team. |
|
53 |
||
223 | 54 |
[author:] |
55 |
Claus Gittinger |
|
145 | 56 |
" |
57 |
||
58 |
! |
|
59 |
||
60 |
examples |
|
61 |
" |
|
155 | 62 |
checkToggle 3 shows the value of toggle1 AND toggle2 |
231 | 63 |
[exBegin] |
64 |
|val1 val2 both box| |
|
155 | 65 |
|
231 | 66 |
val1 := false asValue. |
67 |
val2 := false asValue. |
|
68 |
both := BlockValue |
|
69 |
with:[:v1 :v2 | |
|
268 | 70 |
Transcript showCR:'evaluating ...'. |
231 | 71 |
v1 value and:[v2 value] |
72 |
] |
|
73 |
arguments:(Array with:val1 with:val2). |
|
155 | 74 |
|
231 | 75 |
box := Dialog new. |
76 |
box addCheckBox:'one' on:val1. |
|
77 |
box addCheckBox:'two' on:val2. |
|
78 |
box addHorizontalLine. |
|
79 |
box addCheckBox:'both' on:both. |
|
911 | 80 |
box addOkButton. |
231 | 81 |
box open |
82 |
[exEnd] |
|
155 | 83 |
|
84 |
the same, using a convenient instance creation message: |
|
231 | 85 |
[exBegin] |
86 |
|val1 val2 both box| |
|
155 | 87 |
|
231 | 88 |
val1 := false asValue. |
89 |
val2 := false asValue. |
|
90 |
both := BlockValue forLogical:val1 and:val2. |
|
145 | 91 |
|
231 | 92 |
box := Dialog new. |
93 |
box addCheckBox:'one' on:val1. |
|
94 |
box addCheckBox:'two' on:val2. |
|
95 |
box addHorizontalLine. |
|
911 | 96 |
(box addCheckBox:'both' on:both) disable. |
97 |
box addOkButton. |
|
231 | 98 |
box open |
99 |
[exEnd] |
|
155 | 100 |
|
101 |
logical or: |
|
231 | 102 |
[exBegin] |
103 |
|val1 val2 both box| |
|
155 | 104 |
|
231 | 105 |
val1 := false asValue. |
106 |
val2 := false asValue. |
|
107 |
both := BlockValue forLogical:val1 or:val2. |
|
145 | 108 |
|
231 | 109 |
box := Dialog new. |
110 |
box addCheckBox:'one' on:val1. |
|
111 |
box addCheckBox:'two' on:val2. |
|
112 |
box addHorizontalLine. |
|
113 |
box addCheckBox:'both' on:both. |
|
911 | 114 |
box addOkButton. |
231 | 115 |
box open |
116 |
[exEnd] |
|
155 | 117 |
|
118 |
example use: enabling an element depending on two others: |
|
231 | 119 |
[exBegin] |
120 |
|val1 val2 enabler val3 box| |
|
155 | 121 |
|
231 | 122 |
val1 := false asValue. |
123 |
val2 := false asValue. |
|
124 |
val3 := false asValue. |
|
125 |
enabler := BlockValue forLogical:val1 and:val2. |
|
155 | 126 |
|
231 | 127 |
box := Dialog new. |
128 |
box addCheckBox:'one' on:val1. |
|
129 |
box addCheckBox:'two' on:val2. |
|
130 |
box addHorizontalLine. |
|
911 | 131 |
(box addCheckBox:'three (both of the above)' on:val3) enableChannel:enabler. |
132 |
box addOkButton. |
|
231 | 133 |
box open |
134 |
[exEnd] |
|
382 | 135 |
|
136 |
||
137 |
like above, using a logical-or block: |
|
138 |
[exBegin] |
|
139 |
|val1 val2 enabler val3 box| |
|
140 |
||
141 |
val1 := false asValue. |
|
142 |
val2 := false asValue. |
|
143 |
val3 := false asValue. |
|
144 |
enabler := BlockValue forLogical:val1 or:val2. |
|
145 |
||
146 |
box := Dialog new. |
|
147 |
box addCheckBox:'one' on:val1. |
|
148 |
box addCheckBox:'two' on:val2. |
|
149 |
box addHorizontalLine. |
|
911 | 150 |
(box addCheckBox:'three (any of the above)' on:val3) enableChannel:enabler. |
151 |
box addOkButton. |
|
382 | 152 |
box open |
153 |
[exEnd] |
|
154 |
||
155 |
||
156 |
like above, using a bunch of toggles: |
|
157 |
[exBegin] |
|
158 |
|values anyValue box| |
|
159 |
||
160 |
values := (1 to:10) collect:[:i | false asValue]. |
|
161 |
anyValue := BlockValue forLogicalOrAll:values. |
|
162 |
anyValue onChangeSend:#value to:[Transcript showCR:'any is true']. |
|
163 |
||
164 |
box := Dialog new. |
|
165 |
values keysAndValuesDo:[:index :aValueHolder | |
|
166 |
box addCheckBox:index printString on:aValueHolder. |
|
167 |
]. |
|
168 |
box addHorizontalLine. |
|
911 | 169 |
(box addOkButton) enableChannel:anyValue. |
382 | 170 |
box open |
171 |
[exEnd] |
|
145 | 172 |
" |
173 |
! ! |
|
174 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
175 |
!BlockValue class methodsFor:'initialization'! |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
176 |
|
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
177 |
initialize |
158 | 178 |
NeverComputed isNil ifTrue:[ |
179 |
NeverComputed := Object new. |
|
180 |
] |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
181 |
! ! |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
182 |
|
145 | 183 |
!BlockValue class methodsFor:'instance creation'! |
184 |
||
979
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
185 |
block:aBlock arguments:aCollectionOfArguments |
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
186 |
"return a new BlockValue computing aBlock. |
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
187 |
Same as #with:arguments: for ST80 compatibility" |
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
188 |
|
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
189 |
^ self with:aBlock arguments:aCollectionOfArguments |
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
190 |
|
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
191 |
"Created: / 20.6.1998 / 14:00:16 / cg" |
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
192 |
! |
cd28a5d673c9
added #block:arguments: for ST80 compatibility.
Claus Gittinger <cg@exept.de>
parents:
911
diff
changeset
|
193 |
|
155 | 194 |
forLogical:arg1 and:arg2 |
1326 | 195 |
"return a new BlockValue computing the logical AND of its args |
196 |
(which are usually other valueHolders)" |
|
155 | 197 |
|
198 |
^ (super new) |
|
1326 | 199 |
setBlock:[:a :b | a value and:[b value]] |
200 |
arguments:(Array with:arg1 with:arg2) |
|
155 | 201 |
|
1326 | 202 |
"Created: / 16.12.1995 / 19:20:14 / cg" |
203 |
"Modified: / 30.1.2000 / 23:24:50 / cg" |
|
155 | 204 |
! |
205 |
||
206 |
forLogical:arg1 or:arg2 |
|
1326 | 207 |
"return a new BlockValue computing the logical OR of its args |
208 |
(which are usually other valueHolders)" |
|
155 | 209 |
|
210 |
^ (super new) |
|
1326 | 211 |
setBlock:[:a :b | a value or:[b value]] |
212 |
arguments:(Array with:arg1 with:arg2) |
|
155 | 213 |
|
1326 | 214 |
"Created: / 16.12.1995 / 19:20:14 / cg" |
215 |
"Modified: / 30.1.2000 / 23:24:46 / cg" |
|
155 | 216 |
! |
217 |
||
382 | 218 |
forLogicalAndAll:argArray |
219 |
"return a new BlockValue computing the logical OR of all elements |
|
1326 | 220 |
in the passed argArray (which are usually other valueHolders)" |
382 | 221 |
|
222 |
^ (super new) |
|
223 |
setBlock:[:a | (argArray findFirst:[:a | a value not]) == 0] |
|
911 | 224 |
argumentArray:argArray asArray |
382 | 225 |
|
911 | 226 |
"Created: / 22.1.1997 / 19:12:44 / cg" |
227 |
"Modified: / 28.4.1998 / 20:16:38 / ca" |
|
1326 | 228 |
"Modified: / 30.1.2000 / 23:24:40 / cg" |
382 | 229 |
! |
230 |
||
1045 | 231 |
forLogicalNot:arg |
1326 | 232 |
"return a new BlockValue computing the logical NOT of its arg |
233 |
(which is usually another valueHolder)" |
|
1045 | 234 |
|
235 |
^ (super new) |
|
236 |
setBlock:[:a | a value not] |
|
237 |
arguments:(Array with:arg) |
|
238 |
||
239 |
"Created: / 17.8.1998 / 10:06:41 / cg" |
|
1326 | 240 |
"Modified: / 30.1.2000 / 23:24:18 / cg" |
1045 | 241 |
! |
242 |
||
382 | 243 |
forLogicalOrAll:argArray |
244 |
"return a new BlockValue computing the logical OR of all elements |
|
1326 | 245 |
in the passed argArray (which are usually other valueHolders)" |
382 | 246 |
|
247 |
^ (super new) |
|
606 | 248 |
setBlock:[:arg | (arg findFirst:[:el | el value]) ~~ 0] |
911 | 249 |
argumentArray:argArray asArray |
382 | 250 |
|
911 | 251 |
"Created: / 22.1.1997 / 19:13:01 / cg" |
252 |
"Modified: / 28.4.1998 / 20:20:09 / ca" |
|
1326 | 253 |
"Modified: / 30.1.2000 / 23:24:34 / cg" |
382 | 254 |
! |
255 |
||
145 | 256 |
with:aBlock |
257 |
"return a new BlockValue computing aBlock" |
|
258 |
||
259 |
^ (super new) setBlock:aBlock |
|
260 |
||
261 |
"Created: 16.12.1995 / 19:16:33 / cg" |
|
262 |
! |
|
263 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
264 |
with:aBlock argument:anArgument |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
265 |
"return a new BlockValue computing aBlock" |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
266 |
|
1352 | 267 |
^ (super new) |
268 |
setBlock:aBlock |
|
269 |
arguments:(Array with:anArgument) |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
270 |
|
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
271 |
"Created: 16.12.1995 / 19:20:14 / cg" |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
272 |
! |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
273 |
|
1255 | 274 |
with:aBlock argument:anArgument1 argument:anArgument2 |
275 |
"return a new BlockValue computing aBlock" |
|
276 |
||
1352 | 277 |
^ (super new) |
278 |
setBlock:aBlock |
|
279 |
arguments:(Array with:anArgument1 with:anArgument2) |
|
1255 | 280 |
|
281 |
"Created: 16.12.1995 / 19:20:14 / cg" |
|
282 |
! |
|
283 |
||
1352 | 284 |
with:aBlock argument:anArgument1 argument:anArgument2 argument:anArgument3 |
285 |
"return a new BlockValue computing aBlock" |
|
286 |
||
287 |
^ (super new) |
|
288 |
setBlock:aBlock |
|
289 |
arguments:(Array with:anArgument1 with:anArgument2 with:anArgument3) |
|
290 |
||
291 |
! |
|
292 |
||
145 | 293 |
with:aBlock arguments:aCollectionOfArguments |
294 |
"return a new BlockValue computing aBlock" |
|
295 |
||
1352 | 296 |
^ (super new) |
297 |
setBlock:aBlock |
|
298 |
arguments:aCollectionOfArguments |
|
145 | 299 |
|
300 |
"Created: 16.12.1995 / 19:20:14 / cg" |
|
301 |
! ! |
|
302 |
||
303 |
!BlockValue methodsFor:'accessing'! |
|
304 |
||
491 | 305 |
computeValue |
306 |
"evaluate the receivers action block" |
|
307 |
||
308 |
|sz| |
|
309 |
||
310 |
arguments isNil ifTrue:[ |
|
311 |
^ block value |
|
312 |
]. |
|
313 |
sz := arguments size. |
|
314 |
sz == 0 ifTrue:[ |
|
315 |
^ block value |
|
316 |
]. |
|
317 |
sz == 1 ifTrue:[ |
|
318 |
^ block value:(arguments at:1) value |
|
319 |
]. |
|
320 |
sz == 2 ifTrue:[ |
|
321 |
^ block value:(arguments at:1) value |
|
322 |
value:(arguments at:2) value |
|
323 |
]. |
|
324 |
sz == 3 ifTrue:[ |
|
325 |
^ block value:(arguments at:1) value |
|
326 |
value:(arguments at:2) value |
|
327 |
value:(arguments at:3) value |
|
328 |
]. |
|
329 |
^ block |
|
330 |
valueWithArguments:(arguments collect:[:arg | arg value]) asArray |
|
331 |
||
332 |
"Created: 16.12.1995 / 19:27:40 / cg" |
|
333 |
"Modified: 22.1.1997 / 19:05:57 / cg" |
|
334 |
! |
|
335 |
||
145 | 336 |
dependOn:someObject |
382 | 337 |
"arrange for the blockValue to be reevaluated, whenever someObject |
338 |
changes (i.e. sends a change notification)" |
|
339 |
||
145 | 340 |
arguments isNil ifTrue:[ |
382 | 341 |
arguments := Array with:someObject |
145 | 342 |
] ifFalse:[ |
382 | 343 |
arguments := arguments copyWith:someObject |
145 | 344 |
]. |
345 |
someObject addDependent:self |
|
346 |
||
382 | 347 |
"Modified: 22.1.1997 / 19:05:26 / cg" |
145 | 348 |
! |
349 |
||
350 |
evaluate |
|
382 | 351 |
"evaluate the receivers action block" |
352 |
||
145 | 353 |
arguments isNil ifTrue:[ |
382 | 354 |
^ block value |
145 | 355 |
]. |
356 |
^ block valueWithArguments:(arguments asArray) |
|
357 |
||
358 |
"Created: 16.12.1995 / 19:27:40 / cg" |
|
382 | 359 |
"Modified: 22.1.1997 / 19:05:57 / cg" |
145 | 360 |
! |
361 |
||
362 |
setBlock:aBlock |
|
382 | 363 |
"set the receivers action block" |
364 |
||
145 | 365 |
block := aBlock. |
366 |
arguments notNil ifTrue:[ |
|
382 | 367 |
self release |
145 | 368 |
]. |
369 |
arguments := nil. |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
370 |
cachedValue := NeverComputed. |
145 | 371 |
|
372 |
"Created: 16.12.1995 / 19:16:59 / cg" |
|
382 | 373 |
"Modified: 22.1.1997 / 19:05:54 / cg" |
374 |
! |
|
375 |
||
376 |
setBlock:aBlock argumentArray:anArgumentCollection |
|
377 |
"set the receivers action block, and define an arguments collection |
|
378 |
to be passed to it. |
|
379 |
A change in any element of the collection will force reevaluation of the |
|
380 |
action block (passing the collection as a single argument) |
|
381 |
- possibly generating another change from myself" |
|
382 |
||
383 |
block := aBlock. |
|
384 |
arguments notNil ifTrue:[ |
|
385 |
self release |
|
386 |
]. |
|
387 |
arguments := Array with:anArgumentCollection. |
|
388 |
anArgumentCollection do:[:arg | |
|
389 |
arg addDependent:self |
|
390 |
]. |
|
391 |
cachedValue := NeverComputed. |
|
392 |
||
393 |
"Modified: 22.1.1997 / 19:08:51 / cg" |
|
394 |
"Created: 22.1.1997 / 19:22:13 / cg" |
|
145 | 395 |
! |
396 |
||
397 |
setBlock:aBlock arguments:aCollectionOfArguments |
|
382 | 398 |
"set the receivers action block, and define arguments to be passed to it. |
399 |
A change in any of the arguments will force reevaluation of the action |
|
400 |
block - possibly generating another change from myself" |
|
401 |
||
145 | 402 |
block := aBlock. |
403 |
arguments notNil ifTrue:[ |
|
382 | 404 |
self release |
145 | 405 |
]. |
406 |
arguments := aCollectionOfArguments. |
|
407 |
arguments do:[:arg | |
|
382 | 408 |
arg addDependent:self |
145 | 409 |
]. |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
410 |
cachedValue := NeverComputed. |
145 | 411 |
|
412 |
"Created: 16.12.1995 / 19:21:41 / cg" |
|
382 | 413 |
"Modified: 22.1.1997 / 19:08:51 / cg" |
145 | 414 |
! |
415 |
||
155 | 416 |
setValue:newValue |
417 |
"physically set my value, without change notifications. |
|
418 |
This is a noop here, since my value is computed." |
|
419 |
||
420 |
^ self |
|
421 |
||
422 |
||
423 |
! |
|
424 |
||
145 | 425 |
value |
382 | 426 |
"retrieve my value - this does not always evaluate the action block, |
427 |
since the returned value is cached internally" |
|
428 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
429 |
cachedValue == NeverComputed ifTrue:[ |
491 | 430 |
cachedValue := self computeValue |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
431 |
]. |
145 | 432 |
^ cachedValue |
433 |
||
434 |
"Created: 16.12.1995 / 19:23:26 / cg" |
|
382 | 435 |
"Modified: 22.1.1997 / 19:06:59 / cg" |
145 | 436 |
! ! |
437 |
||
347 | 438 |
!BlockValue methodsFor:'change & update'! |
145 | 439 |
|
440 |
update:something with:aParameter from:someone |
|
382 | 441 |
"the one I depend on has changed - reevaluate may actionBlock, |
442 |
and possibly send a change notification to my dependents" |
|
443 |
||
145 | 444 |
|oldValue| |
445 |
||
446 |
oldValue := cachedValue. |
|
491 | 447 |
cachedValue := self computeValue. |
145 | 448 |
oldValue ~~ cachedValue ifTrue:[ |
382 | 449 |
self changed:#value |
145 | 450 |
]. |
451 |
||
452 |
"Created: 16.12.1995 / 19:22:54 / cg" |
|
382 | 453 |
"Modified: 22.1.1997 / 19:07:39 / cg" |
145 | 454 |
! ! |
455 |
||
456 |
!BlockValue methodsFor:'release'! |
|
457 |
||
458 |
release |
|
382 | 459 |
"release any dependencies upon the arguments" |
460 |
||
145 | 461 |
arguments notNil ifTrue:[ |
382 | 462 |
arguments do:[:arg | arg removeDependent:self]. |
145 | 463 |
]. |
464 |
||
382 | 465 |
"Modified: 22.1.1997 / 19:08:06 / cg" |
145 | 466 |
! ! |
467 |
||
468 |
!BlockValue class methodsFor:'documentation'! |
|
469 |
||
470 |
version |
|
1352 | 471 |
^ '$Header: /cvs/stx/stx/libview2/BlockValue.st,v 1.17 2000-02-17 15:00:56 cg Exp $' |
145 | 472 |
! ! |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
473 |
BlockValue initialize! |