TypeConverter.st
author Claus Gittinger <cg@exept.de>
Thu, 17 Apr 1997 03:31:06 +0200
changeset 540 555743a120c0
parent 508 64ee54ff8a77
child 722 ea6d08251333
permissions -rw-r--r--
checkin from browser

PluggableAdaptor subclass:#TypeConverter
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'Interface-Support-Models'
!

!TypeConverter class methodsFor:'documentation'!

documentation
"
    a typeConverter can be used as an editFields model,
    to convert the fields string value to some object.
    No real new functionality is added here - all is inherited
    from PluggableAdapter; however, some specialized instance creation
    methods are added here..

    Notice: 
        this class was implemented using protocol information
        from alpha testers - it may not be complete or compatible to
        the corresponding ST-80 class. 
        If you encounter any incompatibilities, please forward a note 
        describing the incompatibility verbal (i.e. no code) to the ST/X team.

    [author:]
        Claus Gittinger
"


!

examples 
"
    convert a number to a string:
                                                                        [exBegin]
        |v t i|

        v := 1 asValue.

        t := HorizontalPanelView new.
        t extent:200@50.
        t horizontalLayout:#fitSpace.

        i := EditField in:t.
        i model:(TypeConverter onNumberValue:v).
        t open.

        (Delay forSeconds:3) wait.
        v value:2.
                                                                        [exEnd]
    convert a date to a string:
                                                                        [exBegin]
        |d v|

        v := nil asValue.

        d := DialogBox new.
        d addInputFieldOn:(TypeConverter onDateValue:v).
        d addOkButton.
        d open.
        d accepted ifTrue:[
            Transcript showCR:v value
        ]
                                                                        [exEnd]
"

! !

!TypeConverter class methodsFor:'instance creation'!

onDateValue:aValueHolder
    "create and return a typeConverter, which retrieves
     a date via #value, and converts
     a date-string to a date via #value:.
     Useful as an editFields model, which operates on some
     date value (or aspectAdaptor, which adapts to a numeric slot)"

    ^ (self on:aValueHolder) dateToText

    "Modified: 21.2.1997 / 18:46:11 / cg"
    "Created: 4.3.1997 / 12:30:49 / cg"
!

onNumberValue:aValueHolder
    "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) numberToText

    "Modified: 21.2.1997 / 18:46:11 / cg"
!

onNumberValue:aValueHolder format:formatString
    "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) numberToTextFormattedBy:formatString

    "Created: 4.3.1997 / 11:51:25 / cg"
    "Modified: 4.3.1997 / 11:51:41 / cg"
!

onSymbolValue:aValueHolder
    "create and return a typeConverter, which retrieves
     a symbol-values string representation via #value, and converts
     a string to a symbol via #value:.
     Useful as an editFields model, which operates on some
     numeric value (or aspectAdaptor, which adapts to a numeric slot)"

    ^ (self on:aValueHolder) symbolOrNil

    "Modified: 21.2.1997 / 18:46:11 / cg"
!

onTimeValue:aValueHolder
    "create and return a typeConverter, which retrieves
     a time via #value, and converts
     a time-string to a time via #value:.
     Useful as an editFields model, which operates on some
     date value (or aspectAdaptor, which adapts to a numeric slot)"

    ^ (self on:aValueHolder) timeToText

    "Modified: 21.2.1997 / 18:46:11 / cg"
    "Created: 4.3.1997 / 11:52:10 / cg"
! !

!TypeConverter methodsFor:'accessing'!

subject
    "return the cobverted subject"

    ^ model

    "Created: 21.2.1997 / 18:45:12 / cg"
!

value:newValue
    "convert and change"

    self setValue:newValue

    "Created: 21.2.1997 / 18:45:39 / cg"
! !

!TypeConverter methodsFor:'initialize-release'!

dateOrNil
    "setup the converter to convert from a string to a date
     and vice versa."

    self
        getBlock:[:model |
                |date|

                (date := model value) isNil ifTrue:[
                    ''
                ] ifFalse:[
                    date printString
                ]]

        putBlock:
                [:model :string |

                |value|

                string isEmpty ifTrue:[
                    value := nil
                ] ifFalse:[
                    value := Date readFrom:string onError:nil
                ].
                model value:value]

        updateBlock: [:m :a :p | true]

    "Created: 4.3.1997 / 11:56:36 / cg"
    "Modified: 4.3.1997 / 12:05:52 / cg"
!

dateToText
    "setup the converter to convert from a string to a date
     and vice versa."

    self
        getBlock:[:model |
                |date|

                (date := model value) isNil ifTrue:[
                    Date today printString
                ] ifFalse:[
                    date printString
                ]]

        putBlock:
                [:model :string |

                |value|

                string isEmpty ifTrue:[
                    value := Date today
                ] ifFalse:[
                    value := Date readFrom:string onError:Date today
                ].
                model value:value]

        updateBlock: [:m :a :p | true]

    "Modified: 21.2.1997 / 18:59:06 / cg"
    "Created: 4.3.1997 / 12:32:19 / cg"
!

number
    "setup the converter to convert from a string to a number
     and vice versa. Invalid numbers are converted to nil."

    self numberToText

    "Created: 4.4.1997 / 12:46:14 / cg"
!

numberOrNil
    "setup the converter to convert from a string to a number
     and vice versa. Invalid numbers are converted to nil."

    self
        getBlock:[:model |
                |numericValue|

                (numericValue := model value) isNil ifTrue:[
                    String new
                ] ifFalse:[
                    numericValue printString
                ]]

        putBlock:
                [:model :string |

                |value|

                string isEmpty ifTrue:[
                    value := nil
                ] ifFalse:[
                    value := Number readFrom:string onError:nil
                ].
                model value:value]

        updateBlock: [:m :a :p | true]

    "Created: 21.2.1997 / 18:58:38 / cg"
!

numberToText
    "setup the converter to convert from a string to a number
     and vice versa."

    self
        getBlock:[:model |
                |numericValue|

                (numericValue := model value) isNil ifTrue:[
                    String new
                ] ifFalse:[
                    numericValue printString
                ]]

        putBlock:
                [:model :string |

                |value|

                string isEmpty ifTrue:[
                    value := 0
                ] ifFalse:[
                    value := string asNumber
                ].
                model value:value]

        updateBlock: [:m :a :p | true]

    "Created: 21.2.1997 / 18:57:05 / cg"
    "Modified: 4.3.1997 / 11:50:29 / cg"
!

numberToTextFormattedBy:formatString
    "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:[
                    PrintConverter print:numericValue formattedBy:formatString
                ]]

        putBlock:
                [:model :string |

                |value|

                string isEmpty ifTrue:[
                    value := 0
                ] ifFalse:[
                    value := string asNumber "asNumberFromFormatString:formatString"
                ].
                model value:value]

        updateBlock: [:m :a :p | true]

    "Modified: 21.2.1997 / 18:59:44 / cg"
!

symbolOrNil
    "setup the converter to convert from a string to a symbol
     and vice versa. Invalid symbols (i.e. empty) are converted to nil."

    self
        getBlock:[:model |
                |symbolValue|

                (symbolValue := model value) isNil ifTrue:[
                    String new
                ] ifFalse:[
                    '#' , symbolValue asString
                ]]

        putBlock:
                [:model :string |

                |value s|

                string isEmpty ifTrue:[
                    value := nil
                ] ifFalse:[
                    s := string withoutSeparators.
                    (s startsWith:'#') ifTrue:[
                        s := s copyFrom:2
                    ].
                    value := s asSymbol
                ].
                model value:value]

        updateBlock: [:m :a :p | true]

    "Created: 21.2.1997 / 18:58:38 / cg"
!

timeOrNil
    "setup the converter to convert from a string to a time
     and vice versa."

    self
        getBlock:[:model |
                |time|

                (time := model value) isNil ifTrue:[
                    ''
                ] ifFalse:[
                    time printString
                ]]

        putBlock:
                [:model :string |

                |value|

                string isEmpty ifTrue:[
                    value := nil
                ] ifFalse:[
                    value := Time readFrom:string onError:nil
                ].
                model value:value]

        updateBlock: [:m :a :p | true]

    "Created: 4.3.1997 / 11:52:47 / cg"
    "Modified: 4.3.1997 / 12:05:48 / cg"
!

timeToText
    "setup the converter to convert from a string to a time
     and vice versa."

    self
        getBlock:[:model |
                |time|

                (time := model value) isNil ifTrue:[
                    Time now printString
                ] ifFalse:[
                    time printString
                ]]

        putBlock:
                [:model :string |

                |value|

                string isEmpty ifTrue:[
                    value := Time now
                ] ifFalse:[
                    value := Time readFrom:string onError:Time now
                ].
                model value:value]

        updateBlock: [:m :a :p | true]

    "Modified: 21.2.1997 / 18:59:06 / cg"
    "Created: 4.3.1997 / 11:52:47 / cg"
! !

!TypeConverter class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.6 1997-04-04 10:47:42 cg Exp $'
! !