author | Claus Gittinger <cg@exept.de> |
Sat, 27 Apr 1996 19:59:26 +0200 | |
changeset 231 | 2fec6188bd28 |
parent 223 | b65dc250db8d |
child 268 | 1998023f12dc |
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 | |
|
70 |
Transcript showCr:'evaluating ...'. |
|
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] |
|
145 | 131 |
" |
132 |
! ! |
|
133 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
134 |
!BlockValue class methodsFor:'initialization'! |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
135 |
|
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
136 |
initialize |
158 | 137 |
NeverComputed isNil ifTrue:[ |
138 |
NeverComputed := Object new. |
|
139 |
] |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
140 |
! ! |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
141 |
|
145 | 142 |
!BlockValue class methodsFor:'instance creation'! |
143 |
||
155 | 144 |
forLogical:arg1 and:arg2 |
145 |
"return a new BlockValue computing the logical AND of its args" |
|
146 |
||
147 |
^ (super new) |
|
158 | 148 |
setBlock:[:a :b | a value and:[b value]] |
149 |
arguments:(Array with:arg1 with:arg2) |
|
155 | 150 |
|
151 |
"Created: 16.12.1995 / 19:20:14 / cg" |
|
152 |
! |
|
153 |
||
154 |
forLogical:arg1 or:arg2 |
|
155 |
"return a new BlockValue computing the logical OR of its args" |
|
156 |
||
157 |
^ (super new) |
|
158 | 158 |
setBlock:[:a :b | a value or:[b value]] |
159 |
arguments:(Array with:arg1 with:arg2) |
|
155 | 160 |
|
161 |
"Created: 16.12.1995 / 19:20:14 / cg" |
|
162 |
! |
|
163 |
||
145 | 164 |
with:aBlock |
165 |
"return a new BlockValue computing aBlock" |
|
166 |
||
167 |
^ (super new) setBlock:aBlock |
|
168 |
||
169 |
"Created: 16.12.1995 / 19:16:33 / cg" |
|
170 |
! |
|
171 |
||
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
172 |
with:aBlock argument:anArgument |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
173 |
"return a new BlockValue computing aBlock" |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
174 |
|
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
175 |
^ (super new) setBlock:aBlock arguments:(Array with:anArgument) |
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 |
"Created: 16.12.1995 / 19:20:14 / cg" |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
178 |
! |
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
179 |
|
145 | 180 |
with:aBlock arguments:aCollectionOfArguments |
181 |
"return a new BlockValue computing aBlock" |
|
182 |
||
183 |
^ (super new) setBlock:aBlock arguments:aCollectionOfArguments |
|
184 |
||
185 |
"Created: 16.12.1995 / 19:20:14 / cg" |
|
186 |
! ! |
|
187 |
||
188 |
!BlockValue methodsFor:'accessing'! |
|
189 |
||
190 |
dependOn:someObject |
|
191 |
arguments isNil ifTrue:[ |
|
158 | 192 |
arguments := Array with:someObject |
145 | 193 |
] ifFalse:[ |
158 | 194 |
arguments := arguments copyWith:someObject |
145 | 195 |
]. |
196 |
someObject addDependent:self |
|
197 |
||
198 |
"Modified: 16.12.1995 / 19:18:31 / cg" |
|
199 |
! |
|
200 |
||
201 |
evaluate |
|
202 |
arguments isNil ifTrue:[ |
|
158 | 203 |
^ block value |
145 | 204 |
]. |
205 |
^ block valueWithArguments:(arguments asArray) |
|
206 |
||
207 |
"Created: 16.12.1995 / 19:27:40 / cg" |
|
208 |
! |
|
209 |
||
210 |
setBlock:aBlock |
|
211 |
block := aBlock. |
|
212 |
arguments notNil ifTrue:[ |
|
158 | 213 |
self release |
145 | 214 |
]. |
215 |
arguments := nil. |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
216 |
cachedValue := NeverComputed. |
145 | 217 |
|
218 |
"Created: 16.12.1995 / 19:16:59 / cg" |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
219 |
"Modified: 16.12.1995 / 19:51:23 / cg" |
145 | 220 |
! |
221 |
||
222 |
setBlock:aBlock arguments:aCollectionOfArguments |
|
223 |
block := aBlock. |
|
224 |
arguments notNil ifTrue:[ |
|
158 | 225 |
self release |
145 | 226 |
]. |
227 |
arguments := aCollectionOfArguments. |
|
228 |
arguments do:[:arg | |
|
158 | 229 |
arg addDependent:self |
145 | 230 |
]. |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
231 |
cachedValue := NeverComputed. |
145 | 232 |
|
233 |
"Created: 16.12.1995 / 19:21:41 / cg" |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
234 |
"Modified: 16.12.1995 / 19:51:28 / cg" |
145 | 235 |
! |
236 |
||
155 | 237 |
setValue:newValue |
238 |
"physically set my value, without change notifications. |
|
239 |
This is a noop here, since my value is computed." |
|
240 |
||
241 |
^ self |
|
242 |
||
243 |
||
244 |
! |
|
245 |
||
145 | 246 |
value |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
247 |
cachedValue == NeverComputed ifTrue:[ |
158 | 248 |
cachedValue := self evaluate |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
249 |
]. |
145 | 250 |
^ cachedValue |
251 |
||
252 |
"Created: 16.12.1995 / 19:23:26 / cg" |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
253 |
"Modified: 17.12.1995 / 15:59:16 / cg" |
145 | 254 |
! ! |
255 |
||
256 |
!BlockValue methodsFor:'change and update'! |
|
257 |
||
258 |
update:something with:aParameter from:someone |
|
259 |
|oldValue| |
|
260 |
||
261 |
oldValue := cachedValue. |
|
262 |
cachedValue := self evaluate. |
|
263 |
oldValue ~~ cachedValue ifTrue:[ |
|
158 | 264 |
self changed:#value |
145 | 265 |
]. |
266 |
||
267 |
"Created: 16.12.1995 / 19:22:54 / cg" |
|
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
268 |
"Modified: 17.12.1995 / 15:58:56 / cg" |
145 | 269 |
! ! |
270 |
||
271 |
!BlockValue methodsFor:'release'! |
|
272 |
||
273 |
release |
|
274 |
arguments notNil ifTrue:[ |
|
158 | 275 |
arguments do:[:arg | arg removeDependent:self]. |
145 | 276 |
]. |
277 |
||
278 |
"Modified: 16.12.1995 / 19:21:11 / cg" |
|
279 |
! ! |
|
280 |
||
281 |
!BlockValue class methodsFor:'documentation'! |
|
282 |
||
283 |
version |
|
231 | 284 |
^ '$Header: /cvs/stx/stx/libview2/BlockValue.st,v 1.6 1996-04-27 17:59:10 cg Exp $' |
145 | 285 |
! ! |
146
9603cec98ac2
oops change notification was wrong
Claus Gittinger <cg@exept.de>
parents:
145
diff
changeset
|
286 |
BlockValue initialize! |