PrintConverter.st
changeset 67 e48bf03eb059
child 68 43b867285d01
equal deleted inserted replaced
66:6ee963fd8e27 67:e48bf03eb059
       
     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 "
       
    12 $Header: /cvs/stx/stx/libview2/PrintConverter.st,v 1.1 1995-05-03 18:15:57 claus Exp $
       
    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 
       
    49     see concrete uses in the EditField examples.
       
    50 "
       
    51 ! !
       
    52 
       
    53 !PrintConverter class methodsFor:'instance creation'!
       
    54 
       
    55 new
       
    56     ^ (super new) 
       
    57 	toPrint:[:val | val]
       
    58 	toRead:[:string | string]
       
    59 ! !
       
    60 
       
    61 !PrintConverter methodsFor:'initialization'!
       
    62 
       
    63 toPrint:printBlock toRead:readBlock
       
    64     valueToStringBlock := printBlock.
       
    65     stringToValueBlock := readBlock.
       
    66 !
       
    67 
       
    68 initForNumber
       
    69     valueToStringBlock := [:num | num printString].
       
    70     stringToValueBlock := [:string | Number readFromString:string onError:0]
       
    71 !
       
    72 
       
    73 initForNumberOrNil
       
    74     valueToStringBlock := [:num | num isNil ifTrue:[''] ifFalse:[num printString]].
       
    75     stringToValueBlock := [:string | Number readFromString:string onError:nil]
       
    76 !
       
    77 
       
    78 initForString
       
    79     valueToStringBlock := [:val | val isNil ifTrue:[''] ifFalse:[val]].
       
    80     stringToValueBlock := [:string | string]
       
    81 !
       
    82 
       
    83 initForDate
       
    84     valueToStringBlock := [:date | date printString].
       
    85     stringToValueBlock := [:string | Date readFromString:string onError:[Date today]]
       
    86 ! !
       
    87 
       
    88 !PrintConverter methodsFor:'converting'!
       
    89 
       
    90 printStringFor:aValue
       
    91     ^ valueToStringBlock value:aValue
       
    92 !
       
    93 
       
    94 readValueFrom:aString
       
    95     ^ stringToValueBlock value:aString
       
    96 ! !