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