PrintConverter.st
changeset 69 225a9efd50f5
parent 68 43b867285d01
child 75 a53337dc3e19
equal deleted inserted replaced
68:43b867285d01 69:225a9efd50f5
     7 
     7 
     8 !PrintConverter class methodsFor:'documentation'!
     8 !PrintConverter class methodsFor:'documentation'!
     9 
     9 
    10 version 
    10 version 
    11 "
    11 "
    12 $Header: /cvs/stx/stx/libview2/PrintConverter.st,v 1.2 1995-05-06 14:14:34 claus Exp $
    12 $Header: /cvs/stx/stx/libview2/PrintConverter.st,v 1.3 1995-05-09 00:21:50 claus Exp $
    13 "
    13 "
    14 !
    14 !
    15 
    15 
    16 documentation
    16 documentation
    17 "
    17 "
    28 "
    28 "
    29 !
    29 !
    30 
    30 
    31 examples 
    31 examples 
    32 "
    32 "
    33     |conv|
    33     stupid examples"
       
    34       |conv|
    34 
    35 
    35     conv := (PrintConverter new)
    36       conv := (PrintConverter new)
    36 		toPrint:[:date | date printString]
    37 		  toPrint:[:date | date printString]
    37 		toRead:[:string | Date readFromString:string].
    38 		  toRead:[:string | Date readFromString:string].
    38     (conv printStringFor:(Date today)) inspect.
    39       (conv printStringFor:(Date today)) inspect.
    39     (conv readValueFrom:(Date today printString)) inspect
    40       (conv readValueFrom:(Date today printString)) inspect
    40 
    41 
    41 
    42 
    42     |conv|
    43       |conv|
    43 
    44 
    44     conv := (PrintConverter new) initForNumber.
    45       conv := (PrintConverter new) initForNumber.
    45     (conv printStringFor:12345) inspect.
    46       (conv printStringFor:12345) inspect.
    46     (conv readValueFrom:'12345') inspect
    47       (conv readValueFrom:'12345') inspect
    47 
    48 
    48 
    49 
    49     |conv|
    50       |conv|
    50 
    51 
    51     conv := (PrintConverter new) initForYesNo.
    52       conv := (PrintConverter new) initForYesNo.
    52     (conv printStringFor:true).  
    53       (conv printStringFor:true).  
    53     (conv printStringFor:false).    
    54       (conv printStringFor:false).    
    54     (conv readValueFrom:'yes').  
    55       (conv readValueFrom:'yes').  
    55     (conv readValueFrom:'no').  
    56       (conv readValueFrom:'no').  
    56     'if language is german:'.
    57       'if language is german:'.
    57     (conv readValueFrom:'ja').    
    58       (conv readValueFrom:'ja').    
    58     (conv readValueFrom:'nein')  
    59       (conv readValueFrom:'nein')  
    59 
    60 
    60     see concrete uses in the EditField examples.
    61     concrete example: convert in an inputField:
       
    62 
       
    63       |dialog field|
       
    64 
       
    65       dialog := Dialog new.
       
    66       dialog addTextLabel:'a number (and only numbers):'.
       
    67       dialog addVerticalSpace.
       
    68       field := dialog addInputFieldOn:(0 asValue).
       
    69       field converter:(PrintConverter new initForNumber).
       
    70       field immediateAccept:true.
       
    71       dialog addOkButton.
       
    72       dialog open.
       
    73       dialog accepted ifTrue:[
       
    74 	  Transcript showCr:field editValue
       
    75       ]
       
    76 
       
    77 
       
    78     see more examples in the EditField examples.
    61 "
    79 "
    62 ! !
    80 ! !
    63 
    81 
    64 !PrintConverter class methodsFor:'instance creation'!
    82 !PrintConverter class methodsFor:'instance creation'!
    65 
    83 
    70 ! !
    88 ! !
    71 
    89 
    72 !PrintConverter methodsFor:'initialization'!
    90 !PrintConverter methodsFor:'initialization'!
    73 
    91 
    74 toPrint:printBlock toRead:readBlock
    92 toPrint:printBlock toRead:readBlock
       
    93     "initialize to convert using two custom blocks"
       
    94 
    75     valueToStringBlock := printBlock.
    95     valueToStringBlock := printBlock.
    76     stringToValueBlock := readBlock.
    96     stringToValueBlock := readBlock.
    77 !
    97 !
    78 
    98 
       
    99 initForInteger
       
   100     "initialize to convert to/from an integer 
       
   101      - if the string is empty or invalid, convert to 0"
       
   102 
       
   103     valueToStringBlock := [:num | num printString].
       
   104     stringToValueBlock := [:string | Integer readFromString:string onError:0]
       
   105 !
       
   106 
       
   107 initForIntegerOrNil
       
   108     "initialize to convert to/from an integer 
       
   109      - if the string is empty or invalid, convert to nil"
       
   110 
       
   111     valueToStringBlock := [:num | 
       
   112 	num isNil ifTrue:[''] 
       
   113 		  ifFalse:[num printString]].
       
   114     stringToValueBlock := [:string | 
       
   115 	Integer readFromString:string onError:nil]
       
   116 !
       
   117 
    79 initForNumber
   118 initForNumber
       
   119     "initialize to convert to/from a number 
       
   120      - if the string is empty or invalid, convert to 0"
       
   121 
    80     valueToStringBlock := [:num | num printString].
   122     valueToStringBlock := [:num | num printString].
    81     stringToValueBlock := [:string | Number readFromString:string onError:0]
   123     stringToValueBlock := [:string | Number readFromString:string onError:0]
    82 !
   124 !
    83 
   125 
    84 initForNumberOrNil
   126 initForNumberOrNil
    85     valueToStringBlock := [:num | num isNil ifTrue:[''] ifFalse:[num printString]].
   127     "initialize to convert to/from a number 
    86     stringToValueBlock := [:string | Number readFromString:string onError:nil]
   128      - if the string is empty or invalid, convert to nil"
       
   129 
       
   130     valueToStringBlock := [:num | 
       
   131 	num isNil ifTrue:[''] 
       
   132 		  ifFalse:[num printString]].
       
   133     stringToValueBlock := [:string | 
       
   134 	Number readFromString:string onError:nil]
    87 !
   135 !
    88 
   136 
    89 initForString
   137 initForString
       
   138     "initialize for a string - this is trivial"
       
   139 
    90     valueToStringBlock := [:val | val isNil ifTrue:[''] ifFalse:[val]].
   140     valueToStringBlock := [:val | val isNil ifTrue:[''] ifFalse:[val]].
    91     stringToValueBlock := [:string | string]
   141     stringToValueBlock := [:string | string]
    92 !
   142 !
    93 
   143 
    94 initForDate
   144 initForDate
       
   145     "initialize to convert to/from a date 
       
   146      - if the string is empty or invalid, convert to the current date"
       
   147 
    95     valueToStringBlock := [:date | date printString].
   148     valueToStringBlock := [:date | date printString].
    96     stringToValueBlock := [:string | Date readFromString:string onError:[Date today]]
   149     stringToValueBlock := [:string | Date readFromString:string onError:[Date today]]
    97 !
   150 !
    98 
   151 
       
   152 initForDateOrNil
       
   153     "initialize to convert to/from a date 
       
   154      - if the string is empty or invalid, convert to nil"
       
   155 
       
   156     valueToStringBlock := [:date | date printString].
       
   157     stringToValueBlock := [:string | Date readFromString:string onError:nil]
       
   158 !
       
   159 
    99 initForYesNo
   160 initForYesNo
       
   161     "initialize to convert a 'yes'/'no' string to/from a boolean 
       
   162      The string is supposed to be in the current Language
       
   163      (i.e. if german, ja/nein has to be enterred.
       
   164      Invalid entries are converted to false."
       
   165 
   100     valueToStringBlock := [:bool | bool ifTrue:[ApplicationModel classResources string:'yes']
   166     valueToStringBlock := [:bool | bool ifTrue:[ApplicationModel classResources string:'yes']
   101 					ifFalse:[ApplicationModel classResources string:'no']].
   167 					ifFalse:[ApplicationModel classResources string:'no']].
   102     stringToValueBlock := [:string | string = (ApplicationModel classResources string:'yes')]
   168     stringToValueBlock := [:string | string = (ApplicationModel classResources string:'yes')]
   103 ! !
   169 ! !
   104 
   170