author | Claus Gittinger <cg@exept.de> |
Fri, 04 Apr 1997 15:02:02 +0200 | |
changeset 509 | df395b9c70c8 |
parent 491 | c5174fe6f09c |
child 606 | 8a8b36a9e67d |
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. |
|
80 |
box open |
|
81 |
[exEnd] |
|
155 | 82 |
|
83 |
the same, using a convenient instance creation message: |
|
231 | 84 |
[exBegin] |
85 |
|val1 val2 both box| |
|
155 | 86 |
|
231 | 87 |
val1 := false asValue. |
88 |
val2 := false asValue. |
|
89 |
both := BlockValue forLogical:val1 and:val2. |
|
145 | 90 |
|
231 | 91 |
box := Dialog new. |
92 |
box addCheckBox:'one' on:val1. |
|
93 |
box addCheckBox:'two' on:val2. |
|
94 |
box addHorizontalLine. |
|
95 |
box addCheckBox:'both' on:both. |
|
96 |
box open |
|
97 |
[exEnd] |
|
155 | 98 |
|
99 |
logical or: |
|
231 | 100 |
[exBegin] |
101 |
|val1 val2 both box| |
|
155 | 102 |
|
231 | 103 |
val1 := false asValue. |
104 |
val2 := false asValue. |
|
105 |
both := BlockValue forLogical:val1 or:val2. |
|
145 | 106 |
|
231 | 107 |
box := Dialog new. |
108 |
box addCheckBox:'one' on:val1. |
|
109 |
box addCheckBox:'two' on:val2. |
|
110 |
box addHorizontalLine. |
|
111 |
box addCheckBox:'both' on:both. |
|
112 |
box open |
|
113 |
[exEnd] |
|
155 | 114 |
|
115 |
example use: enabling an element depending on two others: |
|
231 | 116 |
[exBegin] |
117 |
|val1 val2 enabler val3 box| |
|
155 | 118 |
|
231 | 119 |
val1 := false asValue. |
120 |
val2 := false asValue. |
|
121 |
val3 := false asValue. |
|
122 |
enabler := BlockValue forLogical:val1 and:val2. |
|
155 | 123 |
|
231 | 124 |
box := Dialog new. |
125 |
box addCheckBox:'one' on:val1. |
|
126 |
box addCheckBox:'two' on:val2. |
|
127 |
box addHorizontalLine. |
|
128 |
(box addCheckBox:'three' on:val3) enableChannel:enabler. |
|
129 |
box open |
|
130 |
[exEnd] |
|
382 | 131 |
|
132 |
||
133 |
like above, using a logical-or block: |
|
134 |
[exBegin] |
|
135 |
|val1 val2 enabler val3 box| |
|
136 |
||
137 |
val1 := false asValue. |
|
138 |
val2 := false asValue. |
|
139 |
val3 := false asValue. |
|
140 |
enabler := BlockValue forLogical:val1 or:val2. |
|
141 |
||
142 |
box := Dialog new. |
|
143 |
box addCheckBox:'one' on:val1. |
|
144 |
box addCheckBox:'two' on:val2. |
|
145 |
box addHorizontalLine. |
|
146 |
(box addCheckBox:'three' on:val3) enableChannel:enabler. |
|
147 |
box open |
|
148 |
[exEnd] |
|
149 |
||
150 |
||
151 |
like above, using a bunch of toggles: |
|
152 |
[exBegin] |
|
153 |
|values anyValue box| |
|
154 |
||
155 |
values := (1 to:10) collect:[:i | false asValue]. |
|
156 |
anyValue := BlockValue forLogicalOrAll:values. |
|
157 |
anyValue onChangeSend:#value to:[Transcript showCR:'any is true']. |
|
158 |
||
159 |
box := Dialog new. |
|
160 |
values keysAndValuesDo:[:index :aValueHolder | |
|
161 |
box addCheckBox:index printString on:aValueHolder. |
|
162 |
]. |
|
163 |
box addHorizontalLine. |
|
164 |
(box addCheckBox:'any' on:nil) enableChannel:anyValue. |
|
165 |
box open |
|
166 |
[exEnd] |
|
145 | 167 |
" |
168 |
! ! |
|
169 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
170 |
!BlockValue class methodsFor:'initialization'! |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
171 |
|
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
172 |
initialize |
158 | 173 |
NeverComputed isNil ifTrue:[ |
174 |
NeverComputed := Object new. |
|
175 |
] |
|
146
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 |
|
145 | 178 |
!BlockValue class methodsFor:'instance creation'! |
179 |
||
155 | 180 |
forLogical:arg1 and:arg2 |
181 |
"return a new BlockValue computing the logical AND of its args" |
|
182 |
||
183 |
^ (super new) |
|
158 | 184 |
setBlock:[:a :b | a value and:[b value]] |
185 |
arguments:(Array with:arg1 with:arg2) |
|
155 | 186 |
|
187 |
"Created: 16.12.1995 / 19:20:14 / cg" |
|
188 |
! |
|
189 |
||
190 |
forLogical:arg1 or:arg2 |
|
191 |
"return a new BlockValue computing the logical OR of its args" |
|
192 |
||
193 |
^ (super new) |
|
158 | 194 |
setBlock:[:a :b | a value or:[b value]] |
195 |
arguments:(Array with:arg1 with:arg2) |
|
155 | 196 |
|
197 |
"Created: 16.12.1995 / 19:20:14 / cg" |
|
198 |
! |
|
199 |
||
382 | 200 |
forLogicalAndAll:argArray |
201 |
"return a new BlockValue computing the logical OR of all elements |
|
202 |
in the passed argArray" |
|
203 |
||
204 |
^ (super new) |
|
205 |
setBlock:[:a | (argArray findFirst:[:a | a value not]) == 0] |
|
206 |
arguments:(Array with:argArray) |
|
207 |
||
208 |
"Created: 22.1.1997 / 19:12:44 / cg" |
|
209 |
! |
|
210 |
||
211 |
forLogicalOrAll:argArray |
|
212 |
"return a new BlockValue computing the logical OR of all elements |
|
213 |
in the passed argArray" |
|
214 |
||
215 |
^ (super new) |
|
216 |
setBlock:[:arg | self halt. (arg findFirst:[:el | el value]) ~~ 0] |
|
217 |
arguments:(Array with:argArray) |
|
218 |
||
219 |
"Created: 22.1.1997 / 19:13:01 / cg" |
|
220 |
"Modified: 22.1.1997 / 19:19:42 / cg" |
|
221 |
! |
|
222 |
||
145 | 223 |
with:aBlock |
224 |
"return a new BlockValue computing aBlock" |
|
225 |
||
226 |
^ (super new) setBlock:aBlock |
|
227 |
||
228 |
"Created: 16.12.1995 / 19:16:33 / cg" |
|
229 |
! |
|
230 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
231 |
with:aBlock argument:anArgument |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
232 |
"return a new BlockValue computing aBlock" |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
233 |
|
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
234 |
^ (super new) setBlock:aBlock arguments:(Array with:anArgument) |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
235 |
|
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
236 |
"Created: 16.12.1995 / 19:20:14 / cg" |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
237 |
! |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
238 |
|
145 | 239 |
with:aBlock arguments:aCollectionOfArguments |
240 |
"return a new BlockValue computing aBlock" |
|
241 |
||
242 |
^ (super new) setBlock:aBlock arguments:aCollectionOfArguments |
|
243 |
||
244 |
"Created: 16.12.1995 / 19:20:14 / cg" |
|
245 |
! ! |
|
246 |
||
247 |
!BlockValue methodsFor:'accessing'! |
|
248 |
||
491 | 249 |
computeValue |
250 |
"evaluate the receivers action block" |
|
251 |
||
252 |
|sz| |
|
253 |
||
254 |
arguments isNil ifTrue:[ |
|
255 |
^ block value |
|
256 |
]. |
|
257 |
sz := arguments size. |
|
258 |
sz == 0 ifTrue:[ |
|
259 |
^ block value |
|
260 |
]. |
|
261 |
sz == 1 ifTrue:[ |
|
262 |
^ block value:(arguments at:1) value |
|
263 |
]. |
|
264 |
sz == 2 ifTrue:[ |
|
265 |
^ block value:(arguments at:1) value |
|
266 |
value:(arguments at:2) value |
|
267 |
]. |
|
268 |
sz == 3 ifTrue:[ |
|
269 |
^ block value:(arguments at:1) value |
|
270 |
value:(arguments at:2) value |
|
271 |
value:(arguments at:3) value |
|
272 |
]. |
|
273 |
^ block |
|
274 |
valueWithArguments:(arguments collect:[:arg | arg value]) asArray |
|
275 |
||
276 |
"Created: 16.12.1995 / 19:27:40 / cg" |
|
277 |
"Modified: 22.1.1997 / 19:05:57 / cg" |
|
278 |
! |
|
279 |
||
145 | 280 |
dependOn:someObject |
382 | 281 |
"arrange for the blockValue to be reevaluated, whenever someObject |
282 |
changes (i.e. sends a change notification)" |
|
283 |
||
145 | 284 |
arguments isNil ifTrue:[ |
382 | 285 |
arguments := Array with:someObject |
145 | 286 |
] ifFalse:[ |
382 | 287 |
arguments := arguments copyWith:someObject |
145 | 288 |
]. |
289 |
someObject addDependent:self |
|
290 |
||
382 | 291 |
"Modified: 22.1.1997 / 19:05:26 / cg" |
145 | 292 |
! |
293 |
||
294 |
evaluate |
|
382 | 295 |
"evaluate the receivers action block" |
296 |
||
145 | 297 |
arguments isNil ifTrue:[ |
382 | 298 |
^ block value |
145 | 299 |
]. |
300 |
^ block valueWithArguments:(arguments asArray) |
|
301 |
||
302 |
"Created: 16.12.1995 / 19:27:40 / cg" |
|
382 | 303 |
"Modified: 22.1.1997 / 19:05:57 / cg" |
145 | 304 |
! |
305 |
||
306 |
setBlock:aBlock |
|
382 | 307 |
"set the receivers action block" |
308 |
||
145 | 309 |
block := aBlock. |
310 |
arguments notNil ifTrue:[ |
|
382 | 311 |
self release |
145 | 312 |
]. |
313 |
arguments := nil. |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
314 |
cachedValue := NeverComputed. |
145 | 315 |
|
316 |
"Created: 16.12.1995 / 19:16:59 / cg" |
|
382 | 317 |
"Modified: 22.1.1997 / 19:05:54 / cg" |
318 |
! |
|
319 |
||
320 |
setBlock:aBlock argumentArray:anArgumentCollection |
|
321 |
"set the receivers action block, and define an arguments collection |
|
322 |
to be passed to it. |
|
323 |
A change in any element of the collection will force reevaluation of the |
|
324 |
action block (passing the collection as a single argument) |
|
325 |
- possibly generating another change from myself" |
|
326 |
||
327 |
block := aBlock. |
|
328 |
arguments notNil ifTrue:[ |
|
329 |
self release |
|
330 |
]. |
|
331 |
arguments := Array with:anArgumentCollection. |
|
332 |
anArgumentCollection do:[:arg | |
|
333 |
arg addDependent:self |
|
334 |
]. |
|
335 |
cachedValue := NeverComputed. |
|
336 |
||
337 |
"Modified: 22.1.1997 / 19:08:51 / cg" |
|
338 |
"Created: 22.1.1997 / 19:22:13 / cg" |
|
145 | 339 |
! |
340 |
||
341 |
setBlock:aBlock arguments:aCollectionOfArguments |
|
382 | 342 |
"set the receivers action block, and define arguments to be passed to it. |
343 |
A change in any of the arguments will force reevaluation of the action |
|
344 |
block - possibly generating another change from myself" |
|
345 |
||
145 | 346 |
block := aBlock. |
347 |
arguments notNil ifTrue:[ |
|
382 | 348 |
self release |
145 | 349 |
]. |
350 |
arguments := aCollectionOfArguments. |
|
351 |
arguments do:[:arg | |
|
382 | 352 |
arg addDependent:self |
145 | 353 |
]. |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
354 |
cachedValue := NeverComputed. |
145 | 355 |
|
356 |
"Created: 16.12.1995 / 19:21:41 / cg" |
|
382 | 357 |
"Modified: 22.1.1997 / 19:08:51 / cg" |
145 | 358 |
! |
359 |
||
155 | 360 |
setValue:newValue |
361 |
"physically set my value, without change notifications. |
|
362 |
This is a noop here, since my value is computed." |
|
363 |
||
364 |
^ self |
|
365 |
||
366 |
||
367 |
! |
|
368 |
||
145 | 369 |
value |
382 | 370 |
"retrieve my value - this does not always evaluate the action block, |
371 |
since the returned value is cached internally" |
|
372 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
373 |
cachedValue == NeverComputed ifTrue:[ |
491 | 374 |
cachedValue := self computeValue |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
375 |
]. |
145 | 376 |
^ cachedValue |
377 |
||
378 |
"Created: 16.12.1995 / 19:23:26 / cg" |
|
382 | 379 |
"Modified: 22.1.1997 / 19:06:59 / cg" |
145 | 380 |
! ! |
381 |
||
347 | 382 |
!BlockValue methodsFor:'change & update'! |
145 | 383 |
|
384 |
update:something with:aParameter from:someone |
|
382 | 385 |
"the one I depend on has changed - reevaluate may actionBlock, |
386 |
and possibly send a change notification to my dependents" |
|
387 |
||
145 | 388 |
|oldValue| |
389 |
||
390 |
oldValue := cachedValue. |
|
491 | 391 |
cachedValue := self computeValue. |
145 | 392 |
oldValue ~~ cachedValue ifTrue:[ |
382 | 393 |
self changed:#value |
145 | 394 |
]. |
395 |
||
396 |
"Created: 16.12.1995 / 19:22:54 / cg" |
|
382 | 397 |
"Modified: 22.1.1997 / 19:07:39 / cg" |
145 | 398 |
! ! |
399 |
||
400 |
!BlockValue methodsFor:'release'! |
|
401 |
||
402 |
release |
|
382 | 403 |
"release any dependencies upon the arguments" |
404 |
||
145 | 405 |
arguments notNil ifTrue:[ |
382 | 406 |
arguments do:[:arg | arg removeDependent:self]. |
145 | 407 |
]. |
408 |
||
382 | 409 |
"Modified: 22.1.1997 / 19:08:06 / cg" |
145 | 410 |
! ! |
411 |
||
412 |
!BlockValue class methodsFor:'documentation'! |
|
413 |
||
414 |
version |
|
491 | 415 |
^ '$Header: /cvs/stx/stx/libview2/BlockValue.st,v 1.10 1997-03-05 16:09:43 ca Exp $' |
145 | 416 |
! ! |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
417 |
BlockValue initialize! |