TypeConverter.st
changeset 487 c7465591e41a
parent 486 f21ed8fd7df3
child 508 64ee54ff8a77
equal deleted inserted replaced
486:f21ed8fd7df3 487:c7465591e41a
    46         t open.
    46         t open.
    47 
    47 
    48         (Delay forSeconds:3) wait.
    48         (Delay forSeconds:3) wait.
    49         v value:2.
    49         v value:2.
    50                                                                         [exEnd]
    50                                                                         [exEnd]
       
    51     convert a date to a string:
       
    52                                                                         [exBegin]
       
    53         |d v|
       
    54 
       
    55         v := nil asValue.
       
    56 
       
    57         d := DialogBox new.
       
    58         d addInputFieldOn:(TypeConverter onDateValue:v).
       
    59         d addOkButton.
       
    60         d open.
       
    61         d accepted ifTrue:[
       
    62             Transcript showCR:v value
       
    63         ]
       
    64                                                                         [exEnd]
    51 "
    65 "
    52 
    66 
    53 ! !
    67 ! !
    54 
    68 
    55 !TypeConverter class methodsFor:'instance creation'!
    69 !TypeConverter class methodsFor:'instance creation'!
    77     ^ (self on:aValueHolder) numberToText
    91     ^ (self on:aValueHolder) numberToText
    78 
    92 
    79     "Modified: 21.2.1997 / 18:46:11 / cg"
    93     "Modified: 21.2.1997 / 18:46:11 / cg"
    80 !
    94 !
    81 
    95 
       
    96 onNumberValue:aValueHolder format:formatString
       
    97     "create and return a typeConverter, which retrieves
       
    98      a values string representation via #value, and converts
       
    99      a number-string to a value via #value:.
       
   100      Useful as an editFields model, which operates on some
       
   101      numeric value (or aspectAdaptor, which adapts to a numeric slot)"
       
   102 
       
   103     ^ (self on:aValueHolder) numberToTextFormattedBy:formatString
       
   104 
       
   105     "Created: 4.3.1997 / 11:51:25 / cg"
       
   106     "Modified: 4.3.1997 / 11:51:41 / cg"
       
   107 !
       
   108 
    82 onSymbolValue:aValueHolder
   109 onSymbolValue:aValueHolder
    83     "create and return a typeConverter, which retrieves
   110     "create and return a typeConverter, which retrieves
    84      a symbol-values string representation via #value, and converts
   111      a symbol-values string representation via #value, and converts
    85      a string to a symbol via #value:.
   112      a string to a symbol via #value:.
    86      Useful as an editFields model, which operates on some
   113      Useful as an editFields model, which operates on some
    87      numeric value (or aspectAdaptor, which adapts to a numeric slot)"
   114      numeric value (or aspectAdaptor, which adapts to a numeric slot)"
    88 
   115 
    89     ^ (self on:aValueHolder) symbolOrNil
   116     ^ (self on:aValueHolder) symbolOrNil
    90 
   117 
    91     "Modified: 21.2.1997 / 18:46:11 / cg"
   118     "Modified: 21.2.1997 / 18:46:11 / cg"
       
   119 !
       
   120 
       
   121 onTimeValue:aValueHolder
       
   122     "create and return a typeConverter, which retrieves
       
   123      a time via #value, and converts
       
   124      a time-string to a time via #value:.
       
   125      Useful as an editFields model, which operates on some
       
   126      date value (or aspectAdaptor, which adapts to a numeric slot)"
       
   127 
       
   128     ^ (self on:aValueHolder) timeToText
       
   129 
       
   130     "Modified: 21.2.1997 / 18:46:11 / cg"
       
   131     "Created: 4.3.1997 / 11:52:10 / cg"
    92 ! !
   132 ! !
    93 
   133 
    94 !TypeConverter methodsFor:'accessing'!
   134 !TypeConverter methodsFor:'accessing'!
    95 
   135 
    96 subject
   136 subject
   108 
   148 
   109     "Created: 21.2.1997 / 18:45:39 / cg"
   149     "Created: 21.2.1997 / 18:45:39 / cg"
   110 ! !
   150 ! !
   111 
   151 
   112 !TypeConverter methodsFor:'initialize-release'!
   152 !TypeConverter methodsFor:'initialize-release'!
       
   153 
       
   154 dateOrNil
       
   155     "setup the converter to convert from a string to a date
       
   156      and vice versa."
       
   157 
       
   158     self
       
   159         getBlock:[:model |
       
   160                 |date|
       
   161 
       
   162                 (date := model value) isNil ifTrue:[
       
   163                     ''
       
   164                 ] ifFalse:[
       
   165                     date printString
       
   166                 ]]
       
   167 
       
   168         putBlock:
       
   169                 [:model :string |
       
   170 
       
   171                 |value|
       
   172 
       
   173                 string isEmpty ifTrue:[
       
   174                     value := nil
       
   175                 ] ifFalse:[
       
   176                     value := Date readFrom:string onError:nil
       
   177                 ].
       
   178                 model value:value]
       
   179 
       
   180         updateBlock: [:m :a :p | true]
       
   181 
       
   182     "Created: 4.3.1997 / 11:56:36 / cg"
       
   183     "Modified: 4.3.1997 / 12:05:52 / cg"
       
   184 !
   113 
   185 
   114 dateToText
   186 dateToText
   115     "setup the converter to convert from a string to a date
   187     "setup the converter to convert from a string to a date
   116      and vice versa."
   188      and vice versa."
   117 
   189 
   174     "Created: 21.2.1997 / 18:58:38 / cg"
   246     "Created: 21.2.1997 / 18:58:38 / cg"
   175 !
   247 !
   176 
   248 
   177 numberToText
   249 numberToText
   178     "setup the converter to convert from a string to a number
   250     "setup the converter to convert from a string to a number
   179      and vice versa, using formatString."
   251      and vice versa."
   180 
   252 
   181     self
   253     self
   182         getBlock:[:model |
   254         getBlock:[:model |
   183                 |numericValue|
   255                 |numericValue|
   184 
   256 
   201                 model value:value]
   273                 model value:value]
   202 
   274 
   203         updateBlock: [:m :a :p | true]
   275         updateBlock: [:m :a :p | true]
   204 
   276 
   205     "Created: 21.2.1997 / 18:57:05 / cg"
   277     "Created: 21.2.1997 / 18:57:05 / cg"
   206     "Modified: 21.2.1997 / 18:59:06 / cg"
   278     "Modified: 4.3.1997 / 11:50:29 / cg"
   207 !
   279 !
   208 
   280 
   209 numberToTextFormattedBy:formatString
   281 numberToTextFormattedBy:formatString
   210     "setup the converter to convert from a string to a number
   282     "setup the converter to convert from a string to a number
   211      and vice versa, using formatString.
   283      and vice versa, using formatString.
   270                 model value:value]
   342                 model value:value]
   271 
   343 
   272         updateBlock: [:m :a :p | true]
   344         updateBlock: [:m :a :p | true]
   273 
   345 
   274     "Created: 21.2.1997 / 18:58:38 / cg"
   346     "Created: 21.2.1997 / 18:58:38 / cg"
       
   347 !
       
   348 
       
   349 timeOrNil
       
   350     "setup the converter to convert from a string to a time
       
   351      and vice versa."
       
   352 
       
   353     self
       
   354         getBlock:[:model |
       
   355                 |time|
       
   356 
       
   357                 (time := model value) isNil ifTrue:[
       
   358                     ''
       
   359                 ] ifFalse:[
       
   360                     time printString
       
   361                 ]]
       
   362 
       
   363         putBlock:
       
   364                 [:model :string |
       
   365 
       
   366                 |value|
       
   367 
       
   368                 string isEmpty ifTrue:[
       
   369                     value := nil
       
   370                 ] ifFalse:[
       
   371                     value := Time readFrom:string onError:nil
       
   372                 ].
       
   373                 model value:value]
       
   374 
       
   375         updateBlock: [:m :a :p | true]
       
   376 
       
   377     "Created: 4.3.1997 / 11:52:47 / cg"
       
   378     "Modified: 4.3.1997 / 12:05:48 / cg"
       
   379 !
       
   380 
       
   381 timeToText
       
   382     "setup the converter to convert from a string to a time
       
   383      and vice versa."
       
   384 
       
   385     self
       
   386         getBlock:[:model |
       
   387                 |time|
       
   388 
       
   389                 (time := model value) isNil ifTrue:[
       
   390                     Time now printString
       
   391                 ] ifFalse:[
       
   392                     time printString
       
   393                 ]]
       
   394 
       
   395         putBlock:
       
   396                 [:model :string |
       
   397 
       
   398                 |value|
       
   399 
       
   400                 string isEmpty ifTrue:[
       
   401                     value := Time now
       
   402                 ] ifFalse:[
       
   403                     value := Time readFrom:string onError:Time now
       
   404                 ].
       
   405                 model value:value]
       
   406 
       
   407         updateBlock: [:m :a :p | true]
       
   408 
       
   409     "Modified: 21.2.1997 / 18:59:06 / cg"
       
   410     "Created: 4.3.1997 / 11:52:47 / cg"
   275 ! !
   411 ! !
   276 
   412 
   277 !TypeConverter class methodsFor:'documentation'!
   413 !TypeConverter class methodsFor:'documentation'!
   278 
   414 
   279 version
   415 version
   280     ^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.4 1997-03-04 11:33:55 cg Exp $'
   416     ^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.5 1997-03-04 19:17:56 cg Exp $'
   281 ! !
   417 ! !