author | Claus Gittinger <cg@exept.de> |
Sat, 11 Nov 1995 17:05:49 +0100 | |
changeset 114 | e577a2f332d0 |
parent 96 | 948318b2fbd4 |
child 125 | fa5b5e4336bf |
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 |
|
79 | 14 |
instanceVariableNames:'model getBlock putBlock updateBlock' |
75 | 15 |
classVariableNames:'' |
16 |
poolDictionaries:'' |
|
17 |
category:'Interface-Support-Models' |
|
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 |
version |
|
114
e577a2f332d0
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
37 |
^ '$Header: /cvs/stx/stx/libview2/PluggableAdaptor.st,v 1.7 1995-11-11 16:04:52 cg Exp $' |
75 | 38 |
! |
39 |
||
40 |
documentation |
|
41 |
" |
|
42 |
Allows forwarding of value/value:/change messages via blocks. |
|
43 |
Kind of what ST/X always did .... |
|
44 |
||
45 |
PluggableAdaptor is the most general of the adaptor models - |
|
46 |
any other adapter can be simulated. However, they incur certain |
|
82 | 47 |
overhead and complexity. |
48 |
Therefore, in many situations, an AspectAdaptor is the better choice. |
|
76 | 49 |
" |
50 |
! |
|
75 | 51 |
|
76 | 52 |
examples |
53 |
" |
|
54 |
an adaptor for the variable x: |
|
75 | 55 |
|
56 |
|m x t| |
|
57 |
||
58 |
m := (PluggableAdaptor new) |
|
59 |
getBlock:[:m | x] |
|
60 |
putBlock:[:m :newValue | x := newValue. Transcript showCr:x] |
|
61 |
updateBlock:[:m :aspect :param | Transcript showCr:'changed']. |
|
62 |
t := Toggle new. |
|
63 |
t model:m. |
|
64 |
t label:'toggle me'. |
|
65 |
t open. |
|
66 |
||
67 |
||
68 |
||
76 | 69 |
an adaptor to send #destroy: |
75 | 70 |
|
71 |
|m t| |
|
72 |
||
73 |
m := (PluggableAdaptor new) |
|
74 |
getBlock:[:m | false] |
|
75 |
putBlock:[:m :newValue | t destroy] |
|
76 |
updateBlock:[:m :aspect :param | ]. |
|
77 |
t := Button new. |
|
78 |
t model:m. |
|
79 |
t label:'close me'. |
|
80 |
t open. |
|
81 |
||
82 |
||
83 |
||
84 |
as above, more convenient setup: |
|
85 |
||
86 |
|m t| |
|
87 |
||
88 |
t := Button new. |
|
89 |
||
90 |
m := (PluggableAdaptor on:t) performAction:#destroy. |
|
91 |
t model:m. |
|
92 |
t label:'close me'. |
|
93 |
t open. |
|
94 |
||
95 |
||
96 |
extract values from a complex model: |
|
97 |
||
98 |
|model dialog name pId| |
|
99 |
||
100 |
model := Plug new. |
|
101 |
model respondTo:#name with:[name]. |
|
102 |
model respondTo:#name: with:[:newValue | name := newValue]. |
|
103 |
model respondTo:#passportId with:[pId]. |
|
104 |
model respondTo:#passportId: with:[:newValue | pId := newValue]. |
|
105 |
name := 'John Smith'. |
|
106 |
pId := 56785432. |
|
107 |
||
108 |
dialog := Dialog new. |
|
109 |
||
110 |
dialog addInputFieldOn:((PluggableAdaptor on:model) |
|
111 |
getBlock:[:m | m name] |
|
112 |
putBlock:[:m :v | m name:v] |
|
113 |
updateBlock:[:m :a :p | false]). |
|
114 |
||
115 |
dialog addVerticalSpace. |
|
116 |
||
117 |
dialog addInputFieldOn:((PluggableAdaptor on:model) |
|
118 |
getBlock:[:m | m passportId printString] |
|
119 |
putBlock:[:m :v | m passportId:v asNumber] |
|
120 |
updateBlock:[:m :a :p | false]). |
|
121 |
dialog addAbortButton; addOkButton. |
|
122 |
dialog width:200; sizeFixed:true. |
|
123 |
dialog open. |
|
124 |
||
125 |
dialog accept value ifTrue:[ |
|
126 |
Transcript showCr:'accepted.'. |
|
127 |
]. |
|
128 |
Transcript showCr:' Name: ' , model name. |
|
129 |
Transcript showCr:' ID : ' , model passportId printString. |
|
130 |
||
131 |
||
132 |
||
82 | 133 |
extract values from a point into different labels: |
134 |
||
135 |
|p t l| |
|
136 |
||
137 |
p := 10 @ 20. |
|
138 |
||
139 |
t := HorizontalPanelView new. |
|
140 |
t extent:200@50. |
|
141 |
t horizontalLayout:#fitSpace. |
|
142 |
||
143 |
l := Label in:t. |
|
144 |
l model:((PluggableAdaptor on:p) getSelector:#x putSelector:#x:); |
|
145 |
labelMessage:#value; aspect:#value; |
|
146 |
level:-1; |
|
147 |
sizeFixed:true. |
|
148 |
l := Label in:t. |
|
149 |
l model:((PluggableAdaptor on:p) getSelector:#y putSelector:#y:); |
|
150 |
labelMessage:#value; aspect:#value; |
|
151 |
level:-1; |
|
152 |
sizeFixed:true. |
|
153 |
t open. |
|
154 |
||
155 |
(Delay forSeconds:5) wait. |
|
156 |
p x:100. |
|
157 |
p changed. |
|
158 |
||
159 |
||
160 |
extract values from an array into different labels: |
|
75 | 161 |
|
162 |
|a t l| |
|
163 |
||
164 |
a := #('one' 'two' 'three'). |
|
165 |
||
166 |
t := HorizontalPanelView new. |
|
167 |
t extent:200@50. |
|
168 |
t horizontalLayout:#fitSpace. |
|
169 |
||
170 |
l := Label in:t. |
|
171 |
l model:((PluggableAdaptor on:a) collectionIndex:1); |
|
172 |
labelMessage:#value; aspect:#value; |
|
82 | 173 |
level:-1; |
174 |
sizeFixed:true. |
|
75 | 175 |
l := Label in:t. |
176 |
l model:((PluggableAdaptor on:a) collectionIndex:2); |
|
177 |
labelMessage:#value; aspect:#value; |
|
82 | 178 |
level:-1; |
179 |
sizeFixed:true. |
|
75 | 180 |
l := Label in:t. |
181 |
l model:((PluggableAdaptor on:a) collectionIndex:3); |
|
182 |
labelMessage:#value; aspect:#value; |
|
82 | 183 |
level:-1; |
184 |
sizeFixed:true. |
|
75 | 185 |
t open. |
186 |
||
187 |
(Delay forSeconds:5) wait. |
|
188 |
a at:1 put:'1'. |
|
189 |
a changed. |
|
190 |
" |
|
191 |
! ! |
|
192 |
||
193 |
!PluggableAdaptor class methodsFor:'instance creation'! |
|
194 |
||
195 |
on:anObject |
|
196 |
^ self new model:anObject |
|
197 |
! ! |
|
198 |
||
199 |
!PluggableAdaptor methodsFor:'initialize-release'! |
|
200 |
||
201 |
model:anObject |
|
79 | 202 |
model notNil ifTrue:[ |
203 |
model removeDependent:self |
|
75 | 204 |
]. |
79 | 205 |
model := anObject. |
206 |
model addDependent:self |
|
75 | 207 |
! |
208 |
||
209 |
subjectChannel:aValueHolder |
|
210 |
self model:aValueHolder |
|
211 |
! |
|
212 |
||
213 |
performAction:aSelector |
|
214 |
"configure the adaptor to send an aSelector-message to the model |
|
215 |
whenever a new value is stored via #value:" |
|
216 |
||
217 |
getBlock := [:model | nil]. |
|
218 |
putBlock := [:model :newValue | model perform:aSelector]. |
|
219 |
updateBlock := [:model :aspect :parameter | false] |
|
220 |
! |
|
221 |
||
222 |
selectValue:something |
|
223 |
"configure the adaptor to behave like a boolean value, returning |
|
224 |
true whenever the models value equals something" |
|
225 |
||
226 |
getBlock := [:model | model value = something]. |
|
227 |
putBlock := [:model :newValue | ]. |
|
228 |
updateBlock := [:model :aspect :parameter | false] |
|
229 |
! |
|
230 |
||
231 |
getSelector:getSelector putSelector:putSelector |
|
232 |
"configure the adaptor to translate #value-messages into a send of |
|
233 |
getSelector and #value:-messages into sends of putSelector." |
|
234 |
||
235 |
getBlock := [:model | model perform:getSelector]. |
|
236 |
putBlock := [:model :newValue | model perform:putSelector with:newValue]. |
|
237 |
updateBlock := [:model :aspect :parameter | |
|
82 | 238 |
aspect isNil |
239 |
or:[aspect == getSelector |
|
240 |
or:[aspect == #value]] |
|
75 | 241 |
] |
242 |
! |
|
243 |
||
244 |
collectionIndex:idx |
|
245 |
"configure the adaptor to translate #value/#value:-messages into |
|
246 |
indexed accesses via #at:/#at:put:, using the supplied index" |
|
247 |
||
248 |
getBlock := [:model | model at:idx]. |
|
249 |
putBlock := [:model :newValue | model at:idx put:newValue]. |
|
250 |
updateBlock := [:model :aspect :parameter | |
|
82 | 251 |
aspect isNil or:[aspect == #value] |
75 | 252 |
] |
253 |
! |
|
254 |
||
255 |
getBlock:block1 putBlock:block2 updateBlock:block3 |
|
256 |
"configure the adaptor to translate #value/#value:-messages into |
|
257 |
evaluation of the corresponding block1/block2. |
|
258 |
The getBlock argument block1 is called with one argument, the model, and is |
|
259 |
supposed to extract & return a value from that model. |
|
260 |
The putBlock argument, block2 is called with 2 arguments, the model |
|
261 |
and the new value, and is supposed to store the new value. |
|
262 |
The updateBlock argument, block3 is called with 3 arguments, the model, |
|
263 |
the aspect as changed in the model and the change parameter. It |
|
264 |
is called when the adaptor receives an update message from the model, |
|
265 |
and should return true if a new value should be fetched from the model." |
|
266 |
||
267 |
getBlock := block1. |
|
268 |
putBlock := block2. |
|
269 |
updateBlock := block3. |
|
270 |
! ! |
|
271 |
||
272 |
!PluggableAdaptor methodsFor:'accessing'! |
|
273 |
||
274 |
value |
|
275 |
getBlock notNil ifTrue:[ |
|
79 | 276 |
^ getBlock value:model |
75 | 277 |
]. |
79 | 278 |
^ model value "/ stupid default |
75 | 279 |
! |
280 |
||
281 |
setValue:newValue |
|
282 |
putBlock notNil ifTrue:[ |
|
79 | 283 |
^ putBlock value:model value:newValue |
75 | 284 |
]. |
79 | 285 |
model value:newValue "/ stupid default |
75 | 286 |
! ! |
287 |
||
288 |
!PluggableAdaptor methodsFor:'changes'! |
|
289 |
||
290 |
update:something with:aParameter from:changedObject |
|
79 | 291 |
changedObject == model ifTrue:[ |
75 | 292 |
updateBlock notNil ifTrue:[ |
79 | 293 |
(updateBlock value:model value:something value:aParameter) |
75 | 294 |
ifTrue:[ |
86 | 295 |
"/ self notifyChange:#value |
296 |
self changed:#value |
|
75 | 297 |
] |
298 |
] |
|
299 |
]. |
|
300 |
! ! |