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 |
|
69
|
13 |
'From Smalltalk/X, Version:2.10.5 on 9-may-1995 at 12:03:43 pm'!
|
|
14 |
|
|
15 |
ValueModel subclass:#ProtocolAdaptor
|
|
16 |
instanceVariableNames:'accessPath subject subjectChannel subjectSendsUpdates'
|
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
71
|
19 |
category:'Interface-Support-Models'
|
69
|
20 |
!
|
|
21 |
|
|
22 |
!ProtocolAdaptor class methodsFor:'documentation'!
|
|
23 |
|
|
24 |
copyright
|
|
25 |
"
|
|
26 |
COPYRIGHT (c) 1995 by Claus Gittinger
|
71
|
27 |
All Rights Reserved
|
69
|
28 |
|
|
29 |
This software is furnished under a license and may be used
|
|
30 |
only in accordance with the terms of that license and with the
|
|
31 |
inclusion of the above copyright notice. This software may not
|
|
32 |
be provided or otherwise made available to, or used by, any
|
|
33 |
other person. No title to or ownership of the software is
|
|
34 |
hereby transferred.
|
|
35 |
"
|
|
36 |
!
|
|
37 |
|
|
38 |
version
|
|
39 |
"
|
100
|
40 |
$Header: /cvs/stx/stx/libview2/ProtocolAdaptor.st,v 1.6 1995-09-09 02:29:58 claus Exp $
|
69
|
41 |
"
|
|
42 |
!
|
|
43 |
|
|
44 |
documentation
|
|
45 |
"
|
|
46 |
a ProtocolAdaptor allows access to embeded values in a
|
|
47 |
complex model and plays model towards the outside world.
|
|
48 |
|
|
49 |
Consider the case where editFields are required for the
|
|
50 |
elements (instance variables) of a more complex model.
|
|
51 |
Using ValueHolders, you had to copy the individual
|
|
52 |
values out-of and into multiple valueHolders.
|
|
53 |
A protocolAdaptor makes this easier, by playing model towards
|
|
54 |
the editField, returning a value from the complex model,
|
|
55 |
and forwards changes to the complex model.
|
|
56 |
|
|
57 |
Notice: since you can specify the aspect- and changeSymbols in most ST/X
|
|
58 |
widgets, ProtocolAdapters are not always needed (at least, if no access-
|
|
59 |
path is required).
|
|
60 |
However, if you want to apply widgets on objects which where not originally
|
|
61 |
designed as models (such as Arrays), ProtocolAdapters are very useful.
|
|
62 |
|
|
63 |
Notice: this class was implemented using protocol information
|
|
64 |
from alpha testers - it may not be complete or compatible to
|
|
65 |
the corresponding ST-80 class. If you encounter any incompatibilities,
|
|
66 |
please forward a note to the ST/X team.
|
|
67 |
"
|
|
68 |
!
|
|
69 |
|
|
70 |
examples
|
|
71 |
"
|
|
72 |
|a obj|
|
|
73 |
|
|
74 |
a := ProtocolAdaptor accessPath:#(1 2 3).
|
|
75 |
obj := Array with:#(11 (121 122 123) 13)
|
|
76 |
with:#(21 (221 222 223) 23)
|
|
77 |
with:#(33 (321 322 323) 33).
|
|
78 |
a valueUsingSubject:obj
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|a obj|
|
|
83 |
|
|
84 |
a := ProtocolAdaptor accessPath:#(1 2 origin).
|
|
85 |
obj := Array with:(Array with:1@1 with:(1@2 corner:100@100))
|
|
86 |
with:(Array with:2@1 with:2@2)
|
|
87 |
with:(Array with:3@1 with:3@2).
|
|
88 |
a valueUsingSubject:obj
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|a model|
|
|
93 |
|
|
94 |
a := ProtocolAdaptor accessPath:#(1 2 origin).
|
|
95 |
model := (Array with:(Array with:1@1 with:(1@2 corner:100@100))
|
|
96 |
with:(Array with:2@1 with:2@2)
|
|
97 |
with:(Array with:3@1 with:3@2)) asValue.
|
|
98 |
a subjectChannel:model.
|
|
99 |
a value
|
|
100 |
"
|
|
101 |
! !
|
|
102 |
|
|
103 |
!ProtocolAdaptor class methodsFor:'instance creation'!
|
|
104 |
|
|
105 |
accessPath:aCollectionOfSelectors
|
|
106 |
^ (self new) accessPath:aCollectionOfSelectors
|
|
107 |
!
|
|
108 |
|
|
109 |
subject:anObject
|
|
110 |
^ (self new) subject:anObject
|
|
111 |
!
|
|
112 |
|
|
113 |
subject:anObject sendsUpdates:aBoolean
|
|
114 |
^ (self new) subject:anObject; sendsUpdates:aBoolean
|
|
115 |
!
|
|
116 |
|
|
117 |
subject:anObject accessPath:aCollectionOfSelectors
|
|
118 |
^ (self new) subject:anObject; accessPath:aCollectionOfSelectors
|
|
119 |
!
|
|
120 |
|
|
121 |
subject:anObject sendsUpdates:aBoolean accessPath:aCollectionOfSelectors
|
|
122 |
^ (self new) subject:anObject; sendsUpdates:aBoolean; accessPath:aCollectionOfSelectors
|
|
123 |
!
|
|
124 |
|
|
125 |
subjectChannel:aValueHolder
|
|
126 |
^ (self new) subjectChannel:aValueHolder
|
|
127 |
!
|
|
128 |
|
|
129 |
subjectChannel:aValueHolder sendsUpdates:aBoolean
|
|
130 |
^ (self new) subjectChannel:aValueHolder; sendsUpdates:aBoolean
|
|
131 |
!
|
|
132 |
|
|
133 |
subjectChannel:aValueHolder accessPath:aCollectionOfSelectors
|
|
134 |
^ (self new) subjectChannel:aValueHolder; accessPath:aCollectionOfSelectors
|
|
135 |
!
|
|
136 |
|
|
137 |
subjectChannel:aValueHolder sendsUpdates:aBoolean accessPath:aCollectionOfSelectors
|
|
138 |
^ (self new) subjectChannel:aValueHolder; sendsUpdates:aBoolean; accessPath:aCollectionOfSelectors
|
|
139 |
! !
|
|
140 |
|
|
141 |
!ProtocolAdaptor methodsFor:'accessing'!
|
|
142 |
|
|
143 |
setValue:newValue usingSubject:anObject
|
|
144 |
"set a value in anObject, using the selectors in accessPath.
|
|
145 |
A helper for setValue:."
|
|
146 |
|
|
147 |
|obj lastIndex|
|
|
148 |
|
|
149 |
obj := anObject.
|
|
150 |
lastIndex := accessPath size.
|
|
151 |
accessPath keysAndValuesDo:[:idx :aSelectorOrIndex |
|
|
152 |
aSelectorOrIndex isInteger ifTrue:[
|
|
153 |
idx == lastIndex ifTrue:[
|
|
154 |
obj at:aSelectorOrIndex put:newValue
|
|
155 |
] ifFalse:[
|
|
156 |
obj := obj at:aSelectorOrIndex
|
|
157 |
]
|
|
158 |
] ifFalse:[
|
|
159 |
idx == lastIndex ifTrue:[
|
|
160 |
obj perform:(aSelectorOrIndex , ':') asSymbol with:newValue
|
|
161 |
] ifFalse:[
|
|
162 |
obj := obj perform:aSelectorOrIndex
|
|
163 |
]
|
|
164 |
]
|
|
165 |
].
|
|
166 |
^ newValue
|
|
167 |
!
|
|
168 |
|
|
169 |
valueUsingSubject:anObject
|
|
170 |
"return the value from anObject, using the selectors in accessPath.
|
|
171 |
A helper for value."
|
|
172 |
|
|
173 |
|obj|
|
|
174 |
|
|
175 |
obj := anObject.
|
|
176 |
accessPath notNil ifTrue:[
|
|
177 |
accessPath do:[:aSelectorOrIndex |
|
|
178 |
aSelectorOrIndex isInteger ifTrue:[
|
|
179 |
obj := obj at:aSelectorOrIndex
|
|
180 |
] ifFalse:[
|
|
181 |
obj := obj perform:aSelectorOrIndex
|
|
182 |
]
|
|
183 |
].
|
|
184 |
].
|
|
185 |
^ obj
|
|
186 |
!
|
|
187 |
|
|
188 |
value
|
|
189 |
"return the value from my subject or subjectChannel."
|
|
190 |
|
|
191 |
|obj|
|
|
192 |
|
|
193 |
subject notNil ifTrue:[
|
|
194 |
obj := subject.
|
|
195 |
] ifFalse:[
|
|
196 |
obj := subjectChannel value
|
|
197 |
].
|
|
198 |
^ self valueUsingSubject:obj
|
|
199 |
!
|
|
200 |
|
|
201 |
setValue:newValue
|
|
202 |
"set the value in my subject or subjectChannel."
|
|
203 |
|
|
204 |
|obj|
|
|
205 |
|
|
206 |
subject notNil ifTrue:[
|
|
207 |
obj := subject.
|
|
208 |
] ifFalse:[
|
|
209 |
obj := subjectChannel value
|
|
210 |
].
|
|
211 |
^ self setValue:newValue usingSubject:obj
|
|
212 |
! !
|
|
213 |
|
86
|
214 |
!ProtocolAdaptor ignoredMethodsFor:'change notification'!
|
69
|
215 |
|
|
216 |
notifyChange
|
|
217 |
subjectSendsUpdates ifFalse:[
|
71
|
218 |
super notifyChange:#value
|
69
|
219 |
]
|
|
220 |
! !
|
|
221 |
|
86
|
222 |
!ProtocolAdaptor methodsFor:'change notification'!
|
|
223 |
|
|
224 |
changed:aspect
|
|
225 |
subjectSendsUpdates ifFalse:[
|
|
226 |
super changed:aspect
|
|
227 |
]
|
|
228 |
! !
|
|
229 |
|
69
|
230 |
!ProtocolAdaptor methodsFor:'initialization'!
|
|
231 |
|
|
232 |
initialize
|
|
233 |
super initialize.
|
|
234 |
subjectSendsUpdates := false.
|
|
235 |
! !
|
|
236 |
|
|
237 |
!ProtocolAdaptor methodsFor:'change & update'!
|
|
238 |
|
|
239 |
update:something with:aPArameter from:changedObject
|
|
240 |
"translate updates from my subject into value-changes towards
|
|
241 |
my dependents. Since I have no specific aspect, every change is forwarded"
|
|
242 |
|
|
243 |
(changedObject == subject
|
|
244 |
or:[changedObject == subjectChannel]) ifTrue:[
|
|
245 |
self changed:#value.
|
|
246 |
^ self
|
|
247 |
].
|
|
248 |
! !
|
|
249 |
|
|
250 |
!ProtocolAdaptor methodsFor:'accessing-spec'!
|
|
251 |
|
|
252 |
subjectSendsUpdates
|
|
253 |
"return true, if the subject sends updates itself
|
|
254 |
If true, the receiver will not send updates on changes"
|
|
255 |
|
|
256 |
^ subjectSendsUpdates
|
|
257 |
!
|
|
258 |
|
|
259 |
subjectSendsUpdates:aBoolean
|
|
260 |
"set/clear the flag which states if the subject sends updates itself.
|
|
261 |
If true, the receiver will not send updates on changes"
|
|
262 |
|
|
263 |
subjectSendsUpdates := aBoolean.
|
|
264 |
!
|
|
265 |
|
|
266 |
subjectChannel:aValueHolder
|
|
267 |
|oldChannel|
|
|
268 |
|
100
|
269 |
subjectChannel notNil ifTrue:[
|
|
270 |
subjectChannel removeDependent:self
|
|
271 |
].
|
69
|
272 |
oldChannel := subjectChannel.
|
|
273 |
subjectChannel := aValueHolder.
|
100
|
274 |
subjectChannel notNil ifTrue:[
|
|
275 |
subjectChannel addDependent:self
|
|
276 |
].
|
69
|
277 |
oldChannel notNil ifTrue:[
|
|
278 |
self changed:#value.
|
|
279 |
].
|
100
|
280 |
|
|
281 |
"Modified: 6.9.1995 / 01:19:27 / claus"
|
69
|
282 |
!
|
|
283 |
|
|
284 |
subjectChannel
|
|
285 |
^ subjectChannel
|
|
286 |
!
|
|
287 |
|
|
288 |
subject:anObject
|
|
289 |
subject notNil ifTrue:[
|
|
290 |
subject removeDependent:self
|
|
291 |
].
|
|
292 |
subject := anObject.
|
|
293 |
self changed:#value.
|
|
294 |
subject notNil ifTrue:[
|
|
295 |
subject addDependent:self
|
|
296 |
].
|
|
297 |
!
|
|
298 |
|
|
299 |
subject
|
|
300 |
^ subject
|
|
301 |
!
|
|
302 |
|
|
303 |
accessPath:aCollectionOfSelectors
|
|
304 |
accessPath := aCollectionOfSelectors
|
|
305 |
!
|
|
306 |
|
|
307 |
accessPath
|
|
308 |
^ accessPath
|
|
309 |
! !
|
|
310 |
|