TypeConverter.st
changeset 442 2f5c72b7d46b
child 446 e4a8087792d9
equal deleted inserted replaced
441:b0d486981a62 442:2f5c72b7d46b
       
     1 PluggableAdaptor subclass:#TypeConverter
       
     2 	instanceVariableNames:''
       
     3 	classVariableNames:''
       
     4 	poolDictionaries:''
       
     5 	category:'Interface-Support-Models'
       
     6 !
       
     7 
       
     8 !TypeConverter class methodsFor:'documentation'!
       
     9 
       
    10 documentation
       
    11 "
       
    12     a typeConverter can be used as an editFields model,
       
    13     to convert the fields string value to some object.
       
    14     No real new functionality is added here - all is inherited
       
    15     from PluggableAdapter; however, some specialized instance creation
       
    16     methods are added here..
       
    17 
       
    18     Notice: 
       
    19         this class was implemented using protocol information
       
    20         from alpha testers - it may not be complete or compatible to
       
    21         the corresponding ST-80 class. 
       
    22         If you encounter any incompatibilities, please forward a note 
       
    23         describing the incompatibility verbal (i.e. no code) to the ST/X team.
       
    24 
       
    25     [author:]
       
    26         Claus Gittinger
       
    27 "
       
    28 
       
    29 
       
    30 !
       
    31 
       
    32 examples 
       
    33 "
       
    34     convert a number to a string:
       
    35                                                                         [exBegin]
       
    36         |v t i|
       
    37 
       
    38         v := 1 asValue.
       
    39 
       
    40         t := HorizontalPanelView new.
       
    41         t extent:200@50.
       
    42         t horizontalLayout:#fitSpace.
       
    43 
       
    44         i := EditField in:t.
       
    45         i model:(TypeConverter onNumberValue:v).
       
    46         t open.
       
    47 
       
    48         (Delay forSeconds:3) wait.
       
    49         v value:2.
       
    50                                                                         [exEnd]
       
    51 "
       
    52 
       
    53 ! !
       
    54 
       
    55 !TypeConverter class methodsFor:'instance creation'!
       
    56 
       
    57 onNumberValue:aValueHolder
       
    58     "create and return a typeConverter, which retrieves
       
    59      a values string representation via #value, and converts
       
    60      a number-string to a value via #value:.
       
    61      Useful as an editFields model, which operates on some
       
    62      numeric value (or aspectAdaptor, which adapts to a numeric slot)"
       
    63 
       
    64     ^ (self on:aValueHolder) numberToText
       
    65 
       
    66     "Modified: 21.2.1997 / 18:46:11 / cg"
       
    67 ! !
       
    68 
       
    69 !TypeConverter methodsFor:'accessing'!
       
    70 
       
    71 subject
       
    72     "return the cobverted subject"
       
    73 
       
    74     ^ model
       
    75 
       
    76     "Created: 21.2.1997 / 18:45:12 / cg"
       
    77 !
       
    78 
       
    79 value:newValue
       
    80     "convert and change"
       
    81 
       
    82     self setValue:newValue
       
    83 
       
    84     "Created: 21.2.1997 / 18:45:39 / cg"
       
    85 ! !
       
    86 
       
    87 !TypeConverter methodsFor:'initialize-release'!
       
    88 
       
    89 numberOrNil
       
    90     "setup the converter to convert from a string to a number
       
    91      and vice versa. Invalid numbers are converted to nil."
       
    92 
       
    93     self
       
    94         getBlock:[:model |
       
    95                 |numericValue|
       
    96 
       
    97                 (numericValue := model value) isNil ifTrue:[
       
    98                     String new
       
    99                 ] ifFalse:[
       
   100                     numericValue printString
       
   101                 ]]
       
   102 
       
   103         putBlock:
       
   104                 [:model :string |
       
   105 
       
   106                 |value|
       
   107 
       
   108                 string isEmpty ifTrue:[
       
   109                     value := nil
       
   110                 ] ifFalse:[
       
   111                     value := string asNumber
       
   112                 ].
       
   113                 model value:value]
       
   114 
       
   115         updateBlock: [:m :a :p | true]
       
   116 
       
   117     "Created: 21.2.1997 / 18:58:38 / cg"
       
   118 !
       
   119 
       
   120 numberToText
       
   121     "setup the converter to convert from a string to a number
       
   122      and vice versa, using formatString."
       
   123 
       
   124     self
       
   125         getBlock:[:model |
       
   126                 |numericValue|
       
   127 
       
   128                 (numericValue := model value) isNil ifTrue:[
       
   129                     String new
       
   130                 ] ifFalse:[
       
   131                     numericValue printString
       
   132                 ]]
       
   133 
       
   134         putBlock:
       
   135                 [:model :string |
       
   136 
       
   137                 |value|
       
   138 
       
   139                 string isEmpty ifTrue:[
       
   140                     value := 0
       
   141                 ] ifFalse:[
       
   142                     value := string asNumber
       
   143                 ].
       
   144                 model value:value]
       
   145 
       
   146         updateBlock: [:m :a :p | true]
       
   147 
       
   148     "Created: 21.2.1997 / 18:57:05 / cg"
       
   149     "Modified: 21.2.1997 / 18:59:06 / cg"
       
   150 !
       
   151 
       
   152 numberToTextFormattedBy:formatString
       
   153     "setup the converter to convert from a string to a number
       
   154      and vice versa, using formatString.
       
   155      The formatString is currently ignored when numbers are converted
       
   156      from a string."
       
   157 
       
   158     self
       
   159         getBlock:[:model |
       
   160                 |numericValue|
       
   161 
       
   162                 (numericValue := model value) isNil ifTrue:[
       
   163                     String new
       
   164                 ] ifFalse:[
       
   165                     PrintConverter print:numericValue formattedBy:formatString
       
   166                 ]]
       
   167 
       
   168         putBlock:
       
   169                 [:model :string |
       
   170 
       
   171                 |value|
       
   172 
       
   173                 string isEmpty ifTrue:[
       
   174                     value := 0
       
   175                 ] ifFalse:[
       
   176                     value := string asNumber "asNumberFromFormatString:formatString"
       
   177                 ].
       
   178                 model value:value]
       
   179 
       
   180         updateBlock: [:m :a :p | true]
       
   181 
       
   182     "Modified: 21.2.1997 / 18:59:44 / cg"
       
   183 ! !
       
   184 
       
   185 !TypeConverter class methodsFor:'documentation'!
       
   186 
       
   187 version
       
   188     ^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.1 1997-02-21 18:09:11 cg Exp $'
       
   189 ! !