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