added: a converter for number - string, string lwftpaddedTo: with:
authortm
Tue, 14 Nov 2000 19:17:13 +0100
changeset 1426 6a5f5372fcc4
parent 1425 b2db2ec8a083
child 1427 b3e355574ccf
added: a converter for number - string, string lwftpaddedTo: with:
TypeConverter.st
--- a/TypeConverter.st	Mon Nov 13 12:03:13 2000 +0100
+++ b/TypeConverter.st	Tue Nov 14 19:17:13 2000 +0100
@@ -15,10 +15,10 @@
 "{ Package: 'stx:libview2' }"
 
 PluggableAdaptor subclass:#TypeConverter
-        instanceVariableNames:''
-        classVariableNames:''
-        poolDictionaries:''
-        category:'Interface-Support-Models'
+	instanceVariableNames:''
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Interface-Support-Models'
 !
 
 !TypeConverter class methodsFor:'documentation'!
@@ -160,6 +160,23 @@
     "Modified: 4.3.1997 / 11:51:41 / cg"
 !
 
+onNumberValue:aValueHolder leftPaddedTo:aSize with:aCharacter
+    "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) numberToTextLeftPaddedTo:aSize with:aCharacter
+
+"
+  |m|
+  m := 5 asValue.
+  ((EditField model:(TypeConverter onNumberValue:m leftPaddedTo:2 with:$-)) width:100) openAt:(200@200).
+  m inspect.
+"
+!
+
 onNumberValue:aValueHolder minValue:min maxValue:max
     "create and return a typeConverter, which retrieves
      a values string representation via #value, and converts
@@ -752,6 +769,39 @@
     "Modified: 21.2.1997 / 18:59:44 / cg"
 !
 
+numberToTextLeftPaddedTo:aSize with:aCharacter
+    "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:[
+                    numericValue printStringLeftPaddedTo:aSize with:aCharacter
+                ]]
+
+        putBlock:
+                [:model :string |
+
+                |value|
+
+                string isEmpty ifTrue:[
+                    value := 0
+                ] ifFalse:[          
+                    value := Number readFromString: string onError:[0]. "asNumberFromFormatString:formatString"
+                ].
+                model value:value]
+
+        updateBlock: [:m :a :p | true]
+
+    "Modified: 21.2.1997 / 18:59:44 / cg"
+!
+
 numberToTextMinValue:minVal maxValue:maxVal
     "setup the converter to convert from a string to a number
      and vice versa, but clamping the number into the range."
@@ -1009,5 +1059,5 @@
 !TypeConverter class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.34 2000-09-14 16:38:40 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.35 2000-11-14 18:17:13 tm Exp $'
 ! !