author | Claus Gittinger <cg@exept.de> |
Tue, 10 Aug 1999 12:18:47 +0200 | |
changeset 1216 | 26fe79e724d0 |
parent 1045 | c501e3553048 |
child 1255 | 39081e9cddf1 |
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 |
195 |
"return a new BlockValue computing the logical AND of its args" |
|
196 |
||
197 |
^ (super new) |
|
158 | 198 |
setBlock:[:a :b | a value and:[b value]] |
199 |
arguments:(Array with:arg1 with:arg2) |
|
155 | 200 |
|
201 |
"Created: 16.12.1995 / 19:20:14 / cg" |
|
202 |
! |
|
203 |
||
204 |
forLogical:arg1 or:arg2 |
|
205 |
"return a new BlockValue computing the logical OR of its args" |
|
206 |
||
207 |
^ (super new) |
|
158 | 208 |
setBlock:[:a :b | a value or:[b value]] |
209 |
arguments:(Array with:arg1 with:arg2) |
|
155 | 210 |
|
211 |
"Created: 16.12.1995 / 19:20:14 / cg" |
|
212 |
! |
|
213 |
||
382 | 214 |
forLogicalAndAll:argArray |
215 |
"return a new BlockValue computing the logical OR of all elements |
|
216 |
in the passed argArray" |
|
217 |
||
218 |
^ (super new) |
|
219 |
setBlock:[:a | (argArray findFirst:[:a | a value not]) == 0] |
|
911 | 220 |
argumentArray:argArray asArray |
382 | 221 |
|
911 | 222 |
"Created: / 22.1.1997 / 19:12:44 / cg" |
223 |
"Modified: / 28.4.1998 / 20:16:38 / ca" |
|
382 | 224 |
! |
225 |
||
1045 | 226 |
forLogicalNot:arg |
227 |
"return a new BlockValue computing the logical NOT of its arg" |
|
228 |
||
229 |
^ (super new) |
|
230 |
setBlock:[:a | a value not] |
|
231 |
arguments:(Array with:arg) |
|
232 |
||
233 |
"Created: / 17.8.1998 / 10:06:41 / cg" |
|
234 |
! |
|
235 |
||
382 | 236 |
forLogicalOrAll:argArray |
237 |
"return a new BlockValue computing the logical OR of all elements |
|
238 |
in the passed argArray" |
|
239 |
||
240 |
^ (super new) |
|
606 | 241 |
setBlock:[:arg | (arg findFirst:[:el | el value]) ~~ 0] |
911 | 242 |
argumentArray:argArray asArray |
382 | 243 |
|
911 | 244 |
"Created: / 22.1.1997 / 19:13:01 / cg" |
245 |
"Modified: / 17.6.1997 / 18:05:12 / cg" |
|
246 |
"Modified: / 28.4.1998 / 20:20:09 / ca" |
|
382 | 247 |
! |
248 |
||
145 | 249 |
with:aBlock |
250 |
"return a new BlockValue computing aBlock" |
|
251 |
||
252 |
^ (super new) setBlock:aBlock |
|
253 |
||
254 |
"Created: 16.12.1995 / 19:16:33 / cg" |
|
255 |
! |
|
256 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
257 |
with:aBlock argument:anArgument |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
258 |
"return a new BlockValue computing aBlock" |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
259 |
|
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
260 |
^ (super new) setBlock:aBlock arguments:(Array with:anArgument) |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
261 |
|
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
262 |
"Created: 16.12.1995 / 19:20:14 / cg" |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
263 |
! |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
264 |
|
145 | 265 |
with:aBlock arguments:aCollectionOfArguments |
266 |
"return a new BlockValue computing aBlock" |
|
267 |
||
268 |
^ (super new) setBlock:aBlock arguments:aCollectionOfArguments |
|
269 |
||
270 |
"Created: 16.12.1995 / 19:20:14 / cg" |
|
271 |
! ! |
|
272 |
||
273 |
!BlockValue methodsFor:'accessing'! |
|
274 |
||
491 | 275 |
computeValue |
276 |
"evaluate the receivers action block" |
|
277 |
||
278 |
|sz| |
|
279 |
||
280 |
arguments isNil ifTrue:[ |
|
281 |
^ block value |
|
282 |
]. |
|
283 |
sz := arguments size. |
|
284 |
sz == 0 ifTrue:[ |
|
285 |
^ block value |
|
286 |
]. |
|
287 |
sz == 1 ifTrue:[ |
|
288 |
^ block value:(arguments at:1) value |
|
289 |
]. |
|
290 |
sz == 2 ifTrue:[ |
|
291 |
^ block value:(arguments at:1) value |
|
292 |
value:(arguments at:2) value |
|
293 |
]. |
|
294 |
sz == 3 ifTrue:[ |
|
295 |
^ block value:(arguments at:1) value |
|
296 |
value:(arguments at:2) value |
|
297 |
value:(arguments at:3) value |
|
298 |
]. |
|
299 |
^ block |
|
300 |
valueWithArguments:(arguments collect:[:arg | arg value]) asArray |
|
301 |
||
302 |
"Created: 16.12.1995 / 19:27:40 / cg" |
|
303 |
"Modified: 22.1.1997 / 19:05:57 / cg" |
|
304 |
! |
|
305 |
||
145 | 306 |
dependOn:someObject |
382 | 307 |
"arrange for the blockValue to be reevaluated, whenever someObject |
308 |
changes (i.e. sends a change notification)" |
|
309 |
||
145 | 310 |
arguments isNil ifTrue:[ |
382 | 311 |
arguments := Array with:someObject |
145 | 312 |
] ifFalse:[ |
382 | 313 |
arguments := arguments copyWith:someObject |
145 | 314 |
]. |
315 |
someObject addDependent:self |
|
316 |
||
382 | 317 |
"Modified: 22.1.1997 / 19:05:26 / cg" |
145 | 318 |
! |
319 |
||
320 |
evaluate |
|
382 | 321 |
"evaluate the receivers action block" |
322 |
||
145 | 323 |
arguments isNil ifTrue:[ |
382 | 324 |
^ block value |
145 | 325 |
]. |
326 |
^ block valueWithArguments:(arguments asArray) |
|
327 |
||
328 |
"Created: 16.12.1995 / 19:27:40 / cg" |
|
382 | 329 |
"Modified: 22.1.1997 / 19:05:57 / cg" |
145 | 330 |
! |
331 |
||
332 |
setBlock:aBlock |
|
382 | 333 |
"set the receivers action block" |
334 |
||
145 | 335 |
block := aBlock. |
336 |
arguments notNil ifTrue:[ |
|
382 | 337 |
self release |
145 | 338 |
]. |
339 |
arguments := nil. |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
340 |
cachedValue := NeverComputed. |
145 | 341 |
|
342 |
"Created: 16.12.1995 / 19:16:59 / cg" |
|
382 | 343 |
"Modified: 22.1.1997 / 19:05:54 / cg" |
344 |
! |
|
345 |
||
346 |
setBlock:aBlock argumentArray:anArgumentCollection |
|
347 |
"set the receivers action block, and define an arguments collection |
|
348 |
to be passed to it. |
|
349 |
A change in any element of the collection will force reevaluation of the |
|
350 |
action block (passing the collection as a single argument) |
|
351 |
- possibly generating another change from myself" |
|
352 |
||
353 |
block := aBlock. |
|
354 |
arguments notNil ifTrue:[ |
|
355 |
self release |
|
356 |
]. |
|
357 |
arguments := Array with:anArgumentCollection. |
|
358 |
anArgumentCollection do:[:arg | |
|
359 |
arg addDependent:self |
|
360 |
]. |
|
361 |
cachedValue := NeverComputed. |
|
362 |
||
363 |
"Modified: 22.1.1997 / 19:08:51 / cg" |
|
364 |
"Created: 22.1.1997 / 19:22:13 / cg" |
|
145 | 365 |
! |
366 |
||
367 |
setBlock:aBlock arguments:aCollectionOfArguments |
|
382 | 368 |
"set the receivers action block, and define arguments to be passed to it. |
369 |
A change in any of the arguments will force reevaluation of the action |
|
370 |
block - possibly generating another change from myself" |
|
371 |
||
145 | 372 |
block := aBlock. |
373 |
arguments notNil ifTrue:[ |
|
382 | 374 |
self release |
145 | 375 |
]. |
376 |
arguments := aCollectionOfArguments. |
|
377 |
arguments do:[:arg | |
|
382 | 378 |
arg addDependent:self |
145 | 379 |
]. |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
380 |
cachedValue := NeverComputed. |
145 | 381 |
|
382 |
"Created: 16.12.1995 / 19:21:41 / cg" |
|
382 | 383 |
"Modified: 22.1.1997 / 19:08:51 / cg" |
145 | 384 |
! |
385 |
||
155 | 386 |
setValue:newValue |
387 |
"physically set my value, without change notifications. |
|
388 |
This is a noop here, since my value is computed." |
|
389 |
||
390 |
^ self |
|
391 |
||
392 |
||
393 |
! |
|
394 |
||
145 | 395 |
value |
382 | 396 |
"retrieve my value - this does not always evaluate the action block, |
397 |
since the returned value is cached internally" |
|
398 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
399 |
cachedValue == NeverComputed ifTrue:[ |
491 | 400 |
cachedValue := self computeValue |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
401 |
]. |
145 | 402 |
^ cachedValue |
403 |
||
404 |
"Created: 16.12.1995 / 19:23:26 / cg" |
|
382 | 405 |
"Modified: 22.1.1997 / 19:06:59 / cg" |
145 | 406 |
! ! |
407 |
||
347 | 408 |
!BlockValue methodsFor:'change & update'! |
145 | 409 |
|
410 |
update:something with:aParameter from:someone |
|
382 | 411 |
"the one I depend on has changed - reevaluate may actionBlock, |
412 |
and possibly send a change notification to my dependents" |
|
413 |
||
145 | 414 |
|oldValue| |
415 |
||
416 |
oldValue := cachedValue. |
|
491 | 417 |
cachedValue := self computeValue. |
145 | 418 |
oldValue ~~ cachedValue ifTrue:[ |
382 | 419 |
self changed:#value |
145 | 420 |
]. |
421 |
||
422 |
"Created: 16.12.1995 / 19:22:54 / cg" |
|
382 | 423 |
"Modified: 22.1.1997 / 19:07:39 / cg" |
145 | 424 |
! ! |
425 |
||
426 |
!BlockValue methodsFor:'release'! |
|
427 |
||
428 |
release |
|
382 | 429 |
"release any dependencies upon the arguments" |
430 |
||
145 | 431 |
arguments notNil ifTrue:[ |
382 | 432 |
arguments do:[:arg | arg removeDependent:self]. |
145 | 433 |
]. |
434 |
||
382 | 435 |
"Modified: 22.1.1997 / 19:08:06 / cg" |
145 | 436 |
! ! |
437 |
||
438 |
!BlockValue class methodsFor:'documentation'! |
|
439 |
||
440 |
version |
|
1045 | 441 |
^ '$Header: /cvs/stx/stx/libview2/BlockValue.st,v 1.14 1998-08-17 08:06:57 cg Exp $' |
145 | 442 |
! ! |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
443 |
BlockValue initialize! |