TypeConverter.st
changeset 442 2f5c72b7d46b
child 446 e4a8087792d9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TypeConverter.st	Fri Feb 21 19:09:11 1997 +0100
@@ -0,0 +1,189 @@
+PluggableAdaptor subclass:#TypeConverter
+	instanceVariableNames:''
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Interface-Support-Models'
+!
+
+!TypeConverter class methodsFor:'documentation'!
+
+documentation
+"
+    a typeConverter can be used as an editFields model,
+    to convert the fields string value to some object.
+    No real new functionality is added here - all is inherited
+    from PluggableAdapter; however, some specialized instance creation
+    methods are added here..
+
+    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 
+        describing the incompatibility verbal (i.e. no code) to the ST/X team.
+
+    [author:]
+        Claus Gittinger
+"
+
+
+!
+
+examples 
+"
+    convert a number to a string:
+                                                                        [exBegin]
+        |v t i|
+
+        v := 1 asValue.
+
+        t := HorizontalPanelView new.
+        t extent:200@50.
+        t horizontalLayout:#fitSpace.
+
+        i := EditField in:t.
+        i model:(TypeConverter onNumberValue:v).
+        t open.
+
+        (Delay forSeconds:3) wait.
+        v value:2.
+                                                                        [exEnd]
+"
+
+! !
+
+!TypeConverter class methodsFor:'instance creation'!
+
+onNumberValue:aValueHolder
+    "create and return a typeConverter, which retrieves
+     a values string representation via #value, and converts
+     a number-string to a value via #value:.
+     Useful as an editFields model, which operates on some
+     numeric value (or aspectAdaptor, which adapts to a numeric slot)"
+
+    ^ (self on:aValueHolder) numberToText
+
+    "Modified: 21.2.1997 / 18:46:11 / cg"
+! !
+
+!TypeConverter methodsFor:'accessing'!
+
+subject
+    "return the cobverted subject"
+
+    ^ model
+
+    "Created: 21.2.1997 / 18:45:12 / cg"
+!
+
+value:newValue
+    "convert and change"
+
+    self setValue:newValue
+
+    "Created: 21.2.1997 / 18:45:39 / cg"
+! !
+
+!TypeConverter methodsFor:'initialize-release'!
+
+numberOrNil
+    "setup the converter to convert from a string to a number
+     and vice versa. Invalid numbers are converted to nil."
+
+    self
+        getBlock:[:model |
+                |numericValue|
+
+                (numericValue := model value) isNil ifTrue:[
+                    String new
+                ] ifFalse:[
+                    numericValue printString
+                ]]
+
+        putBlock:
+                [:model :string |
+
+                |value|
+
+                string isEmpty ifTrue:[
+                    value := nil
+                ] ifFalse:[
+                    value := string asNumber
+                ].
+                model value:value]
+
+        updateBlock: [:m :a :p | true]
+
+    "Created: 21.2.1997 / 18:58:38 / cg"
+!
+
+numberToText
+    "setup the converter to convert from a string to a number
+     and vice versa, using formatString."
+
+    self
+        getBlock:[:model |
+                |numericValue|
+
+                (numericValue := model value) isNil ifTrue:[
+                    String new
+                ] ifFalse:[
+                    numericValue printString
+                ]]
+
+        putBlock:
+                [:model :string |
+
+                |value|
+
+                string isEmpty ifTrue:[
+                    value := 0
+                ] ifFalse:[
+                    value := string asNumber
+                ].
+                model value:value]
+
+        updateBlock: [:m :a :p | true]
+
+    "Created: 21.2.1997 / 18:57:05 / cg"
+    "Modified: 21.2.1997 / 18:59:06 / cg"
+!
+
+numberToTextFormattedBy:formatString
+    "setup the converter to convert from a string to a number
+     and vice versa, using formatString.
+     The formatString is currently ignored when numbers are converted
+     from a string."
+
+    self
+        getBlock:[:model |
+                |numericValue|
+
+                (numericValue := model value) isNil ifTrue:[
+                    String new
+                ] ifFalse:[
+                    PrintConverter print:numericValue formattedBy:formatString
+                ]]
+
+        putBlock:
+                [:model :string |
+
+                |value|
+
+                string isEmpty ifTrue:[
+                    value := 0
+                ] ifFalse:[
+                    value := string asNumber "asNumberFromFormatString:formatString"
+                ].
+                model value:value]
+
+        updateBlock: [:m :a :p | true]
+
+    "Modified: 21.2.1997 / 18:59:44 / cg"
+! !
+
+!TypeConverter class methodsFor:'documentation'!
+
+version
+    ^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.1 1997-02-21 18:09:11 cg Exp $'
+! !