Building the data converter
So far, you have defined a new data type, which is ready to use in your applications, provided you are familiar with the class and its methods.
To make the data type more useful in VA Smalltalk applications, you can make it available in the settings views of parts by creating a data converter class.
Because you have already created the class definition, open the Script Editor for the MyCanadianProvinceConverter class.
Adding types to settings views
First create the displayName class method, which adds your new data type to the list of data types in the converter property of the properties table. If you forget to implement this method, VA Smalltalk will omit your new type from the converter property's list of data types.
displayName
"Answer the name for the data types list."
^'Canadian Province'
Because this data converter uses a special data type, instead of a predefined one, you must implement the following class method, which associates your province data type with this converter:
objectClass
"Answer my data type"
^MyCanadianProvince
Similarly, you must implement a class method in MyCanadianProvince that associates the converter with the data type:
untitledConverterClass
"Answer my converter class"
^MyCanadianProvinceConverter
Validating input data
A data converter is responsible for converting and validating data as it is transformed from what a user types into a Smalltalk object and from a Smalltalk object into a format that can be displayed. To do the validation you implement two methods, which check the data as it moves in each direction.
The first method is acceptsAsDisplayToObjectInput:, which does basic checking of the data that a user enters. This method ensures that the object passed to it is a valid string before VA Smalltalk tries to convert it to a province object.
acceptsAsDisplayToObjectInput: anInput
"Answer true if anInput is acceptable."
^anInput isString
The second method is acceptsAsObjectToDisplayInput:. This method ensures that the object passed to it is really a province object before VA Smalltalk tries to display it.
acceptsAsObjectToDisplayInput: anInput
"Answer true if anInput is acceptable."
^(anInput isKindOf: MyCanadianProvince)
Usually, these two methods only do basic checking to see if the input is acceptable. Conversions between user input and Smalltalk objects are handled by two other methods, which you'll write next.
Converting user data
Converting the data from what the user types into a Smalltalk object is handled by the primDisplayToObject:ifError: method:
primDisplayToObject: anInput ifError: aOneArgExceptionBlock
"Answer the result of converting anInput from display
to object form. If there is an error, then answer
(aOneArgExceptionBlock value: errObj)."
| prov str |
str := anInput trimBlanks.
(prov := MyCanadianProvince fromAbbreviation: str) notNil
ifTrue: [ ^prov ].
(prov := MyCanadianProvince fromProvince: str) notNil
ifTrue: ^prov ].
^aOneArgExceptionBlock value:
(self errorObject: 'Unknown province', str ).
Notice that this method trims away extra white space from an input string before trying to convert it. Because the user can type either the full name of the province or the abbreviation, the code tries both before rejecting the input string.
If you need to do any special parsing, or if you want to change the way errors are handled, primDisplayToObject:ifError: is the best place to add your logic.
Converting a Smalltalk object into a display format is done by the primObjectToDisplay: method:
primObjectToDisplay: aProvince
"Answer the result of converting to display form."
^aProvince province.
The preceding method determines how the province data is displayed after it is validated. If you prefer to display the abbreviation rather than the full province name, modify the method to answer aProvince abbreviation instead.
Before the converter will work, it needs one more instance method. The following method tells VA Smalltalk that your data type converter can convert between display and object formats:
supportsDisplayToObjectConversion
"Answer true if this converter supports display-to-object
conversion."
^true
If you forget to implement the preceding method, VA Smalltalk will not call your conversion methods, and the data you enter in a Canadian Province field will always be rejected.
Testing your work
Now let's test everything. In a testing application, create a new visual part.
1. Add a Text part, and open its settings.
2. Select Canadian Province from the Data Type drop-down list of the converter property.
3. Add a Push Button part.
Test the part. Try entering province names, abbreviations, and invalid names. Whenever you enter an invalid name, you should see ** error ** in the field after you tab to the Push Button part. When you enter a valid abbreviation, it should be replaced with the full province name.
Last modified date: 01/29/2015