diff -r 43b867285d01 -r 225a9efd50f5 PrintConverter.st --- a/PrintConverter.st Sat May 06 16:15:18 1995 +0200 +++ b/PrintConverter.st Tue May 09 02:23:22 1995 +0200 @@ -9,7 +9,7 @@ version " -$Header: /cvs/stx/stx/libview2/PrintConverter.st,v 1.2 1995-05-06 14:14:34 claus Exp $ +$Header: /cvs/stx/stx/libview2/PrintConverter.st,v 1.3 1995-05-09 00:21:50 claus Exp $ " ! @@ -30,34 +30,52 @@ examples " - |conv| + stupid 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 := (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| + + 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') + + concrete example: convert in an inputField: - conv := (PrintConverter new) initForNumber. - (conv printStringFor:12345) inspect. - (conv readValueFrom:'12345') inspect + |dialog field| + + dialog := Dialog new. + dialog addTextLabel:'a number (and only numbers):'. + dialog addVerticalSpace. + field := dialog addInputFieldOn:(0 asValue). + field converter:(PrintConverter new initForNumber). + field immediateAccept:true. + dialog addOkButton. + dialog open. + dialog accepted ifTrue:[ + Transcript showCr:field editValue + ] - |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. + see more examples in the EditField examples. " ! ! @@ -72,31 +90,79 @@ !PrintConverter methodsFor:'initialization'! toPrint:printBlock toRead:readBlock + "initialize to convert using two custom blocks" + valueToStringBlock := printBlock. stringToValueBlock := readBlock. ! +initForInteger + "initialize to convert to/from an integer + - if the string is empty or invalid, convert to 0" + + valueToStringBlock := [:num | num printString]. + stringToValueBlock := [:string | Integer readFromString:string onError:0] +! + +initForIntegerOrNil + "initialize to convert to/from an integer + - if the string is empty or invalid, convert to nil" + + valueToStringBlock := [:num | + num isNil ifTrue:[''] + ifFalse:[num printString]]. + stringToValueBlock := [:string | + Integer readFromString:string onError:nil] +! + initForNumber + "initialize to convert to/from a number + - if the string is empty or invalid, convert to 0" + 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] + "initialize to convert to/from a number + - if the string is empty or invalid, convert to nil" + + valueToStringBlock := [:num | + num isNil ifTrue:[''] + ifFalse:[num printString]]. + stringToValueBlock := [:string | + Number readFromString:string onError:nil] ! initForString + "initialize for a string - this is trivial" + valueToStringBlock := [:val | val isNil ifTrue:[''] ifFalse:[val]]. stringToValueBlock := [:string | string] ! initForDate + "initialize to convert to/from a date + - if the string is empty or invalid, convert to the current date" + valueToStringBlock := [:date | date printString]. stringToValueBlock := [:string | Date readFromString:string onError:[Date today]] ! +initForDateOrNil + "initialize to convert to/from a date + - if the string is empty or invalid, convert to nil" + + valueToStringBlock := [:date | date printString]. + stringToValueBlock := [:string | Date readFromString:string onError:nil] +! + initForYesNo + "initialize to convert a 'yes'/'no' string to/from a boolean + The string is supposed to be in the current Language + (i.e. if german, ja/nein has to be enterred. + Invalid entries are converted to false." + valueToStringBlock := [:bool | bool ifTrue:[ApplicationModel classResources string:'yes'] ifFalse:[ApplicationModel classResources string:'no']]. stringToValueBlock := [:string | string = (ApplicationModel classResources string:'yes')]