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