author | Claus Gittinger <cg@exept.de> |
Sat, 01 Mar 1997 23:15:10 +0100 | |
changeset 471 | 350583677b5b |
parent 382 | d64197ef1c9b |
child 491 | c5174fe6f09c |
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 |
||
249 |
dependOn:someObject |
|
382 | 250 |
"arrange for the blockValue to be reevaluated, whenever someObject |
251 |
changes (i.e. sends a change notification)" |
|
252 |
||
145 | 253 |
arguments isNil ifTrue:[ |
382 | 254 |
arguments := Array with:someObject |
145 | 255 |
] ifFalse:[ |
382 | 256 |
arguments := arguments copyWith:someObject |
145 | 257 |
]. |
258 |
someObject addDependent:self |
|
259 |
||
382 | 260 |
"Modified: 22.1.1997 / 19:05:26 / cg" |
145 | 261 |
! |
262 |
||
263 |
evaluate |
|
382 | 264 |
"evaluate the receivers action block" |
265 |
||
145 | 266 |
arguments isNil ifTrue:[ |
382 | 267 |
^ block value |
145 | 268 |
]. |
269 |
^ block valueWithArguments:(arguments asArray) |
|
270 |
||
271 |
"Created: 16.12.1995 / 19:27:40 / cg" |
|
382 | 272 |
"Modified: 22.1.1997 / 19:05:57 / cg" |
145 | 273 |
! |
274 |
||
275 |
setBlock:aBlock |
|
382 | 276 |
"set the receivers action block" |
277 |
||
145 | 278 |
block := aBlock. |
279 |
arguments notNil ifTrue:[ |
|
382 | 280 |
self release |
145 | 281 |
]. |
282 |
arguments := nil. |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
283 |
cachedValue := NeverComputed. |
145 | 284 |
|
285 |
"Created: 16.12.1995 / 19:16:59 / cg" |
|
382 | 286 |
"Modified: 22.1.1997 / 19:05:54 / cg" |
287 |
! |
|
288 |
||
289 |
setBlock:aBlock argumentArray:anArgumentCollection |
|
290 |
"set the receivers action block, and define an arguments collection |
|
291 |
to be passed to it. |
|
292 |
A change in any element of the collection will force reevaluation of the |
|
293 |
action block (passing the collection as a single argument) |
|
294 |
- possibly generating another change from myself" |
|
295 |
||
296 |
block := aBlock. |
|
297 |
arguments notNil ifTrue:[ |
|
298 |
self release |
|
299 |
]. |
|
300 |
arguments := Array with:anArgumentCollection. |
|
301 |
anArgumentCollection do:[:arg | |
|
302 |
arg addDependent:self |
|
303 |
]. |
|
304 |
cachedValue := NeverComputed. |
|
305 |
||
306 |
"Modified: 22.1.1997 / 19:08:51 / cg" |
|
307 |
"Created: 22.1.1997 / 19:22:13 / cg" |
|
145 | 308 |
! |
309 |
||
310 |
setBlock:aBlock arguments:aCollectionOfArguments |
|
382 | 311 |
"set the receivers action block, and define arguments to be passed to it. |
312 |
A change in any of the arguments will force reevaluation of the action |
|
313 |
block - possibly generating another change from myself" |
|
314 |
||
145 | 315 |
block := aBlock. |
316 |
arguments notNil ifTrue:[ |
|
382 | 317 |
self release |
145 | 318 |
]. |
319 |
arguments := aCollectionOfArguments. |
|
320 |
arguments do:[:arg | |
|
382 | 321 |
arg addDependent:self |
145 | 322 |
]. |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
323 |
cachedValue := NeverComputed. |
145 | 324 |
|
325 |
"Created: 16.12.1995 / 19:21:41 / cg" |
|
382 | 326 |
"Modified: 22.1.1997 / 19:08:51 / cg" |
145 | 327 |
! |
328 |
||
155 | 329 |
setValue:newValue |
330 |
"physically set my value, without change notifications. |
|
331 |
This is a noop here, since my value is computed." |
|
332 |
||
333 |
^ self |
|
334 |
||
335 |
||
336 |
! |
|
337 |
||
145 | 338 |
value |
382 | 339 |
"retrieve my value - this does not always evaluate the action block, |
340 |
since the returned value is cached internally" |
|
341 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
342 |
cachedValue == NeverComputed ifTrue:[ |
382 | 343 |
cachedValue := self evaluate |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
344 |
]. |
145 | 345 |
^ cachedValue |
346 |
||
347 |
"Created: 16.12.1995 / 19:23:26 / cg" |
|
382 | 348 |
"Modified: 22.1.1997 / 19:06:59 / cg" |
145 | 349 |
! ! |
350 |
||
347 | 351 |
!BlockValue methodsFor:'change & update'! |
145 | 352 |
|
353 |
update:something with:aParameter from:someone |
|
382 | 354 |
"the one I depend on has changed - reevaluate may actionBlock, |
355 |
and possibly send a change notification to my dependents" |
|
356 |
||
145 | 357 |
|oldValue| |
358 |
||
359 |
oldValue := cachedValue. |
|
360 |
cachedValue := self evaluate. |
|
361 |
oldValue ~~ cachedValue ifTrue:[ |
|
382 | 362 |
self changed:#value |
145 | 363 |
]. |
364 |
||
365 |
"Created: 16.12.1995 / 19:22:54 / cg" |
|
382 | 366 |
"Modified: 22.1.1997 / 19:07:39 / cg" |
145 | 367 |
! ! |
368 |
||
369 |
!BlockValue methodsFor:'release'! |
|
370 |
||
371 |
release |
|
382 | 372 |
"release any dependencies upon the arguments" |
373 |
||
145 | 374 |
arguments notNil ifTrue:[ |
382 | 375 |
arguments do:[:arg | arg removeDependent:self]. |
145 | 376 |
]. |
377 |
||
382 | 378 |
"Modified: 22.1.1997 / 19:08:06 / cg" |
145 | 379 |
! ! |
380 |
||
381 |
!BlockValue class methodsFor:'documentation'! |
|
382 |
||
383 |
version |
|
382 | 384 |
^ '$Header: /cvs/stx/stx/libview2/BlockValue.st,v 1.9 1997-01-23 03:26:07 cg Exp $' |
145 | 385 |
! ! |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
386 |
BlockValue initialize! |