TypeConverters?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

TypeConverters?

Howard Oh
What does the type converter aspect do for an TextEdit?
I'm hoping this topic be cover at MVP nightclass.
Well, I know I will get to understant it after reading it down.
But a short concept reminder can be helpful.

PS: I got very self-assured to compose MVP and use layouts.
It took me a year to reach here, oh man.

Hwa Jong Oh


Reply | Threaded
Open this post in threaded view
|

Re: TypeConverters?

Ian Bartholomew-3
Hwa Jong Oh,

> What does the type converter aspect do for an TextEdit?

It allows you to convert from the text format that the TextPresenter handles
to another type of object (an instance of another Smalltalk class).

Evaluate

t := TextPresenter create.
t view topView extent: 200@60.
t view show

and enter a date into the text edit in your normal format - 15/12/2000 in my
case. Inspect the following and you will see that the TextPresenter answers,
not unsurprisingly, a String.

t value

Now evaluate the following, re-enter the date, inspect the value again and
you should now get an instance of the Date subclass. The typeconverter has
automatically converted the String for you.

t view typeconverter: DateToText new

It also works the other way. Evaluate

t := TextPresenter create.
t view topView extent: 200@60.
t view show.
t value: Date today

You can see that the TextPresenter (or rather the TextEdit>>displayValue
method) will display the Date in text correctly as it always sends
#displayString to the Date before passing it on to Windows. However, this
always uses the default display format, if you want a different format you
can use the TypeConverter.

t view typeconverter: DateToText new.
t view typeconverter format: 'yyyy/MMM/d'.
t value: Date today

You can also use TypeConverters outside of MVP as an easy way to perform
some common conversions

BooleanToText new convertFromRightToLeft: 'true'.
BooleanToText new convertFromRightToLeft: 'FALSE'.
BooleanToText new convertFromLeftToRight: false

The 'left' and 'right' bits refer to the position in the Class name - e.g.
in the above 'left' refers to the Boolean and 'right' refers to the Text

It is also illuminating that note that the _only_ major difference between a
"TextPresenter.Default view" resource and, for example, a
"DatePresenter.Text view" is that the latter defines a typeconverter.

Ian