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