author | Stefan Vogel <sv@exept.de> |
Wed, 01 Feb 2017 15:10:20 +0100 | |
changeset 3859 | fd0b1cf871f2 |
parent 3614 | 3272e655d847 |
child 4471 | df7515191c90 |
permissions | -rw-r--r-- |
74 | 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 |
" |
|
2286 | 12 |
"{ Package: 'stx:libview2' }" |
74 | 13 |
|
3614 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
125 | 16 |
ProtocolAdaptor subclass:#AspectAdaptor |
217 | 17 |
instanceVariableNames:'myAspect getMsg putMsg' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Interface-Support-Models' |
|
69 | 21 |
! |
22 |
||
23 |
!AspectAdaptor class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
71 | 28 |
All Rights Reserved |
69 | 29 |
|
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
2988
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
41 |
an AspectAdaptor forwards updates and change messages from/to a complex model. |
69 | 42 |
|
43 |
Consider the case where editFields are required for the |
|
789 | 44 |
elements (instance variables) of a compound object: |
238 | 45 |
- without an aspect adaptor, you needed to copy the individual |
46 |
values out-of the object and move these into multiple valueHolders. |
|
47 |
Then, let the editFields modify the valueHolders contents and |
|
48 |
finally, fetch the values and put them back into the compound object. |
|
49 |
||
69 | 50 |
An aspectAdaptor makes this easier, by playing model with |
85 | 51 |
value/value: symbols towards the editField, and forwarding changes and |
789 | 52 |
updates to/from the compound object using different aspect symbols |
238 | 53 |
and access messages. |
69 | 54 |
|
231 | 55 |
Notice: |
56 |
this class was implemented using protocol information |
|
57 |
from alpha testers - it may not be complete or compatible to |
|
58 |
the corresponding ST-80 class. |
|
59 |
If you encounter any incompatibilities, please forward a note |
|
60 |
describing the incompatibility verbal (i.e. no code) to the ST/X team. |
|
217 | 61 |
|
223 | 62 |
[author:] |
63 |
Claus Gittinger |
|
64 |
||
217 | 65 |
[see also:] |
66 |
ValueHolder Model |
|
69 | 67 |
" |
68 |
! |
|
69 |
||
70 |
examples |
|
71 |
" |
|
72 |
a dialog on a points x/y coordinates: |
|
231 | 73 |
[exBegin] |
74 |
|dialog data f| |
|
69 | 75 |
|
231 | 76 |
data := 0@0. |
69 | 77 |
|
231 | 78 |
dialog := DialogBox new. |
79 |
dialog addTextLabel:'x:'. |
|
80 |
f := dialog addInputFieldOn:(AspectAdaptor new |
|
81 |
subject:data; |
|
82 |
accessWith:#x |
|
83 |
assignWith:#x:). |
|
84 |
f converter:(PrintConverter new initForNumber). |
|
69 | 85 |
|
231 | 86 |
dialog addTextLabel:'y:'. |
87 |
f := dialog addInputFieldOn:(AspectAdaptor new |
|
88 |
subject:data; |
|
89 |
forAspect:#y). |
|
90 |
f converter:(PrintConverter new initForNumber). |
|
69 | 91 |
|
231 | 92 |
dialog addOkButton. |
93 |
data inspect. |
|
94 |
dialog open. |
|
69 | 95 |
|
231 | 96 |
dialog accepted ifTrue:[ |
268 | 97 |
Transcript showCR:'data now: ' , data printString |
231 | 98 |
] |
99 |
[exEnd] |
|
69 | 100 |
|
101 |
||
102 |
a dialog on a four-field complex model: |
|
231 | 103 |
[exBegin] |
104 |
|dialog data dataModel| |
|
69 | 105 |
|
231 | 106 |
data := #('hello' 'one' 'two' 'three'). |
107 |
dataModel := Plug new. |
|
108 |
dataModel respondTo:#field1 with:[data at:1]. |
|
109 |
dataModel respondTo:#field2 with:[data at:2]. |
|
110 |
dataModel respondTo:#field3 with:[data at:3]. |
|
111 |
dataModel respondTo:#field4 with:[data at:4]. |
|
112 |
dataModel respondTo:#field1: with:[:arg | data at:1 put:arg]. |
|
113 |
dataModel respondTo:#field2: with:[:arg | data at:2 put:arg]. |
|
114 |
dataModel respondTo:#field3: with:[:arg | data at:3 put:arg]. |
|
115 |
dataModel respondTo:#field4: with:[:arg | data at:4 put:arg]. |
|
69 | 116 |
|
231 | 117 |
dialog := DialogBox new. |
118 |
dialog addTextLabel:'1:'. |
|
119 |
dialog addInputFieldOn:(AspectAdaptor new |
|
120 |
subject:dataModel; |
|
121 |
accessWith:#field1 |
|
122 |
assignWith:#field1:). |
|
123 |
dialog addTextLabel:'2:'. |
|
124 |
dialog addInputFieldOn:(AspectAdaptor new |
|
125 |
subject:dataModel; |
|
126 |
forAspect:#field2). |
|
127 |
dialog addTextLabel:'3:'. |
|
128 |
dialog addInputFieldOn:(AspectAdaptor new |
|
129 |
subject:dataModel; |
|
130 |
accessWith:#field3 |
|
131 |
assignWith:#field3: |
|
132 |
aspect:#field3). |
|
133 |
dialog addTextLabel:'4:'. |
|
134 |
dialog addInputFieldOn:(AspectAdaptor new |
|
135 |
subject:dataModel; |
|
136 |
forAspect:#field4). |
|
137 |
dialog addOkButton. |
|
138 |
dataModel inspect. |
|
139 |
dialog open. |
|
140 |
dialog accepted ifTrue:[ |
|
268 | 141 |
Transcript showCR:'data now: ' , data printString |
231 | 142 |
] |
143 |
[exEnd] |
|
69 | 144 |
" |
145 |
! ! |
|
146 |
||
147 |
!AspectAdaptor class methodsFor:'instance creation'! |
|
148 |
||
149 |
accessWith:getSelector assignWith:putSelector |
|
83 | 150 |
"create and return an adaptor which uses getSelector to fetch a value |
151 |
and setSelector to change it." |
|
152 |
||
69 | 153 |
^ (self new) accessWith:getSelector assignWith:putSelector |
85 | 154 |
! |
155 |
||
439 | 156 |
forAspect:anAspect |
157 |
"create and return a new adaptor, which forwards requests |
|
158 |
to anObject, using anAspect as get-selector and anAspect-colon as putSelector |
|
159 |
for access. The returned object can be used in place of a ValueHolder" |
|
160 |
||
161 |
^ self new forAspect:anAspect |
|
162 |
||
163 |
"Modified: 22.1.1997 / 12:00:42 / cg" |
|
164 |
! |
|
165 |
||
85 | 166 |
subject:anObject sendsUpdates:aBoolean accessWith:getSel assignWith:putSel aspect:aspect |
379 | 167 |
"create and return a new adaptor, which forwards requests |
168 |
to anObject, using getSel/putSel for access. |
|
169 |
The returned object can be used in place of a ValueHolder" |
|
170 |
||
85 | 171 |
^ (self subject:anObject sendsUpdates:aBoolean) |
379 | 172 |
accessWith:getSel assignWith:putSel aspect:aspect |
173 |
||
174 |
"Modified: 22.1.1997 / 12:00:42 / cg" |
|
69 | 175 |
! ! |
176 |
||
125 | 177 |
!AspectAdaptor methodsFor:'accessing-spec'! |
178 |
||
179 |
accessWith:getSelector assignWith:putSelector |
|
3614 | 180 |
"setup the receiver to use getSelector to fetch a value |
381 | 181 |
and putSelector to change it." |
125 | 182 |
|
183 |
getMsg := getSelector. |
|
184 |
putMsg := putSelector |
|
381 | 185 |
|
186 |
"Modified: 22.1.1997 / 18:28:46 / cg" |
|
125 | 187 |
! |
188 |
||
189 |
accessWith:getSelector assignWith:putSelector aspect:aspectSymbol |
|
3614 | 190 |
"setup the receiver to use getSelector to fetch a value |
381 | 191 |
and putSelector to change it." |
125 | 192 |
|
193 |
getMsg := getSelector. |
|
194 |
putMsg := putSelector. |
|
195 |
myAspect := aspectSymbol |
|
381 | 196 |
|
197 |
"Modified: 22.1.1997 / 18:28:51 / cg" |
|
125 | 198 |
! |
199 |
||
409 | 200 |
aspect:aSelector |
201 |
"set the adapters change aspect - this is the aspect of the update message, |
|
202 |
on which the adaptor reacts" |
|
203 |
||
204 |
myAspect := aSelector. |
|
205 |
! |
|
206 |
||
125 | 207 |
forAspect |
381 | 208 |
"get the adapters aspect - if none was defined, the getMsg is returned" |
209 |
||
125 | 210 |
myAspect isNil ifTrue:[ |
381 | 211 |
^ getMsg |
125 | 212 |
]. |
213 |
^ myAspect |
|
381 | 214 |
|
215 |
"Modified: 22.1.1997 / 18:27:24 / cg" |
|
125 | 216 |
! |
217 |
||
218 |
forAspect:aSelector |
|
381 | 219 |
"set the adapters aspect - this sets both the get- and put-Messages |
220 |
(the putMessage is the aspect with a colon)" |
|
221 |
||
125 | 222 |
getMsg := myAspect := aSelector. |
3300 | 223 |
putMsg := aSelector asMutator. |
381 | 224 |
|
225 |
"Modified: 22.1.1997 / 18:29:05 / cg" |
|
125 | 226 |
! ! |
227 |
||
228 |
!AspectAdaptor methodsFor:'accessing-value'! |
|
229 |
||
2988
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
230 |
defaultValueIfNoSubject |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
231 |
"if there is no subject (taget to provide the value), |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
232 |
this value is returned." |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
233 |
|
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
234 |
^ nil |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
235 |
|
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
236 |
"Created: / 01-03-2012 / 08:53:38 / cg" |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
237 |
! |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
238 |
|
125 | 239 |
setValue:newValue |
381 | 240 |
"set the value - this forwards a putMessage to the target" |
241 |
||
125 | 242 |
|target oldValue| |
243 |
||
244 |
target := super value. |
|
2988
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
245 |
target notNil ifTrue:[ |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
246 |
oldValue := target perform:getMsg. |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
247 |
oldValue ~~ newValue ifTrue:[ |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
248 |
target perform:putMsg with:newValue. |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
249 |
] |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
250 |
]. |
2286 | 251 |
|
2988
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
252 |
"Modified: / 01-03-2012 / 08:54:10 / cg" |
125 | 253 |
! |
254 |
||
255 |
value |
|
256 |
"translate a query for my value from my user |
|
257 |
into an aspect access towards my subject" |
|
258 |
||
259 |
|target| |
|
260 |
||
261 |
target := super value. |
|
2988
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
262 |
target isNil ifTrue:[^ self defaultValueIfNoSubject]. |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
263 |
|
125 | 264 |
^ target perform:getMsg |
2988
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
265 |
|
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
266 |
"Modified (format): / 01-03-2012 / 08:54:33 / cg" |
125 | 267 |
! |
268 |
||
269 |
value:newValue |
|
381 | 270 |
"set the value - this forwards a putMessage to the target |
271 |
and sends out a changeNotification if the value did really change." |
|
272 |
||
125 | 273 |
|target oldValue| |
274 |
||
275 |
target := super value. |
|
2988
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
276 |
target notNil ifTrue:[ |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
277 |
oldValue := target perform:getMsg. |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
278 |
oldValue ~~ newValue ifTrue:[ |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
279 |
target perform:putMsg with:newValue. |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
280 |
subjectSendsUpdates ifFalse:[ |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
281 |
self changed:#value |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
282 |
] |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
283 |
] |
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
284 |
]. |
2286 | 285 |
|
2988
bc154fbdd68d
added: #defaultValueIfNoSubject
Claus Gittinger <cg@exept.de>
parents:
2742
diff
changeset
|
286 |
"Modified: / 01-03-2012 / 08:54:27 / cg" |
125 | 287 |
! ! |
288 |
||
69 | 289 |
!AspectAdaptor methodsFor:'change & update'! |
290 |
||
291 |
update:something with:aParameter from:changedObject |
|
292 |
"translate an update from the model into a #value-change |
|
293 |
via my depenedents ..." |
|
294 |
||
448 | 295 |
((changedObject == subject and:[something == self forAspect]) |
69 | 296 |
or:[changedObject == subjectChannel]) ifTrue:[ |
448 | 297 |
self changed:#value |
69 | 298 |
]. |
299 |
! ! |
|
300 |
||
129 | 301 |
!AspectAdaptor class methodsFor:'documentation'! |
302 |
||
303 |
version |
|
3614 | 304 |
^ '$Header$' |
129 | 305 |
! ! |
3300 | 306 |