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