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