author | Claus Gittinger <cg@exept.de> |
Mon, 22 Apr 1996 13:02:23 +0200 | |
changeset 191 | cb2815b77100 |
parent 114 | e577a2f332d0 |
child 212 | 874afdeb380a |
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 |
||
67 | 13 |
Object subclass:#PrintConverter |
100 | 14 |
instanceVariableNames:'valueToStringBlock stringToValueBlock type' |
67 | 15 |
classVariableNames:'' |
16 |
poolDictionaries:'' |
|
17 |
category:'Interface-Support' |
|
18 |
! |
|
19 |
||
20 |
!PrintConverter class methodsFor:'documentation'! |
|
21 |
||
75 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
25 |
All Rights Reserved |
|
26 |
||
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
67 | 36 |
version |
114
e577a2f332d0
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
100
diff
changeset
|
37 |
^ '$Header: /cvs/stx/stx/libview2/Attic/PrintConv.st,v 1.9 1995-11-11 16:04:54 cg Exp $' |
67 | 38 |
! |
39 |
||
40 |
documentation |
|
41 |
" |
|
86 | 42 |
printConverters can be used with labels and editFields to convert |
43 |
an object to/from some printed representation. |
|
67 | 44 |
Conversion is done via two blocks which can be set at instance |
45 |
creation time - either as custom blocks ot to one of the |
|
86 | 46 |
standard conversions. There are a number of standard setups, for |
47 |
common conversions; if none of them fits your needs, create a custom |
|
48 |
converter, by defining its two conversion blocks. |
|
67 | 49 |
|
50 |
Notice: this class was implemented using protocol information |
|
51 |
from alpha testers - it may not be complete or compatible to |
|
52 |
the corresponding ST-80 class. If you encounter any incompatibilities, |
|
53 |
please forward a note to the ST/X team. |
|
54 |
" |
|
55 |
! |
|
56 |
||
57 |
examples |
|
58 |
" |
|
86 | 59 |
stupid examples: |
60 |
convert date <-> string: |
|
61 |
||
69 | 62 |
|conv| |
67 | 63 |
|
69 | 64 |
conv := (PrintConverter new) |
65 |
toPrint:[:date | date printString] |
|
66 |
toRead:[:string | Date readFromString:string]. |
|
67 |
(conv printStringFor:(Date today)) inspect. |
|
68 |
(conv readValueFrom:(Date today printString)) inspect |
|
69 |
||
70 |
||
86 | 71 |
convert number <-> string: |
72 |
||
69 | 73 |
|conv| |
74 |
||
75 |
conv := (PrintConverter new) initForNumber. |
|
76 |
(conv printStringFor:12345) inspect. |
|
77 |
(conv readValueFrom:'12345') inspect |
|
67 | 78 |
|
79 |
||
86 | 80 |
convert boolean <-> string: |
81 |
||
69 | 82 |
|conv| |
83 |
||
84 |
conv := (PrintConverter new) initForYesNo. |
|
85 |
(conv printStringFor:true). |
|
86 |
(conv printStringFor:false). |
|
87 |
(conv readValueFrom:'yes'). |
|
88 |
(conv readValueFrom:'no'). |
|
89 |
'if language is german:'. |
|
90 |
(conv readValueFrom:'ja'). |
|
91 |
(conv readValueFrom:'nein') |
|
92 |
||
86 | 93 |
concrete examples: |
94 |
convert in an inputField: |
|
67 | 95 |
|
69 | 96 |
|dialog field| |
97 |
||
98 |
dialog := Dialog new. |
|
99 |
dialog addTextLabel:'a number (and only numbers):'. |
|
100 |
dialog addVerticalSpace. |
|
101 |
field := dialog addInputFieldOn:(0 asValue). |
|
102 |
field converter:(PrintConverter new initForNumber). |
|
103 |
field immediateAccept:true. |
|
104 |
dialog addOkButton. |
|
105 |
dialog open. |
|
106 |
dialog accepted ifTrue:[ |
|
107 |
Transcript showCr:field editValue |
|
108 |
] |
|
67 | 109 |
|
86 | 110 |
convert a models value for a label: |
111 |
||
112 |
|top v1 v2 l1 l2| |
|
113 |
||
114 |
v1 := 0 asValue. |
|
115 |
v2 := Date today asValue. |
|
116 |
||
117 |
top := StandardSystemView new. |
|
118 |
top extent:200@200. |
|
119 |
||
120 |
l1 := Label origin:0.0@0.0 corner:1.0@0.5 in:top. |
|
121 |
l1 converter:(PrintConverter new initForInteger). |
|
122 |
l1 model:v1; labelMessage:#value; aspect:#value. |
|
123 |
l1 level:-1; inset:10. |
|
124 |
||
125 |
l2 := Label origin:0.0@0.5 corner:1.0@1.0 in:top. |
|
126 |
l2 converter:(PrintConverter new initForDate). |
|
127 |
l2 model:v2; labelMessage:#value; aspect:#value. |
|
128 |
l2 level:-1; inset:10. |
|
129 |
||
130 |
top open. |
|
131 |
||
132 |
'now, change the values ...'. |
|
133 |
[ |
|
134 |
1 to:50 do:[:i| |
|
135 |
v1 value:(v1 value + 1). |
|
136 |
v2 value:(v2 value addDays:1). |
|
137 |
(Delay forSeconds:0.5) wait |
|
138 |
] |
|
139 |
] fork |
|
140 |
||
141 |
convert a models value for a label with limited precision |
|
142 |
float conversion: |
|
143 |
||
144 |
|top v l1 l2| |
|
145 |
||
146 |
v := 0.0 asValue. |
|
147 |
||
148 |
top := StandardSystemView new. |
|
149 |
top extent:200@200. |
|
150 |
||
151 |
l1 := Label origin:0.0@0.0 corner:1.0@0.5 in:top. |
|
152 |
l1 converter:(PrintConverter new initForFloat). |
|
153 |
l1 model:v; labelMessage:#value; aspect:#value. |
|
154 |
l1 level:-1; inset:10. |
|
155 |
||
156 |
l2 := Label origin:0.0@0.5 corner:1.0@1.0 in:top. |
|
157 |
l2 converter:(PrintConverter new initForFloatWithPrecision:2). |
|
158 |
l2 model:v; labelMessage:#value; aspect:#value. |
|
159 |
l2 level:-1; inset:10. |
|
160 |
||
161 |
top open. |
|
162 |
||
163 |
'now, change the values ...'. |
|
164 |
[ |
|
165 |
1 to:100 do:[:i| |
|
166 |
v value:(v value + 0.005). |
|
167 |
(Delay forSeconds:0.5) wait |
|
168 |
] |
|
169 |
] fork |
|
170 |
||
171 |
a custom converter, converting a number to either 'odd' |
|
172 |
or 'even' strings (we only need a one-way converter for labels): |
|
173 |
||
174 |
|top v l1 l2| |
|
175 |
||
176 |
v := 0 asValue. |
|
177 |
||
178 |
top := StandardSystemView new. |
|
179 |
top extent:200@200. |
|
180 |
||
181 |
l1 := Label origin:0.0@0.0 corner:1.0@0.5 in:top. |
|
182 |
l1 converter:(PrintConverter new initForInteger). |
|
183 |
l1 model:v; labelMessage:#value; aspect:#value. |
|
184 |
l1 level:-1; inset:10. |
|
185 |
||
186 |
l2 := Label origin:0.0@0.5 corner:1.0@1.0 in:top. |
|
187 |
l2 converter:(PrintConverter |
|
188 |
new |
|
189 |
toPrint:[:num | num even ifTrue:['even'] |
|
190 |
ifFalse:['odd']] |
|
191 |
toRead:[:string | ]). |
|
192 |
l2 model:v; labelMessage:#value; aspect:#value. |
|
193 |
l2 level:-1; inset:10. |
|
194 |
||
195 |
top open. |
|
196 |
||
197 |
'now, change the values ...'. |
|
198 |
[ |
|
199 |
1 to:100 do:[:i| |
|
200 |
v value:(v value + 1). |
|
201 |
(Delay forSeconds:0.5) wait |
|
202 |
] |
|
203 |
] fork |
|
67 | 204 |
|
69 | 205 |
see more examples in the EditField examples. |
67 | 206 |
" |
207 |
! ! |
|
208 |
||
209 |
!PrintConverter class methodsFor:'instance creation'! |
|
210 |
||
211 |
new |
|
212 |
^ (super new) |
|
213 |
toPrint:[:val | val] |
|
214 |
toRead:[:string | string] |
|
215 |
! ! |
|
216 |
||
86 | 217 |
!PrintConverter class methodsFor:'utilities'! |
218 |
||
219 |
print:aNumber formattedBy:formatString |
|
220 |
"take the digits of aNumbers printString, and squash them |
|
221 |
into formatString, where #-characters are replaced by |
|
222 |
successive characters from the printString. |
|
223 |
Warning: use with care - it does not check for decimal points etc. |
|
224 |
the printString of aNumber must have enough digits for all |
|
225 |
#-characters to be replaced. |
|
226 |
Therefore, precheck the numbers value and use appropriate format |
|
227 |
strings then. |
|
228 |
For number formatting, see also: printfPrintString: implementations." |
|
229 |
||
230 |
|pS fS out| |
|
231 |
||
232 |
pS := ReadStream on:aNumber printString. |
|
233 |
fS := ReadStream on:formatString. |
|
234 |
out := WriteStream on:String new. |
|
235 |
[fS atEnd] whileFalse:[ |
|
236 |
|c| |
|
237 |
||
238 |
c := fS next. |
|
239 |
c == $# ifTrue:[ |
|
240 |
c := pS next |
|
241 |
]. |
|
242 |
out nextPut:c |
|
243 |
]. |
|
244 |
^ out contents |
|
245 |
||
246 |
" |
|
247 |
PrintConverter print:'123456789' formattedBy:'US$ ###,###.##' |
|
248 |
||
249 |
fails for: |
|
250 |
PrintConverter print:'1234' formattedBy:'US$ ###,###.##' |
|
251 |
||
252 |
invalid string for: |
|
253 |
PrintConverter print:'123456789' formattedBy:'US$ ###' |
|
254 |
" |
|
255 |
! ! |
|
256 |
||
100 | 257 |
!PrintConverter methodsFor:'accessing'! |
258 |
||
259 |
type |
|
260 |
"return the type if its one of the standard converters, |
|
261 |
#number, #string etc. nil otherwise" |
|
262 |
||
263 |
^ type |
|
264 |
! ! |
|
265 |
||
67 | 266 |
!PrintConverter methodsFor:'initialization'! |
267 |
||
100 | 268 |
initFor:aTypeSymbol |
269 |
"initialize to convert to/from objects as specified by aTypeSymbol, |
|
270 |
which may be one of #number, #string, #symbol, #date or #password ..." |
|
271 |
||
272 |
aTypeSymbol == #number ifTrue:[ |
|
273 |
self initForNumber |
|
274 |
]. |
|
275 |
(aTypeSymbol == #string or:[aTypeSymbol == #password]) ifTrue:[ |
|
276 |
self initForString |
|
277 |
]. |
|
278 |
aTypeSymbol == #date ifTrue:[ |
|
279 |
self initForDate |
|
280 |
]. |
|
281 |
aTypeSymbol == #symbol ifTrue:[ |
|
282 |
self initForSymbol |
|
283 |
]. |
|
284 |
||
285 |
type := aTypeSymbol |
|
286 |
||
287 |
"Modified: 6.9.1995 / 12:10:38 / claus" |
|
288 |
! |
|
289 |
||
67 | 290 |
toPrint:printBlock toRead:readBlock |
86 | 291 |
"initialize to convert using two custom blocks. |
292 |
printBlock is supposed to get the objects value as argument, |
|
293 |
and to return a string. |
|
294 |
readBlock is supposed to get a string as argument, and return |
|
295 |
the corresponding object." |
|
69 | 296 |
|
67 | 297 |
valueToStringBlock := printBlock. |
298 |
stringToValueBlock := readBlock. |
|
299 |
! |
|
300 |
||
69 | 301 |
initForInteger |
302 |
"initialize to convert to/from an integer |
|
303 |
- if the string is empty or invalid, convert to 0" |
|
304 |
||
86 | 305 |
valueToStringBlock := [:num | num truncated printString]. |
69 | 306 |
stringToValueBlock := [:string | Integer readFromString:string onError:0] |
307 |
! |
|
308 |
||
309 |
initForIntegerOrNil |
|
310 |
"initialize to convert to/from an integer |
|
311 |
- if the string is empty or invalid, convert to nil" |
|
312 |
||
313 |
valueToStringBlock := [:num | |
|
314 |
num isNil ifTrue:[''] |
|
315 |
ifFalse:[num printString]]. |
|
316 |
stringToValueBlock := [:string | |
|
317 |
Integer readFromString:string onError:nil] |
|
318 |
! |
|
319 |
||
86 | 320 |
initForFloat |
321 |
"initialize to convert to/from an float |
|
322 |
- if the string is empty or invalid, convert to 0" |
|
323 |
||
324 |
valueToStringBlock := [:num | num asFloat printString]. |
|
325 |
stringToValueBlock := [:string | Float readFromString:string onError:0] |
|
326 |
! |
|
327 |
||
328 |
initForFloatWithPrecision:nDigits |
|
329 |
"initialize to convert to/from an float with nDigits after the decimal point |
|
330 |
(truncating remaining digits). |
|
331 |
- if the string is empty or invalid, convert to 0" |
|
332 |
||
333 |
|conv| |
|
334 |
||
335 |
conv := '%.' , nDigits printString , 'f'. |
|
336 |
valueToStringBlock := [:num | num asFloat printfPrintString:conv]. |
|
337 |
stringToValueBlock := [:string | Float readFromString:string onError:0] |
|
338 |
! |
|
339 |
||
67 | 340 |
initForNumber |
69 | 341 |
"initialize to convert to/from a number |
342 |
- if the string is empty or invalid, convert to 0" |
|
343 |
||
67 | 344 |
valueToStringBlock := [:num | num printString]. |
345 |
stringToValueBlock := [:string | Number readFromString:string onError:0] |
|
346 |
! |
|
347 |
||
348 |
initForNumberOrNil |
|
69 | 349 |
"initialize to convert to/from a number |
350 |
- if the string is empty or invalid, convert to nil" |
|
351 |
||
352 |
valueToStringBlock := [:num | |
|
353 |
num isNil ifTrue:[''] |
|
354 |
ifFalse:[num printString]]. |
|
355 |
stringToValueBlock := [:string | |
|
356 |
Number readFromString:string onError:nil] |
|
67 | 357 |
! |
358 |
||
359 |
initForString |
|
69 | 360 |
"initialize for a string - this is trivial" |
361 |
||
67 | 362 |
valueToStringBlock := [:val | val isNil ifTrue:[''] ifFalse:[val]]. |
363 |
stringToValueBlock := [:string | string] |
|
364 |
! |
|
365 |
||
366 |
initForDate |
|
69 | 367 |
"initialize to convert to/from a date |
368 |
- if the string is empty or invalid, convert to the current date" |
|
369 |
||
67 | 370 |
valueToStringBlock := [:date | date printString]. |
371 |
stringToValueBlock := [:string | Date readFromString:string onError:[Date today]] |
|
68 | 372 |
! |
373 |
||
69 | 374 |
initForDateOrNil |
375 |
"initialize to convert to/from a date |
|
376 |
- if the string is empty or invalid, convert to nil" |
|
377 |
||
378 |
valueToStringBlock := [:date | date printString]. |
|
379 |
stringToValueBlock := [:string | Date readFromString:string onError:nil] |
|
380 |
! |
|
381 |
||
68 | 382 |
initForYesNo |
69 | 383 |
"initialize to convert a 'yes'/'no' string to/from a boolean |
384 |
The string is supposed to be in the current Language |
|
385 |
(i.e. if german, ja/nein has to be enterred. |
|
386 |
Invalid entries are converted to false." |
|
387 |
||
68 | 388 |
valueToStringBlock := [:bool | bool ifTrue:[ApplicationModel classResources string:'yes'] |
389 |
ifFalse:[ApplicationModel classResources string:'no']]. |
|
390 |
stringToValueBlock := [:string | string = (ApplicationModel classResources string:'yes')] |
|
67 | 391 |
! ! |
392 |
||
393 |
!PrintConverter methodsFor:'converting'! |
|
394 |
||
395 |
printStringFor:aValue |
|
100 | 396 |
"sent when an inputField wants a models value to be converted to a string |
397 |
for display" |
|
398 |
||
67 | 399 |
^ valueToStringBlock value:aValue |
400 |
! |
|
401 |
||
402 |
readValueFrom:aString |
|
100 | 403 |
"sent when an inputField wants a string to be converted to a value |
404 |
to be returned as its contents value" |
|
405 |
||
67 | 406 |
^ stringToValueBlock value:aString |
407 |
! ! |