author | Stefan Vogel <sv@exept.de> |
Tue, 31 Dec 1996 15:01:08 +0100 | |
changeset 352 | ba7970fbcde2 |
parent 268 | 1998023f12dc |
child 443 | 5c57a8861677 |
permissions | -rw-r--r-- |
75 | 1 |
" |
2 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
3 |
All Rights Reserved |
|
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 |
ValueModel subclass:#PluggableAdaptor |
|
223 | 14 |
instanceVariableNames:'model getBlock putBlock updateBlock' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Interface-Support-Models' |
|
75 | 18 |
! |
19 |
||
20 |
!PluggableAdaptor class methodsFor:'documentation'! |
|
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
25 |
All Rights Reserved |
|
26 |
||
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
36 |
documentation |
|
37 |
" |
|
38 |
Allows forwarding of value/value:/change messages via blocks. |
|
39 |
Kind of what ST/X always did .... |
|
40 |
||
41 |
PluggableAdaptor is the most general of the adaptor models - |
|
42 |
any other adapter can be simulated. However, they incur certain |
|
82 | 43 |
overhead and complexity. |
44 |
Therefore, in many situations, an AspectAdaptor is the better choice. |
|
223 | 45 |
|
231 | 46 |
Notice: |
47 |
this class was implemented using protocol information |
|
48 |
from alpha testers - it may not be complete or compatible to |
|
49 |
the corresponding ST-80 class. |
|
50 |
If you encounter any incompatibilities, please forward a note |
|
51 |
describing the incompatibility verbal (i.e. no code) to the ST/X team. |
|
52 |
||
223 | 53 |
[author:] |
54 |
Claus Gittinger |
|
76 | 55 |
" |
56 |
! |
|
75 | 57 |
|
76 | 58 |
examples |
59 |
" |
|
60 |
an adaptor for the variable x: |
|
231 | 61 |
[exBegin] |
62 |
|m x t| |
|
75 | 63 |
|
231 | 64 |
m := (PluggableAdaptor new) |
65 |
getBlock:[:m | x] |
|
268 | 66 |
putBlock:[:m :newValue | x := newValue. Transcript showCR:x] |
67 |
updateBlock:[:m :aspect :param | Transcript showCR:'changed']. |
|
231 | 68 |
t := Toggle new. |
69 |
t model:m. |
|
70 |
t label:'toggle me'. |
|
71 |
t open. |
|
72 |
[exEnd] |
|
75 | 73 |
|
74 |
||
76 | 75 |
an adaptor to send #destroy: |
231 | 76 |
[exBegin] |
77 |
|m t| |
|
75 | 78 |
|
231 | 79 |
m := (PluggableAdaptor new) |
80 |
getBlock:[:m | false] |
|
81 |
putBlock:[:m :newValue | t destroy] |
|
82 |
updateBlock:[:m :aspect :param | ]. |
|
83 |
t := Button new. |
|
84 |
t model:m. |
|
85 |
t label:'close me'. |
|
86 |
t open. |
|
87 |
[exEnd] |
|
75 | 88 |
|
89 |
||
90 |
as above, more convenient setup: |
|
231 | 91 |
[exBegin] |
92 |
|m t| |
|
75 | 93 |
|
231 | 94 |
t := Button new. |
75 | 95 |
|
231 | 96 |
m := (PluggableAdaptor on:t) performAction:#destroy. |
97 |
t model:m. |
|
98 |
t label:'close me'. |
|
99 |
t open. |
|
100 |
[exEnd] |
|
75 | 101 |
|
102 |
extract values from a complex model: |
|
231 | 103 |
[exBegin] |
104 |
|model dialog name pId| |
|
75 | 105 |
|
231 | 106 |
model := Plug new. |
107 |
model respondTo:#name with:[name]. |
|
108 |
model respondTo:#name: with:[:newValue | name := newValue]. |
|
109 |
model respondTo:#passportId with:[pId]. |
|
110 |
model respondTo:#passportId: with:[:newValue | pId := newValue]. |
|
111 |
name := 'John Smith'. |
|
112 |
pId := 56785432. |
|
75 | 113 |
|
231 | 114 |
dialog := Dialog new. |
115 |
||
116 |
dialog addInputFieldOn:((PluggableAdaptor on:model) |
|
117 |
getBlock:[:m | m name] |
|
118 |
putBlock:[:m :v | m name:v] |
|
119 |
updateBlock:[:m :a :p | false]). |
|
75 | 120 |
|
231 | 121 |
dialog addVerticalSpace. |
75 | 122 |
|
231 | 123 |
dialog addInputFieldOn:((PluggableAdaptor on:model) |
124 |
getBlock:[:m | m passportId printString] |
|
125 |
putBlock:[:m :v | m passportId:v asNumber] |
|
126 |
updateBlock:[:m :a :p | false]). |
|
127 |
dialog addAbortButton; addOkButton. |
|
128 |
dialog width:200; sizeFixed:true. |
|
129 |
dialog open. |
|
75 | 130 |
|
231 | 131 |
dialog accept value ifTrue:[ |
268 | 132 |
Transcript showCR:'accepted.'. |
231 | 133 |
]. |
268 | 134 |
Transcript showCR:' Name: ' , model name. |
135 |
Transcript showCR:' ID : ' , model passportId printString. |
|
231 | 136 |
[exEnd] |
137 |
||
75 | 138 |
|
139 |
||
82 | 140 |
extract values from a point into different labels: |
231 | 141 |
[exBegin] |
142 |
|p t l| |
|
82 | 143 |
|
231 | 144 |
p := 10 @ 20. |
82 | 145 |
|
231 | 146 |
t := HorizontalPanelView new. |
147 |
t extent:200@50. |
|
148 |
t horizontalLayout:#fitSpace. |
|
82 | 149 |
|
231 | 150 |
l := Label in:t. |
151 |
l model:((PluggableAdaptor on:p) getSelector:#x putSelector:#x:); |
|
152 |
labelMessage:#value; aspect:#value; |
|
153 |
level:-1; |
|
154 |
sizeFixed:true. |
|
155 |
l := Label in:t. |
|
156 |
l model:((PluggableAdaptor on:p) getSelector:#y putSelector:#y:); |
|
157 |
labelMessage:#value; aspect:#value; |
|
158 |
level:-1; |
|
159 |
sizeFixed:true. |
|
160 |
t open. |
|
82 | 161 |
|
231 | 162 |
(Delay forSeconds:5) wait. |
163 |
p x:100. |
|
164 |
p changed. |
|
165 |
[exEnd] |
|
82 | 166 |
|
167 |
extract values from an array into different labels: |
|
231 | 168 |
[exBegin] |
169 |
|a t l| |
|
75 | 170 |
|
231 | 171 |
a := #('one' 'two' 'three'). |
75 | 172 |
|
231 | 173 |
t := HorizontalPanelView new. |
174 |
t extent:200@50. |
|
175 |
t horizontalLayout:#fitSpace. |
|
75 | 176 |
|
231 | 177 |
l := Label in:t. |
178 |
l model:((PluggableAdaptor on:a) collectionIndex:1); |
|
179 |
labelMessage:#value; aspect:#value; |
|
180 |
level:-1; |
|
181 |
sizeFixed:true. |
|
182 |
l := Label in:t. |
|
183 |
l model:((PluggableAdaptor on:a) collectionIndex:2); |
|
184 |
labelMessage:#value; aspect:#value; |
|
185 |
level:-1; |
|
186 |
sizeFixed:true. |
|
187 |
l := Label in:t. |
|
188 |
l model:((PluggableAdaptor on:a) collectionIndex:3); |
|
189 |
labelMessage:#value; aspect:#value; |
|
190 |
level:-1; |
|
191 |
sizeFixed:true. |
|
192 |
t open. |
|
75 | 193 |
|
231 | 194 |
(Delay forSeconds:5) wait. |
195 |
a at:1 put:'1'. |
|
196 |
a changed. |
|
197 |
[exEnd] |
|
75 | 198 |
" |
199 |
! ! |
|
200 |
||
201 |
!PluggableAdaptor class methodsFor:'instance creation'! |
|
202 |
||
203 |
on:anObject |
|
204 |
^ self new model:anObject |
|
205 |
! ! |
|
206 |
||
125 | 207 |
!PluggableAdaptor methodsFor:'accessing'! |
75 | 208 |
|
125 | 209 |
setValue:newValue |
210 |
putBlock notNil ifTrue:[ |
|
211 |
^ putBlock value:model value:newValue |
|
75 | 212 |
]. |
125 | 213 |
model value:newValue "/ stupid default |
75 | 214 |
! |
215 |
||
125 | 216 |
value |
217 |
getBlock notNil ifTrue:[ |
|
218 |
^ getBlock value:model |
|
219 |
]. |
|
220 |
^ model value "/ stupid default |
|
221 |
! ! |
|
75 | 222 |
|
125 | 223 |
!PluggableAdaptor methodsFor:'changes'! |
75 | 224 |
|
125 | 225 |
update:something with:aParameter from:changedObject |
226 |
changedObject == model ifTrue:[ |
|
227 |
updateBlock notNil ifTrue:[ |
|
228 |
(updateBlock value:model value:something value:aParameter) |
|
229 |
ifTrue:[ |
|
230 |
"/ self notifyChange:#value |
|
231 |
self changed:#value |
|
232 |
] |
|
233 |
] |
|
234 |
]. |
|
235 |
! ! |
|
75 | 236 |
|
125 | 237 |
!PluggableAdaptor methodsFor:'initialize-release'! |
75 | 238 |
|
239 |
collectionIndex:idx |
|
240 |
"configure the adaptor to translate #value/#value:-messages into |
|
241 |
indexed accesses via #at:/#at:put:, using the supplied index" |
|
242 |
||
243 |
getBlock := [:model | model at:idx]. |
|
244 |
putBlock := [:model :newValue | model at:idx put:newValue]. |
|
245 |
updateBlock := [:model :aspect :parameter | |
|
82 | 246 |
aspect isNil or:[aspect == #value] |
75 | 247 |
] |
248 |
! |
|
249 |
||
250 |
getBlock:block1 putBlock:block2 updateBlock:block3 |
|
251 |
"configure the adaptor to translate #value/#value:-messages into |
|
252 |
evaluation of the corresponding block1/block2. |
|
253 |
The getBlock argument block1 is called with one argument, the model, and is |
|
254 |
supposed to extract & return a value from that model. |
|
255 |
The putBlock argument, block2 is called with 2 arguments, the model |
|
256 |
and the new value, and is supposed to store the new value. |
|
257 |
The updateBlock argument, block3 is called with 3 arguments, the model, |
|
258 |
the aspect as changed in the model and the change parameter. It |
|
259 |
is called when the adaptor receives an update message from the model, |
|
260 |
and should return true if a new value should be fetched from the model." |
|
261 |
||
262 |
getBlock := block1. |
|
263 |
putBlock := block2. |
|
264 |
updateBlock := block3. |
|
125 | 265 |
! |
75 | 266 |
|
125 | 267 |
getSelector:getSelector putSelector:putSelector |
268 |
"configure the adaptor to translate #value-messages into a send of |
|
269 |
getSelector and #value:-messages into sends of putSelector." |
|
75 | 270 |
|
125 | 271 |
getBlock := [:model | model perform:getSelector]. |
272 |
putBlock := [:model :newValue | model perform:putSelector with:newValue]. |
|
273 |
updateBlock := [:model :aspect :parameter | |
|
274 |
aspect isNil |
|
275 |
or:[aspect == getSelector |
|
276 |
or:[aspect == #value]] |
|
277 |
] |
|
278 |
! |
|
279 |
||
280 |
model:anObject |
|
281 |
model notNil ifTrue:[ |
|
282 |
model removeDependent:self |
|
75 | 283 |
]. |
125 | 284 |
model := anObject. |
285 |
model addDependent:self |
|
75 | 286 |
! |
287 |
||
125 | 288 |
performAction:aSelector |
289 |
"configure the adaptor to send an aSelector-message to the model |
|
290 |
whenever a new value is stored via #value:" |
|
291 |
||
292 |
getBlock := [:model | nil]. |
|
293 |
putBlock := [:model :newValue | model perform:aSelector]. |
|
294 |
updateBlock := [:model :aspect :parameter | false] |
|
295 |
! |
|
296 |
||
297 |
selectValue:something |
|
298 |
"configure the adaptor to behave like a boolean value, returning |
|
299 |
true whenever the models value equals something" |
|
300 |
||
301 |
getBlock := [:model | model value = something]. |
|
352
ba7970fbcde2
Fix #selectValue: (Used by T-gen).
Stefan Vogel <sv@exept.de>
parents:
268
diff
changeset
|
302 |
putBlock := [:model :newValue | newValue ifTrue:[model value:something]]. |
ba7970fbcde2
Fix #selectValue: (Used by T-gen).
Stefan Vogel <sv@exept.de>
parents:
268
diff
changeset
|
303 |
updateBlock := [:model :aspect :parameter | |
ba7970fbcde2
Fix #selectValue: (Used by T-gen).
Stefan Vogel <sv@exept.de>
parents:
268
diff
changeset
|
304 |
aspect isNil or:[aspect == #value] |
ba7970fbcde2
Fix #selectValue: (Used by T-gen).
Stefan Vogel <sv@exept.de>
parents:
268
diff
changeset
|
305 |
] |
ba7970fbcde2
Fix #selectValue: (Used by T-gen).
Stefan Vogel <sv@exept.de>
parents:
268
diff
changeset
|
306 |
|
ba7970fbcde2
Fix #selectValue: (Used by T-gen).
Stefan Vogel <sv@exept.de>
parents:
268
diff
changeset
|
307 |
"Modified: 31.12.1996 / 13:58:09 / stefan" |
125 | 308 |
! |
309 |
||
310 |
subjectChannel:aValueHolder |
|
311 |
self model:aValueHolder |
|
75 | 312 |
! ! |
313 |
||
129 | 314 |
!PluggableAdaptor class methodsFor:'documentation'! |
315 |
||
316 |
version |
|
352
ba7970fbcde2
Fix #selectValue: (Used by T-gen).
Stefan Vogel <sv@exept.de>
parents:
268
diff
changeset
|
317 |
^ '$Header: /cvs/stx/stx/libview2/Attic/PlugAdptr.st,v 1.13 1996-12-31 14:01:08 stefan Exp $' |
129 | 318 |
! ! |