TypeConverter.st
changeset 1463 df2fec4aad9c
parent 1426 6a5f5372fcc4
child 1464 883cfaa89ffa
--- a/TypeConverter.st	Thu Mar 22 16:56:04 2001 +0100
+++ b/TypeConverter.st	Wed Mar 28 19:45:00 2001 +0200
@@ -249,21 +249,30 @@
 !TypeConverter methodsFor:'input testing'!
 
 setNumberValue: value inModel: model fromInput: string
-    "for not valid number input characters the model is set to nil. 
-     By this, the dependents are forced to update their contents."
-
-    |lastInputChar|
+    "for invalid numbers, the model is set to nil. 
+     By this, the dependents are forced to update their contents
+     (that is: a bad input string will lead to a cleared input field here)."
 
-    string notEmpty
-    ifTrue:
-    [
-        1 to: string size - 1 do: [:i| (string at: i) isLetter ifTrue: [model setValue:nil]].
+    |lastInputChar needChange|
+
+    string notEmpty ifTrue:[
+        1 to: string size - 1 do: [:i| (string at: i) isLetter ifTrue: [needChange := true]].
         lastInputChar := string at: string size.
         (lastInputChar isDigit not and: [lastInputChar isPrintable and: [
         (string size > 1               or: [lastInputChar ~~ $-])  and: [
-        (string occurrencesOf: $.) > 1 or: [lastInputChar ~~ $.]]]]) ifTrue: [model setValue:nil].
+        (string occurrencesOf: $.) > 1 or: [lastInputChar ~~ $.]]]]) ifTrue: [needChange := true].
     ].
 
+    needChange == true ifTrue:[
+        value notNil ifTrue:[
+            model setValue:nil
+        ] ifFalse:[
+            model value isNil ifTrue:[
+                model changed:#value.
+                ^ self
+            ]
+        ].
+    ].
     model value:value
 ! !
 
@@ -544,6 +553,50 @@
     "Created: 21.2.1997 / 18:58:38 / cg"
 !
 
+numberOrNilToTextMinValue:minVal maxValue:maxVal
+    "setup the converter to convert from a string to a number or nil
+     and vice versa, but clamping the number into the range."
+
+    self
+        getBlock:[:model |
+                |numericValue|
+
+                (numericValue := model value) isNil ifTrue:[
+                    String new
+                ] ifFalse:[
+                    numericValue printString
+                ]]
+
+        putBlock:
+                [:model :string |
+
+                |value c|
+
+                (string isEmpty or:[string isBlank]) ifTrue:[
+                    value := nil
+                ] ifFalse:[
+                    value := Number readFromString:string onError:[nil]. 
+                    value isNil ifTrue:[
+                        
+                    ] ifFalse:[
+                        minVal notNil ifTrue:[
+                            (value < minVal and: [string notEmpty]) ifTrue: [model setValue:nil].
+                            value := value max:minVal.
+                        ].
+                        maxVal notNil ifTrue:[
+                            (value > maxVal and: [string notEmpty]) ifTrue: [model setValue:nil].
+                            value := value min:maxVal.
+                        ].
+                    ].
+                ].
+                self setNumberValue: value inModel: model fromInput: string.
+                ]
+
+        updateBlock: [:m :a :p | true]
+
+    "Modified: 21.2.1997 / 18:59:44 / cg"
+!
+
 numberOrPointOrNil
     "setup the converter to convert from a string to a number or point
      and vice versa. Invalid numbers/points are converted to nil."
@@ -1059,5 +1112,5 @@
 !TypeConverter class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.35 2000-11-14 18:17:13 tm Exp $'
+    ^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.36 2001-03-28 17:45:00 cg Exp $'
 ! !