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 |
"
|
2414
|
12 |
"{ Package: 'stx:libview2' }"
|
75
|
13 |
|
69
|
14 |
ValueModel subclass:#ProtocolAdaptor
|
186
|
15 |
instanceVariableNames:'accessPath subject subjectChannel subjectSendsUpdates'
|
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
category:'Interface-Support-Models'
|
69
|
19 |
!
|
|
20 |
|
|
21 |
!ProtocolAdaptor class methodsFor:'documentation'!
|
|
22 |
|
|
23 |
copyright
|
|
24 |
"
|
|
25 |
COPYRIGHT (c) 1995 by Claus Gittinger
|
71
|
26 |
All Rights Reserved
|
69
|
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 |
documentation
|
|
38 |
"
|
|
39 |
a ProtocolAdaptor allows access to embeded values in a
|
|
40 |
complex model and plays model towards the outside world.
|
|
41 |
|
|
42 |
Consider the case where editFields are required for the
|
|
43 |
elements (instance variables) of a more complex model.
|
|
44 |
Using ValueHolders, you had to copy the individual
|
|
45 |
values out-of and into multiple valueHolders.
|
|
46 |
A protocolAdaptor makes this easier, by playing model towards
|
|
47 |
the editField, returning a value from the complex model,
|
|
48 |
and forwards changes to the complex model.
|
|
49 |
|
231
|
50 |
Notice:
|
|
51 |
since you can specify the aspect- and changeSymbols in most ST/X
|
|
52 |
widgets, ProtocolAdapters are not always needed
|
|
53 |
(at least, if no accesspath is required).
|
|
54 |
However, if you want to apply widgets on objects which where not
|
|
55 |
originally designed as models (such as Arrays), ProtocolAdapters
|
|
56 |
are very handy.
|
69
|
57 |
|
231
|
58 |
Notice:
|
|
59 |
this class was implemented using protocol information
|
|
60 |
from alpha testers - it may not be complete or compatible to
|
|
61 |
the corresponding ST-80 class.
|
|
62 |
If you encounter any incompatibilities, please forward a note
|
|
63 |
describing the incompatibility verbal (i.e. no code) to the ST/X team.
|
223
|
64 |
|
|
65 |
[author:]
|
|
66 |
Claus Gittinger
|
231
|
67 |
|
|
68 |
[see also:]
|
|
69 |
Model AspectAdaptor ValueHolder
|
69
|
70 |
"
|
|
71 |
!
|
|
72 |
|
|
73 |
examples
|
|
74 |
"
|
231
|
75 |
[exBegin]
|
|
76 |
|a obj|
|
69
|
77 |
|
231
|
78 |
a := ProtocolAdaptor accessPath:#(1 2 3).
|
|
79 |
obj := Array with:#(11 (121 122 123) 13)
|
|
80 |
with:#(21 (221 222 223) 23)
|
|
81 |
with:#(33 (321 322 323) 33).
|
|
82 |
a valueUsingSubject:obj
|
|
83 |
[exEnd]
|
69
|
84 |
|
|
85 |
|
|
86 |
|
231
|
87 |
[exBegin]
|
|
88 |
|a obj|
|
69
|
89 |
|
231
|
90 |
a := ProtocolAdaptor accessPath:#(1 2 origin).
|
|
91 |
obj := Array with:(Array with:1@1 with:(1@2 corner:100@100))
|
|
92 |
with:(Array with:2@1 with:2@2)
|
|
93 |
with:(Array with:3@1 with:3@2).
|
|
94 |
a valueUsingSubject:obj
|
|
95 |
[exEnd]
|
69
|
96 |
|
|
97 |
|
|
98 |
|
231
|
99 |
[exBegin]
|
|
100 |
|a model|
|
69
|
101 |
|
231
|
102 |
a := ProtocolAdaptor accessPath:#(1 2 origin).
|
|
103 |
model := (Array with:(Array with:1@1 with:(1@2 corner:100@100))
|
|
104 |
with:(Array with:2@1 with:2@2)
|
|
105 |
with:(Array with:3@1 with:3@2)) asValue.
|
|
106 |
a subjectChannel:model.
|
|
107 |
a value
|
|
108 |
[exEnd]
|
69
|
109 |
"
|
|
110 |
! !
|
|
111 |
|
|
112 |
!ProtocolAdaptor class methodsFor:'instance creation'!
|
|
113 |
|
|
114 |
accessPath:aCollectionOfSelectors
|
|
115 |
^ (self new) accessPath:aCollectionOfSelectors
|
|
116 |
!
|
|
117 |
|
|
118 |
subject:anObject
|
|
119 |
^ (self new) subject:anObject
|
|
120 |
!
|
|
121 |
|
125
|
122 |
subject:anObject accessPath:aCollectionOfSelectors
|
|
123 |
^ (self new) subject:anObject; accessPath:aCollectionOfSelectors
|
|
124 |
!
|
|
125 |
|
69
|
126 |
subject:anObject sendsUpdates:aBoolean
|
186
|
127 |
^ (self new) subject:anObject; subjectSendsUpdates:aBoolean
|
|
128 |
|
|
129 |
"Modified: 18.4.1996 / 01:45:34 / cg"
|
69
|
130 |
!
|
|
131 |
|
|
132 |
subject:anObject sendsUpdates:aBoolean accessPath:aCollectionOfSelectors
|
186
|
133 |
^ (self new) subject:anObject; subjectSendsUpdates:aBoolean; accessPath:aCollectionOfSelectors
|
|
134 |
|
|
135 |
"Modified: 18.4.1996 / 01:45:38 / cg"
|
69
|
136 |
!
|
|
137 |
|
|
138 |
subjectChannel:aValueHolder
|
|
139 |
^ (self new) subjectChannel:aValueHolder
|
|
140 |
!
|
|
141 |
|
125
|
142 |
subjectChannel:aValueHolder accessPath:aCollectionOfSelectors
|
|
143 |
^ (self new) subjectChannel:aValueHolder; accessPath:aCollectionOfSelectors
|
|
144 |
!
|
|
145 |
|
69
|
146 |
subjectChannel:aValueHolder sendsUpdates:aBoolean
|
186
|
147 |
^ (self new) subjectChannel:aValueHolder; subjectSendsUpdates:aBoolean
|
|
148 |
|
|
149 |
"Modified: 18.4.1996 / 01:45:45 / cg"
|
69
|
150 |
!
|
|
151 |
|
|
152 |
subjectChannel:aValueHolder sendsUpdates:aBoolean accessPath:aCollectionOfSelectors
|
186
|
153 |
^ (self new) subjectChannel:aValueHolder; subjectSendsUpdates:aBoolean; accessPath:aCollectionOfSelectors
|
|
154 |
|
|
155 |
"Modified: 18.4.1996 / 01:45:50 / cg"
|
69
|
156 |
! !
|
|
157 |
|
|
158 |
!ProtocolAdaptor methodsFor:'accessing'!
|
|
159 |
|
125
|
160 |
setValue:newValue
|
|
161 |
"set the value in my subject or subjectChannel."
|
|
162 |
|
|
163 |
|obj|
|
|
164 |
|
|
165 |
subject notNil ifTrue:[
|
|
166 |
obj := subject.
|
|
167 |
] ifFalse:[
|
|
168 |
obj := subjectChannel value
|
|
169 |
].
|
|
170 |
^ self setValue:newValue usingSubject:obj
|
|
171 |
!
|
|
172 |
|
69
|
173 |
setValue:newValue usingSubject:anObject
|
|
174 |
"set a value in anObject, using the selectors in accessPath.
|
|
175 |
A helper for setValue:."
|
|
176 |
|
|
177 |
|obj lastIndex|
|
|
178 |
|
|
179 |
obj := anObject.
|
|
180 |
lastIndex := accessPath size.
|
|
181 |
accessPath keysAndValuesDo:[:idx :aSelectorOrIndex |
|
3301
|
182 |
aSelectorOrIndex isInteger ifTrue:[
|
|
183 |
idx == lastIndex ifTrue:[
|
|
184 |
obj at:aSelectorOrIndex put:newValue
|
|
185 |
] ifFalse:[
|
|
186 |
obj := obj at:aSelectorOrIndex
|
|
187 |
]
|
|
188 |
] ifFalse:[
|
|
189 |
idx == lastIndex ifTrue:[
|
|
190 |
obj perform:aSelectorOrIndex asMutator with:newValue
|
|
191 |
] ifFalse:[
|
|
192 |
obj := obj perform:aSelectorOrIndex
|
|
193 |
]
|
|
194 |
]
|
69
|
195 |
].
|
|
196 |
^ newValue
|
|
197 |
!
|
|
198 |
|
2414
|
199 |
subjectValue
|
|
200 |
"return the value from my subject or subjectChannel."
|
|
201 |
|
|
202 |
|obj|
|
|
203 |
|
|
204 |
subject notNil ifTrue:[
|
|
205 |
obj := subject.
|
|
206 |
] ifFalse:[
|
|
207 |
obj := subjectChannel value
|
|
208 |
].
|
|
209 |
^ self valueUsingSubject:obj
|
|
210 |
!
|
|
211 |
|
125
|
212 |
value
|
|
213 |
"return the value from my subject or subjectChannel."
|
|
214 |
|
|
215 |
|obj|
|
|
216 |
|
|
217 |
subject notNil ifTrue:[
|
|
218 |
obj := subject.
|
|
219 |
] ifFalse:[
|
|
220 |
obj := subjectChannel value
|
|
221 |
].
|
|
222 |
^ self valueUsingSubject:obj
|
|
223 |
!
|
|
224 |
|
733
|
225 |
value:newValue
|
|
226 |
"set my value & send change notifications to my dependents
|
|
227 |
if it changed. But only if my subject does not itself
|
|
228 |
send change notifications (otherwise, we'd send double notifications)."
|
|
229 |
|
|
230 |
|oldValue|
|
|
231 |
|
|
232 |
oldValue := self value.
|
|
233 |
oldValue ~~ newValue ifTrue:[
|
|
234 |
self setValue:newValue.
|
|
235 |
subjectSendsUpdates ifFalse:[
|
|
236 |
self changed:#value
|
|
237 |
]
|
|
238 |
]
|
|
239 |
|
|
240 |
"Created: / 1.11.1997 / 14:04:45 / cg"
|
|
241 |
!
|
|
242 |
|
69
|
243 |
valueUsingSubject:anObject
|
|
244 |
"return the value from anObject, using the selectors in accessPath.
|
|
245 |
A helper for value."
|
|
246 |
|
|
247 |
|obj|
|
|
248 |
|
|
249 |
obj := anObject.
|
|
250 |
accessPath notNil ifTrue:[
|
|
251 |
accessPath do:[:aSelectorOrIndex |
|
|
252 |
aSelectorOrIndex isInteger ifTrue:[
|
|
253 |
obj := obj at:aSelectorOrIndex
|
|
254 |
] ifFalse:[
|
|
255 |
obj := obj perform:aSelectorOrIndex
|
|
256 |
]
|
|
257 |
].
|
|
258 |
].
|
|
259 |
^ obj
|
|
260 |
! !
|
|
261 |
|
|
262 |
!ProtocolAdaptor methodsFor:'accessing-spec'!
|
|
263 |
|
125
|
264 |
accessPath
|
231
|
265 |
"return the access path"
|
|
266 |
|
125
|
267 |
^ accessPath
|
231
|
268 |
|
|
269 |
"Modified: 27.4.1996 / 16:20:48 / cg"
|
125
|
270 |
!
|
69
|
271 |
|
125
|
272 |
accessPath:aCollectionOfSelectors
|
231
|
273 |
"set the access path"
|
|
274 |
|
125
|
275 |
accessPath := aCollectionOfSelectors
|
231
|
276 |
|
|
277 |
"Modified: 27.4.1996 / 16:20:53 / cg"
|
125
|
278 |
!
|
|
279 |
|
|
280 |
subject
|
231
|
281 |
"return the subject"
|
|
282 |
|
125
|
283 |
^ subject
|
231
|
284 |
|
|
285 |
"Modified: 27.4.1996 / 16:21:03 / cg"
|
69
|
286 |
!
|
|
287 |
|
125
|
288 |
subject:anObject
|
231
|
289 |
"set the subject"
|
|
290 |
|
125
|
291 |
subject notNil ifTrue:[
|
231
|
292 |
subject removeDependent:self
|
125
|
293 |
].
|
|
294 |
subject := anObject.
|
|
295 |
self changed:#value.
|
|
296 |
subject notNil ifTrue:[
|
231
|
297 |
subject addDependent:self
|
125
|
298 |
].
|
231
|
299 |
|
|
300 |
"Modified: 27.4.1996 / 16:21:09 / cg"
|
125
|
301 |
!
|
69
|
302 |
|
125
|
303 |
subjectChannel
|
231
|
304 |
"return the subjectChannel"
|
|
305 |
|
125
|
306 |
^ subjectChannel
|
231
|
307 |
|
|
308 |
"Modified: 27.4.1996 / 16:21:21 / cg"
|
69
|
309 |
!
|
|
310 |
|
|
311 |
subjectChannel:aValueHolder
|
231
|
312 |
"set the subjectChannel"
|
|
313 |
|
69
|
314 |
|oldChannel|
|
|
315 |
|
100
|
316 |
subjectChannel notNil ifTrue:[
|
231
|
317 |
subjectChannel removeDependent:self
|
100
|
318 |
].
|
69
|
319 |
oldChannel := subjectChannel.
|
|
320 |
subjectChannel := aValueHolder.
|
100
|
321 |
subjectChannel notNil ifTrue:[
|
231
|
322 |
subjectChannel addDependent:self
|
100
|
323 |
].
|
69
|
324 |
oldChannel notNil ifTrue:[
|
231
|
325 |
self changed:#value.
|
69
|
326 |
].
|
100
|
327 |
|
|
328 |
"Modified: 6.9.1995 / 01:19:27 / claus"
|
231
|
329 |
"Modified: 27.4.1996 / 16:21:26 / cg"
|
69
|
330 |
!
|
|
331 |
|
125
|
332 |
subjectSendsUpdates
|
|
333 |
"return true, if the subject sends updates itself
|
|
334 |
If true, the receiver will not send updates on changes"
|
|
335 |
|
|
336 |
^ subjectSendsUpdates
|
69
|
337 |
!
|
|
338 |
|
125
|
339 |
subjectSendsUpdates:aBoolean
|
|
340 |
"set/clear the flag which states if the subject sends updates itself.
|
|
341 |
If true, the receiver will not send updates on changes"
|
69
|
342 |
|
125
|
343 |
subjectSendsUpdates := aBoolean.
|
69
|
344 |
! !
|
|
345 |
|
125
|
346 |
!ProtocolAdaptor methodsFor:'change & update'!
|
|
347 |
|
|
348 |
update:something with:aPArameter from:changedObject
|
|
349 |
"translate updates from my subject into value-changes towards
|
|
350 |
my dependents. Since I have no specific aspect, every change is forwarded"
|
|
351 |
|
|
352 |
(changedObject == subject
|
|
353 |
or:[changedObject == subjectChannel]) ifTrue:[
|
|
354 |
self changed:#value.
|
|
355 |
^ self
|
|
356 |
].
|
|
357 |
! !
|
|
358 |
|
|
359 |
!ProtocolAdaptor methodsFor:'initialization'!
|
|
360 |
|
|
361 |
initialize
|
231
|
362 |
"setup, assuming that the subject does not send change notifications"
|
|
363 |
|
125
|
364 |
super initialize.
|
231
|
365 |
subjectSendsUpdates := false.
|
|
366 |
|
|
367 |
"Modified: 27.4.1996 / 16:21:56 / cg"
|
125
|
368 |
! !
|
|
369 |
|
129
|
370 |
!ProtocolAdaptor class methodsFor:'documentation'!
|
|
371 |
|
|
372 |
version
|
3301
|
373 |
^ '$Header: /cvs/stx/stx/libview2/ProtocolAdaptor.st,v 1.15 2014-03-05 22:13:03 cg Exp $'
|
129
|
374 |
! !
|
3301
|
375 |
|