PrintConverter.st
changeset 67 e48bf03eb059
child 68 43b867285d01
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PrintConverter.st	Wed May 03 20:15:57 1995 +0200
@@ -0,0 +1,96 @@
+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.1 1995-05-03 18:15:57 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
+
+
+    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]]
+! !
+
+!PrintConverter methodsFor:'converting'!
+
+printStringFor:aValue
+    ^ valueToStringBlock value:aValue
+!
+
+readValueFrom:aString
+    ^ stringToValueBlock value:aString
+! !