PrintConverter.st
author claus
Sat, 06 May 1995 16:15:18 +0200
changeset 68 43b867285d01
parent 67 e48bf03eb059
child 69 225a9efd50f5
permissions -rw-r--r--
.

Object subclass:#PrintConverter 
	 instanceVariableNames:'valueToStringBlock stringToValueBlock'
	 classVariableNames:''
	 poolDictionaries:''
	 category:'Interface-Support'
!

!PrintConverter class methodsFor:'documentation'!

version 
"
$Header: /cvs/stx/stx/libview2/PrintConverter.st,v 1.2 1995-05-06 14:14:34 claus Exp $
"
!

documentation
"
    printConverters are used with editFields to convert an object
    to/from some printed representation.
    Conversion is done via two blocks which can be set at instance
    creation time - either as custom blocks ot to one of the
    standard conversions.

    Notice: this class was implemented using protocol information
    from alpha testers - it may not be complete or compatible to
    the corresponding ST-80 class. If you encounter any incompatibilities,
    please forward a note to the ST/X team.
"
!

examples 
"
    |conv|

    conv := (PrintConverter new)
		toPrint:[:date | date printString]
		toRead:[:string | Date readFromString:string].
    (conv printStringFor:(Date today)) inspect.
    (conv readValueFrom:(Date today printString)) inspect


    |conv|

    conv := (PrintConverter new) initForNumber.
    (conv printStringFor:12345) inspect.
    (conv readValueFrom:'12345') inspect


    |conv|

    conv := (PrintConverter new) initForYesNo.
    (conv printStringFor:true).  
    (conv printStringFor:false).    
    (conv readValueFrom:'yes').  
    (conv readValueFrom:'no').  
    'if language is german:'.
    (conv readValueFrom:'ja').    
    (conv readValueFrom:'nein')  

    see concrete uses in the EditField examples.
"
! !

!PrintConverter class methodsFor:'instance creation'!

new
    ^ (super new) 
	toPrint:[:val | val]
	toRead:[:string | string]
! !

!PrintConverter methodsFor:'initialization'!

toPrint:printBlock toRead:readBlock
    valueToStringBlock := printBlock.
    stringToValueBlock := readBlock.
!

initForNumber
    valueToStringBlock := [:num | num printString].
    stringToValueBlock := [:string | Number readFromString:string onError:0]
!

initForNumberOrNil
    valueToStringBlock := [:num | num isNil ifTrue:[''] ifFalse:[num printString]].
    stringToValueBlock := [:string | Number readFromString:string onError:nil]
!

initForString
    valueToStringBlock := [:val | val isNil ifTrue:[''] ifFalse:[val]].
    stringToValueBlock := [:string | string]
!

initForDate
    valueToStringBlock := [:date | date printString].
    stringToValueBlock := [:string | Date readFromString:string onError:[Date today]]
!

initForYesNo
    valueToStringBlock := [:bool | bool ifTrue:[ApplicationModel classResources string:'yes']
					ifFalse:[ApplicationModel classResources string:'no']].
    stringToValueBlock := [:string | string = (ApplicationModel classResources string:'yes')]
! !

!PrintConverter methodsFor:'converting'!

printStringFor:aValue
    ^ valueToStringBlock value:aValue
!

readValueFrom:aString
    ^ stringToValueBlock value:aString
! !