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