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
|
|
14 |
instanceVariableNames:'valueToStringBlock stringToValueBlock'
|
|
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
|
|
37 |
"
|
83
|
38 |
$Header: /cvs/stx/stx/libview2/PrintConverter.st,v 1.5 1995-07-02 16:16:59 claus Exp $
|
67
|
39 |
"
|
|
40 |
!
|
|
41 |
|
|
42 |
documentation
|
|
43 |
"
|
|
44 |
printConverters are used with editFields to convert an object
|
|
45 |
to/from some printed representation.
|
|
46 |
Conversion is done via two blocks which can be set at instance
|
|
47 |
creation time - either as custom blocks ot to one of the
|
|
48 |
standard conversions.
|
|
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 |
"
|
83
|
59 |
stupid examples:
|
69
|
60 |
|conv|
|
67
|
61 |
|
69
|
62 |
conv := (PrintConverter new)
|
|
63 |
toPrint:[:date | date printString]
|
|
64 |
toRead:[:string | Date readFromString:string].
|
|
65 |
(conv printStringFor:(Date today)) inspect.
|
|
66 |
(conv readValueFrom:(Date today printString)) inspect
|
|
67 |
|
|
68 |
|
|
69 |
|conv|
|
|
70 |
|
|
71 |
conv := (PrintConverter new) initForNumber.
|
|
72 |
(conv printStringFor:12345) inspect.
|
|
73 |
(conv readValueFrom:'12345') inspect
|
67
|
74 |
|
|
75 |
|
69
|
76 |
|conv|
|
|
77 |
|
|
78 |
conv := (PrintConverter new) initForYesNo.
|
|
79 |
(conv printStringFor:true).
|
|
80 |
(conv printStringFor:false).
|
|
81 |
(conv readValueFrom:'yes').
|
|
82 |
(conv readValueFrom:'no').
|
|
83 |
'if language is german:'.
|
|
84 |
(conv readValueFrom:'ja').
|
|
85 |
(conv readValueFrom:'nein')
|
|
86 |
|
|
87 |
concrete example: convert in an inputField:
|
67
|
88 |
|
69
|
89 |
|dialog field|
|
|
90 |
|
|
91 |
dialog := Dialog new.
|
|
92 |
dialog addTextLabel:'a number (and only numbers):'.
|
|
93 |
dialog addVerticalSpace.
|
|
94 |
field := dialog addInputFieldOn:(0 asValue).
|
|
95 |
field converter:(PrintConverter new initForNumber).
|
|
96 |
field immediateAccept:true.
|
|
97 |
dialog addOkButton.
|
|
98 |
dialog open.
|
|
99 |
dialog accepted ifTrue:[
|
|
100 |
Transcript showCr:field editValue
|
|
101 |
]
|
67
|
102 |
|
|
103 |
|
69
|
104 |
see more examples in the EditField examples.
|
67
|
105 |
"
|
|
106 |
! !
|
|
107 |
|
|
108 |
!PrintConverter class methodsFor:'instance creation'!
|
|
109 |
|
|
110 |
new
|
|
111 |
^ (super new)
|
|
112 |
toPrint:[:val | val]
|
|
113 |
toRead:[:string | string]
|
|
114 |
! !
|
|
115 |
|
|
116 |
!PrintConverter methodsFor:'initialization'!
|
|
117 |
|
|
118 |
toPrint:printBlock toRead:readBlock
|
69
|
119 |
"initialize to convert using two custom blocks"
|
|
120 |
|
67
|
121 |
valueToStringBlock := printBlock.
|
|
122 |
stringToValueBlock := readBlock.
|
|
123 |
!
|
|
124 |
|
69
|
125 |
initForInteger
|
|
126 |
"initialize to convert to/from an integer
|
|
127 |
- if the string is empty or invalid, convert to 0"
|
|
128 |
|
|
129 |
valueToStringBlock := [:num | num printString].
|
|
130 |
stringToValueBlock := [:string | Integer readFromString:string onError:0]
|
|
131 |
!
|
|
132 |
|
|
133 |
initForIntegerOrNil
|
|
134 |
"initialize to convert to/from an integer
|
|
135 |
- if the string is empty or invalid, convert to nil"
|
|
136 |
|
|
137 |
valueToStringBlock := [:num |
|
|
138 |
num isNil ifTrue:['']
|
|
139 |
ifFalse:[num printString]].
|
|
140 |
stringToValueBlock := [:string |
|
|
141 |
Integer readFromString:string onError:nil]
|
|
142 |
!
|
|
143 |
|
67
|
144 |
initForNumber
|
69
|
145 |
"initialize to convert to/from a number
|
|
146 |
- if the string is empty or invalid, convert to 0"
|
|
147 |
|
67
|
148 |
valueToStringBlock := [:num | num printString].
|
|
149 |
stringToValueBlock := [:string | Number readFromString:string onError:0]
|
|
150 |
!
|
|
151 |
|
|
152 |
initForNumberOrNil
|
69
|
153 |
"initialize to convert to/from a number
|
|
154 |
- if the string is empty or invalid, convert to nil"
|
|
155 |
|
|
156 |
valueToStringBlock := [:num |
|
|
157 |
num isNil ifTrue:['']
|
|
158 |
ifFalse:[num printString]].
|
|
159 |
stringToValueBlock := [:string |
|
|
160 |
Number readFromString:string onError:nil]
|
67
|
161 |
!
|
|
162 |
|
|
163 |
initForString
|
69
|
164 |
"initialize for a string - this is trivial"
|
|
165 |
|
67
|
166 |
valueToStringBlock := [:val | val isNil ifTrue:[''] ifFalse:[val]].
|
|
167 |
stringToValueBlock := [:string | string]
|
|
168 |
!
|
|
169 |
|
|
170 |
initForDate
|
69
|
171 |
"initialize to convert to/from a date
|
|
172 |
- if the string is empty or invalid, convert to the current date"
|
|
173 |
|
67
|
174 |
valueToStringBlock := [:date | date printString].
|
|
175 |
stringToValueBlock := [:string | Date readFromString:string onError:[Date today]]
|
68
|
176 |
!
|
|
177 |
|
69
|
178 |
initForDateOrNil
|
|
179 |
"initialize to convert to/from a date
|
|
180 |
- if the string is empty or invalid, convert to nil"
|
|
181 |
|
|
182 |
valueToStringBlock := [:date | date printString].
|
|
183 |
stringToValueBlock := [:string | Date readFromString:string onError:nil]
|
|
184 |
!
|
|
185 |
|
68
|
186 |
initForYesNo
|
69
|
187 |
"initialize to convert a 'yes'/'no' string to/from a boolean
|
|
188 |
The string is supposed to be in the current Language
|
|
189 |
(i.e. if german, ja/nein has to be enterred.
|
|
190 |
Invalid entries are converted to false."
|
|
191 |
|
68
|
192 |
valueToStringBlock := [:bool | bool ifTrue:[ApplicationModel classResources string:'yes']
|
|
193 |
ifFalse:[ApplicationModel classResources string:'no']].
|
|
194 |
stringToValueBlock := [:string | string = (ApplicationModel classResources string:'yes')]
|
67
|
195 |
! !
|
|
196 |
|
|
197 |
!PrintConverter methodsFor:'converting'!
|
|
198 |
|
|
199 |
printStringFor:aValue
|
|
200 |
^ valueToStringBlock value:aValue
|
|
201 |
!
|
|
202 |
|
|
203 |
readValueFrom:aString
|
|
204 |
^ stringToValueBlock value:aString
|
|
205 |
! !
|