442
|
1 |
PluggableAdaptor subclass:#TypeConverter
|
|
2 |
instanceVariableNames:''
|
|
3 |
classVariableNames:''
|
|
4 |
poolDictionaries:''
|
|
5 |
category:'Interface-Support-Models'
|
|
6 |
!
|
|
7 |
|
|
8 |
!TypeConverter class methodsFor:'documentation'!
|
|
9 |
|
|
10 |
documentation
|
|
11 |
"
|
|
12 |
a typeConverter can be used as an editFields model,
|
|
13 |
to convert the fields string value to some object.
|
|
14 |
No real new functionality is added here - all is inherited
|
|
15 |
from PluggableAdapter; however, some specialized instance creation
|
|
16 |
methods are added here..
|
|
17 |
|
|
18 |
Notice:
|
|
19 |
this class was implemented using protocol information
|
|
20 |
from alpha testers - it may not be complete or compatible to
|
|
21 |
the corresponding ST-80 class.
|
|
22 |
If you encounter any incompatibilities, please forward a note
|
|
23 |
describing the incompatibility verbal (i.e. no code) to the ST/X team.
|
|
24 |
|
|
25 |
[author:]
|
|
26 |
Claus Gittinger
|
|
27 |
"
|
|
28 |
|
|
29 |
|
|
30 |
!
|
|
31 |
|
|
32 |
examples
|
|
33 |
"
|
|
34 |
convert a number to a string:
|
|
35 |
[exBegin]
|
|
36 |
|v t i|
|
|
37 |
|
|
38 |
v := 1 asValue.
|
|
39 |
|
|
40 |
t := HorizontalPanelView new.
|
|
41 |
t extent:200@50.
|
|
42 |
t horizontalLayout:#fitSpace.
|
|
43 |
|
|
44 |
i := EditField in:t.
|
|
45 |
i model:(TypeConverter onNumberValue:v).
|
|
46 |
t open.
|
|
47 |
|
|
48 |
(Delay forSeconds:3) wait.
|
|
49 |
v value:2.
|
|
50 |
[exEnd]
|
|
51 |
"
|
|
52 |
|
|
53 |
! !
|
|
54 |
|
|
55 |
!TypeConverter class methodsFor:'instance creation'!
|
|
56 |
|
|
57 |
onNumberValue:aValueHolder
|
|
58 |
"create and return a typeConverter, which retrieves
|
|
59 |
a values string representation via #value, and converts
|
|
60 |
a number-string to a value via #value:.
|
|
61 |
Useful as an editFields model, which operates on some
|
|
62 |
numeric value (or aspectAdaptor, which adapts to a numeric slot)"
|
|
63 |
|
|
64 |
^ (self on:aValueHolder) numberToText
|
|
65 |
|
|
66 |
"Modified: 21.2.1997 / 18:46:11 / cg"
|
446
|
67 |
!
|
|
68 |
|
|
69 |
onSymbolValue:aValueHolder
|
|
70 |
"create and return a typeConverter, which retrieves
|
|
71 |
a symbol-values string representation via #value, and converts
|
|
72 |
a string to a symbol via #value:.
|
|
73 |
Useful as an editFields model, which operates on some
|
|
74 |
numeric value (or aspectAdaptor, which adapts to a numeric slot)"
|
|
75 |
|
|
76 |
^ (self on:aValueHolder) symbolOrNil
|
|
77 |
|
|
78 |
"Modified: 21.2.1997 / 18:46:11 / cg"
|
442
|
79 |
! !
|
|
80 |
|
|
81 |
!TypeConverter methodsFor:'accessing'!
|
|
82 |
|
|
83 |
subject
|
|
84 |
"return the cobverted subject"
|
|
85 |
|
|
86 |
^ model
|
|
87 |
|
|
88 |
"Created: 21.2.1997 / 18:45:12 / cg"
|
|
89 |
!
|
|
90 |
|
|
91 |
value:newValue
|
|
92 |
"convert and change"
|
|
93 |
|
|
94 |
self setValue:newValue
|
|
95 |
|
|
96 |
"Created: 21.2.1997 / 18:45:39 / cg"
|
|
97 |
! !
|
|
98 |
|
|
99 |
!TypeConverter methodsFor:'initialize-release'!
|
|
100 |
|
|
101 |
numberOrNil
|
|
102 |
"setup the converter to convert from a string to a number
|
|
103 |
and vice versa. Invalid numbers are converted to nil."
|
|
104 |
|
|
105 |
self
|
|
106 |
getBlock:[:model |
|
|
107 |
|numericValue|
|
|
108 |
|
|
109 |
(numericValue := model value) isNil ifTrue:[
|
|
110 |
String new
|
|
111 |
] ifFalse:[
|
|
112 |
numericValue printString
|
|
113 |
]]
|
|
114 |
|
|
115 |
putBlock:
|
|
116 |
[:model :string |
|
|
117 |
|
|
118 |
|value|
|
|
119 |
|
|
120 |
string isEmpty ifTrue:[
|
|
121 |
value := nil
|
|
122 |
] ifFalse:[
|
447
|
123 |
value := Number readFrom:string onError:nil
|
442
|
124 |
].
|
|
125 |
model value:value]
|
|
126 |
|
|
127 |
updateBlock: [:m :a :p | true]
|
|
128 |
|
|
129 |
"Created: 21.2.1997 / 18:58:38 / cg"
|
|
130 |
!
|
|
131 |
|
|
132 |
numberToText
|
|
133 |
"setup the converter to convert from a string to a number
|
|
134 |
and vice versa, using formatString."
|
|
135 |
|
|
136 |
self
|
|
137 |
getBlock:[:model |
|
|
138 |
|numericValue|
|
|
139 |
|
|
140 |
(numericValue := model value) isNil ifTrue:[
|
|
141 |
String new
|
|
142 |
] ifFalse:[
|
|
143 |
numericValue printString
|
|
144 |
]]
|
|
145 |
|
|
146 |
putBlock:
|
|
147 |
[:model :string |
|
|
148 |
|
|
149 |
|value|
|
|
150 |
|
|
151 |
string isEmpty ifTrue:[
|
|
152 |
value := 0
|
|
153 |
] ifFalse:[
|
|
154 |
value := string asNumber
|
|
155 |
].
|
|
156 |
model value:value]
|
|
157 |
|
|
158 |
updateBlock: [:m :a :p | true]
|
|
159 |
|
|
160 |
"Created: 21.2.1997 / 18:57:05 / cg"
|
|
161 |
"Modified: 21.2.1997 / 18:59:06 / cg"
|
|
162 |
!
|
|
163 |
|
|
164 |
numberToTextFormattedBy:formatString
|
|
165 |
"setup the converter to convert from a string to a number
|
|
166 |
and vice versa, using formatString.
|
|
167 |
The formatString is currently ignored when numbers are converted
|
|
168 |
from a string."
|
|
169 |
|
|
170 |
self
|
|
171 |
getBlock:[:model |
|
|
172 |
|numericValue|
|
|
173 |
|
|
174 |
(numericValue := model value) isNil ifTrue:[
|
|
175 |
String new
|
|
176 |
] ifFalse:[
|
|
177 |
PrintConverter print:numericValue formattedBy:formatString
|
|
178 |
]]
|
|
179 |
|
|
180 |
putBlock:
|
|
181 |
[:model :string |
|
|
182 |
|
|
183 |
|value|
|
|
184 |
|
|
185 |
string isEmpty ifTrue:[
|
|
186 |
value := 0
|
|
187 |
] ifFalse:[
|
|
188 |
value := string asNumber "asNumberFromFormatString:formatString"
|
|
189 |
].
|
|
190 |
model value:value]
|
|
191 |
|
|
192 |
updateBlock: [:m :a :p | true]
|
|
193 |
|
|
194 |
"Modified: 21.2.1997 / 18:59:44 / cg"
|
446
|
195 |
!
|
|
196 |
|
|
197 |
symbolOrNil
|
|
198 |
"setup the converter to convert from a string to a symbol
|
|
199 |
and vice versa. Invalid symbols (i.e. empty) are converted to nil."
|
|
200 |
|
|
201 |
self
|
|
202 |
getBlock:[:model |
|
|
203 |
|symbolValue|
|
|
204 |
|
|
205 |
(symbolValue := model value) isNil ifTrue:[
|
|
206 |
String new
|
|
207 |
] ifFalse:[
|
|
208 |
'#' , symbolValue asString
|
|
209 |
]]
|
|
210 |
|
|
211 |
putBlock:
|
|
212 |
[:model :string |
|
|
213 |
|
|
214 |
|value s|
|
|
215 |
|
|
216 |
string isEmpty ifTrue:[
|
|
217 |
value := nil
|
|
218 |
] ifFalse:[
|
|
219 |
s := string withoutSeparators.
|
|
220 |
(s startsWith:'#') ifTrue:[
|
|
221 |
s := s copyFrom:2
|
|
222 |
].
|
|
223 |
value := s asSymbol
|
|
224 |
].
|
|
225 |
model value:value]
|
|
226 |
|
|
227 |
updateBlock: [:m :a :p | true]
|
|
228 |
|
|
229 |
"Created: 21.2.1997 / 18:58:38 / cg"
|
442
|
230 |
! !
|
|
231 |
|
|
232 |
!TypeConverter class methodsFor:'documentation'!
|
|
233 |
|
|
234 |
version
|
447
|
235 |
^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.3 1997-02-24 19:50:19 ca Exp $'
|
442
|
236 |
! !
|