TypeConverter.st
changeset 1463 df2fec4aad9c
parent 1426 6a5f5372fcc4
child 1464 883cfaa89ffa
equal deleted inserted replaced
1462:b7ffa1109142 1463:df2fec4aad9c
   247 ! !
   247 ! !
   248 
   248 
   249 !TypeConverter methodsFor:'input testing'!
   249 !TypeConverter methodsFor:'input testing'!
   250 
   250 
   251 setNumberValue: value inModel: model fromInput: string
   251 setNumberValue: value inModel: model fromInput: string
   252     "for not valid number input characters the model is set to nil. 
   252     "for invalid numbers, the model is set to nil. 
   253      By this, the dependents are forced to update their contents."
   253      By this, the dependents are forced to update their contents
   254 
   254      (that is: a bad input string will lead to a cleared input field here)."
   255     |lastInputChar|
   255 
   256 
   256     |lastInputChar needChange|
   257     string notEmpty
   257 
   258     ifTrue:
   258     string notEmpty ifTrue:[
   259     [
   259         1 to: string size - 1 do: [:i| (string at: i) isLetter ifTrue: [needChange := true]].
   260         1 to: string size - 1 do: [:i| (string at: i) isLetter ifTrue: [model setValue:nil]].
       
   261         lastInputChar := string at: string size.
   260         lastInputChar := string at: string size.
   262         (lastInputChar isDigit not and: [lastInputChar isPrintable and: [
   261         (lastInputChar isDigit not and: [lastInputChar isPrintable and: [
   263         (string size > 1               or: [lastInputChar ~~ $-])  and: [
   262         (string size > 1               or: [lastInputChar ~~ $-])  and: [
   264         (string occurrencesOf: $.) > 1 or: [lastInputChar ~~ $.]]]]) ifTrue: [model setValue:nil].
   263         (string occurrencesOf: $.) > 1 or: [lastInputChar ~~ $.]]]]) ifTrue: [needChange := true].
   265     ].
   264     ].
   266 
   265 
       
   266     needChange == true ifTrue:[
       
   267         value notNil ifTrue:[
       
   268             model setValue:nil
       
   269         ] ifFalse:[
       
   270             model value isNil ifTrue:[
       
   271                 model changed:#value.
       
   272                 ^ self
       
   273             ]
       
   274         ].
       
   275     ].
   267     model value:value
   276     model value:value
   268 ! !
   277 ! !
   269 
   278 
   270 !TypeConverter methodsFor:'standard converters'!
   279 !TypeConverter methodsFor:'standard converters'!
   271 
   280 
   540                 model value:value]
   549                 model value:value]
   541 
   550 
   542         updateBlock: [:m :a :p | true]
   551         updateBlock: [:m :a :p | true]
   543 
   552 
   544     "Created: 21.2.1997 / 18:58:38 / cg"
   553     "Created: 21.2.1997 / 18:58:38 / cg"
       
   554 !
       
   555 
       
   556 numberOrNilToTextMinValue:minVal maxValue:maxVal
       
   557     "setup the converter to convert from a string to a number or nil
       
   558      and vice versa, but clamping the number into the range."
       
   559 
       
   560     self
       
   561         getBlock:[:model |
       
   562                 |numericValue|
       
   563 
       
   564                 (numericValue := model value) isNil ifTrue:[
       
   565                     String new
       
   566                 ] ifFalse:[
       
   567                     numericValue printString
       
   568                 ]]
       
   569 
       
   570         putBlock:
       
   571                 [:model :string |
       
   572 
       
   573                 |value c|
       
   574 
       
   575                 (string isEmpty or:[string isBlank]) ifTrue:[
       
   576                     value := nil
       
   577                 ] ifFalse:[
       
   578                     value := Number readFromString:string onError:[nil]. 
       
   579                     value isNil ifTrue:[
       
   580                         
       
   581                     ] ifFalse:[
       
   582                         minVal notNil ifTrue:[
       
   583                             (value < minVal and: [string notEmpty]) ifTrue: [model setValue:nil].
       
   584                             value := value max:minVal.
       
   585                         ].
       
   586                         maxVal notNil ifTrue:[
       
   587                             (value > maxVal and: [string notEmpty]) ifTrue: [model setValue:nil].
       
   588                             value := value min:maxVal.
       
   589                         ].
       
   590                     ].
       
   591                 ].
       
   592                 self setNumberValue: value inModel: model fromInput: string.
       
   593                 ]
       
   594 
       
   595         updateBlock: [:m :a :p | true]
       
   596 
       
   597     "Modified: 21.2.1997 / 18:59:44 / cg"
   545 !
   598 !
   546 
   599 
   547 numberOrPointOrNil
   600 numberOrPointOrNil
   548     "setup the converter to convert from a string to a number or point
   601     "setup the converter to convert from a string to a number or point
   549      and vice versa. Invalid numbers/points are converted to nil."
   602      and vice versa. Invalid numbers/points are converted to nil."
  1057 ! !
  1110 ! !
  1058 
  1111 
  1059 !TypeConverter class methodsFor:'documentation'!
  1112 !TypeConverter class methodsFor:'documentation'!
  1060 
  1113 
  1061 version
  1114 version
  1062     ^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.35 2000-11-14 18:17:13 tm Exp $'
  1115     ^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.36 2001-03-28 17:45:00 cg Exp $'
  1063 ! !
  1116 ! !