67
|
1 |
Object subclass:#PrintConverter
|
|
2 |
instanceVariableNames:'valueToStringBlock stringToValueBlock'
|
|
3 |
classVariableNames:''
|
|
4 |
poolDictionaries:''
|
|
5 |
category:'Interface-Support'
|
|
6 |
!
|
|
7 |
|
|
8 |
!PrintConverter class methodsFor:'documentation'!
|
|
9 |
|
|
10 |
version
|
|
11 |
"
|
68
|
12 |
$Header: /cvs/stx/stx/libview2/PrintConverter.st,v 1.2 1995-05-06 14:14:34 claus Exp $
|
67
|
13 |
"
|
|
14 |
!
|
|
15 |
|
|
16 |
documentation
|
|
17 |
"
|
|
18 |
printConverters are used with editFields to convert an object
|
|
19 |
to/from some printed representation.
|
|
20 |
Conversion is done via two blocks which can be set at instance
|
|
21 |
creation time - either as custom blocks ot to one of the
|
|
22 |
standard conversions.
|
|
23 |
|
|
24 |
Notice: this class was implemented using protocol information
|
|
25 |
from alpha testers - it may not be complete or compatible to
|
|
26 |
the corresponding ST-80 class. If you encounter any incompatibilities,
|
|
27 |
please forward a note to the ST/X team.
|
|
28 |
"
|
|
29 |
!
|
|
30 |
|
|
31 |
examples
|
|
32 |
"
|
|
33 |
|conv|
|
|
34 |
|
|
35 |
conv := (PrintConverter new)
|
|
36 |
toPrint:[:date | date printString]
|
|
37 |
toRead:[:string | Date readFromString:string].
|
|
38 |
(conv printStringFor:(Date today)) inspect.
|
|
39 |
(conv readValueFrom:(Date today printString)) inspect
|
|
40 |
|
|
41 |
|
|
42 |
|conv|
|
|
43 |
|
|
44 |
conv := (PrintConverter new) initForNumber.
|
|
45 |
(conv printStringFor:12345) inspect.
|
|
46 |
(conv readValueFrom:'12345') inspect
|
|
47 |
|
|
48 |
|
68
|
49 |
|conv|
|
|
50 |
|
|
51 |
conv := (PrintConverter new) initForYesNo.
|
|
52 |
(conv printStringFor:true).
|
|
53 |
(conv printStringFor:false).
|
|
54 |
(conv readValueFrom:'yes').
|
|
55 |
(conv readValueFrom:'no').
|
|
56 |
'if language is german:'.
|
|
57 |
(conv readValueFrom:'ja').
|
|
58 |
(conv readValueFrom:'nein')
|
|
59 |
|
67
|
60 |
see concrete uses in the EditField examples.
|
|
61 |
"
|
|
62 |
! !
|
|
63 |
|
|
64 |
!PrintConverter class methodsFor:'instance creation'!
|
|
65 |
|
|
66 |
new
|
|
67 |
^ (super new)
|
|
68 |
toPrint:[:val | val]
|
|
69 |
toRead:[:string | string]
|
|
70 |
! !
|
|
71 |
|
|
72 |
!PrintConverter methodsFor:'initialization'!
|
|
73 |
|
|
74 |
toPrint:printBlock toRead:readBlock
|
|
75 |
valueToStringBlock := printBlock.
|
|
76 |
stringToValueBlock := readBlock.
|
|
77 |
!
|
|
78 |
|
|
79 |
initForNumber
|
|
80 |
valueToStringBlock := [:num | num printString].
|
|
81 |
stringToValueBlock := [:string | Number readFromString:string onError:0]
|
|
82 |
!
|
|
83 |
|
|
84 |
initForNumberOrNil
|
|
85 |
valueToStringBlock := [:num | num isNil ifTrue:[''] ifFalse:[num printString]].
|
|
86 |
stringToValueBlock := [:string | Number readFromString:string onError:nil]
|
|
87 |
!
|
|
88 |
|
|
89 |
initForString
|
|
90 |
valueToStringBlock := [:val | val isNil ifTrue:[''] ifFalse:[val]].
|
|
91 |
stringToValueBlock := [:string | string]
|
|
92 |
!
|
|
93 |
|
|
94 |
initForDate
|
|
95 |
valueToStringBlock := [:date | date printString].
|
|
96 |
stringToValueBlock := [:string | Date readFromString:string onError:[Date today]]
|
68
|
97 |
!
|
|
98 |
|
|
99 |
initForYesNo
|
|
100 |
valueToStringBlock := [:bool | bool ifTrue:[ApplicationModel classResources string:'yes']
|
|
101 |
ifFalse:[ApplicationModel classResources string:'no']].
|
|
102 |
stringToValueBlock := [:string | string = (ApplicationModel classResources string:'yes')]
|
67
|
103 |
! !
|
|
104 |
|
|
105 |
!PrintConverter methodsFor:'converting'!
|
|
106 |
|
|
107 |
printStringFor:aValue
|
|
108 |
^ valueToStringBlock value:aValue
|
|
109 |
!
|
|
110 |
|
|
111 |
readValueFrom:aString
|
|
112 |
^ stringToValueBlock value:aString
|
|
113 |
! !
|