|
Class: TypeConverter
Object
|
+--Model
|
+--ValueModel
|
+--PluggableAdaptor
|
+--TypeConverter
- Package:
- stx:libview2
- Category:
- Interface-Support-Models
- Version:
- rev:
1.101
date: 2024/04/22 17:45:18
- user: stefan
- file: TypeConverter.st directory: libview2
- module: stx stc-classLibrary: libview2
a typeConverter can be used as an editField's model
(remember, that an editField expects a string in its model),
to convert the field's string value to some object and vice versa.
Its main use is when building interfaces with inputFields,
where the datum to be entered is a non-string and you
want to have the entered value be converted automatically.
(and vice versa, to convert the datum to a string).
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.
copyrightCOPYRIGHT (c) 1997 eXept Software AG
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
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 editField's model, which operates on some
date value (or aspectAdaptor, which adapts to a numeric slot)
-
onNumberValue: aValueHolder
-
create and return a typeConverter, which retrieves
a value's string representation via #value, and converts
a number-string to a value via #value:.
Useful as an editField's model, which operates on some
numeric value (or aspectAdaptor, which adapts to a numeric slot)
-
onNumberValue: aValueHolder format: formatString
-
create and return a typeConverter, which retrieves
a value's string representation via #value, and converts
a number-string to a value via #value:.
Useful as an editField's model, which operates on some
numeric value (or aspectAdaptor, which adapts to a numeric slot)
-
onNumberValue: aValueHolder leftPaddedTo: aSize with: aCharacter
-
create and return a typeConverter, which retrieves
a value's string representation via #value, and converts
a number-string to a value via #value:.
Useful as an editField's model, which operates on some
numeric value (or aspectAdaptor, which adapts to a numeric slot)
Usage example(s):
|m|
m := 5 asValue.
((EditField model:(TypeConverter onNumberValue:m leftPaddedTo:2 with:$-)) width:100) openAt:(200@200).
m inspect.
|
-
onNumberValue: aValueHolder minValue: min maxValue: max
-
create and return a typeConverter, which retrieves
a value's string representation via #value, and converts
a number-string to a value via #value:.
Useful as an editField's model, which operates on some
numeric value (or aspectAdaptor, which adapts to a numeric slot)
-
onNumberValue: aValueHolder postDecimalDigits: numPostDecimalDigits
-
create and return a typeConverter, which retrieves
a value's string representation via #value, and converts
a number-string to a value via #value:.
Useful as an editField's model, which operates on some
numeric value (or aspectAdaptor, which adapts to a numeric slot)
-
onNumberValue: aValueHolder printfFormat: formatString
-
create and return a typeConverter, which retrieves
a value's string representation via printf, and converts
a number-string by reading it as a number.
Useful as an editField's model, which operates on some
numeric value (or aspectAdaptor, which adapts to a numeric slot),
and you want to control the number of digits
-
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 editField's model, which operates on some
numeric value (or aspectAdaptor, which adapts to a numeric slot)
-
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 editField's model, which operates on some
date value (or aspectAdaptor, which adapts to a numeric slot)
queries
-
possiblePrintConverterTypes
-
presented in the typeConverter UI-builder menu
accessing
-
subject
-
return the subject to be converted
-
subject: newSubject
-
set the subject to be converted
-
value: newValue
-
convert and change
input testing
-
setNumberValue: value inModel: model fromInput: string
-
for invalid numbers, the model is set to nil.
By this, the dependents are forced to update their contents
(that is: a bad input string will lead to a cleared input field here).
standard converters-date & time
-
convertOldVWDateFormatSpecifierToNewSTXFormat: oldFormatSpec
-
private helper to convert an old (VW) format-Array
of the form:
'mm/dd/yyyy' (VW-backward compatibility)
into:
'%m %d %y' (new ST/X format)
-
date
-
setup the converter to convert from a string to a date
and vice versa. Nil is converted to todays date-string,
likewise, an empty string is converted back to todays date.
-
dateDDMMYYYY
-
setup the converter to convert from a string to a date formatted by printFormat
DD-MM-YYYY; see also Date>>printFormat:
This is a backward compatibility method for very old code to print european-style dates.
Please use dateWithFormat: which is much more flexible.
Usage example(s):
|vh|
vh := Date today asValue.
TypeConverter new
dateDDMMYYYY
model:vh;
value.
vh value
|
Usage example(s):
|vh|
vh := Date today asValue.
TypeConverter new
dateDDMMYYYY
model:vh;
value:'12-01-2008'.
vh value
|
-
dateMMDDYYYY
-
setup the converter to convert from a string to a date formatted by printFormat
MM/DD/YYYY; see also Date>>printFormat:
This is a backward compatibility method for very old code to print us-style dates.
Please use dateWithFormat: which is much more flexible.
Usage example(s):
|vh|
vh := Date today asValue.
TypeConverter new
dateMMDDYYYY
model:vh;
value.
vh value
|
Usage example(s):
|vh|
vh := Date today asValue.
TypeConverter new
dateMMDDYYYY
model:vh;
value:'01/12/2008'.
vh value
|
-
dateOrNil
-
setup the converter to convert from a string to a date
and vice versa. Invalid dates are converted to nil; likewise,
a nil date is converted to an empty string.
-
dateOrNilWithFormat: aFormatString
-
setup the converter to convert from a string to a date
and vice versa. Invalid dates are converted to nil; likewise,
a nil date is converted to an empty string.
The format string must have one of the forms:
'mm/dd/yyyy' (for VW-backward compatibility)
or:
'%m %d %y'
-
dateToText
-
setup the converter to convert from a string to a date
and vice versa. Nil is converted to todays date-string,
likewise, an empty string is converted back to todays date.
-
dateToTextFormattedBy: printFormat
-
setup the converter to convert from a string to a date formatted by printFormat
and vice versa. Nil is converted to todays date-string,
likewise, an empty string is converted back to todays date.
Uses the old, obsolete ST80 printFormat (see Date printFormat:)
-
dateWithFormat: aFormatString
-
setup the converter to convert from a string to a date
and vice versa. Invalid dates are converted to nil; likewise,
a nil date is converted to an empty string.
The format string must have one of the forms:
'mm/dd/yyyy' (for VW-backward compatibility)
or:
'%m %d %y'
see Date addPrintBindingsTo:language: for a format description.
Usage example(s):
|vh|
vh := Date today asValue.
(TypeConverter new
dateWithFormat:'%m%d%y')
model:vh;
value.
vh value
|
-
dateWithFormat: aFormatString orDefault: defaultValue
-
setup the converter to convert from a string to a date and vice versa.
Invalid dates are converted to nil;
likewise, a nil date is converted to an empty string.
The format string must have one of the forms:
'mm/dd/yyyy' (for VW-backward compatibility)
or:
'%m %d %y'
see Date addPrintBindingsTo:language: for a format description.
-
dateWithFormat: aFormatString orDefault: defaultValue language: lang
-
setup the converter to convert from a string to a date and vice versa.
Invalid dates are converted to nil;
likewise, a nil date is converted to an empty string.
The format string must have one of the forms:
'mm/dd/yyyy' (for VW-backward compatibility)
or:
'%m %d %y'
see Date addPrintBindingsTo:language: for a format description.
-
dateYYYYMMDD
-
setup the converter to convert from a string to a date formatted by printFormat
MM/DD/YYYY; see also Date>>printFormat:
This is a backward compatibility method for very old code to print us-style dates.
Please use dateWithFormat: which is much more flexible.
Usage example(s):
|vh|
vh := Date today asValue.
TypeConverter new
dateYYYYMMDD
model:vh;
value.
vh value
|
Usage example(s):
|vh|
vh := Date today asValue.
TypeConverter new
dateYYYYMMDD
model:vh;
value:'2008-11-30'.
vh value
|
-
monthAndYear
-
setup the converter to convert from a string to a date
and vice versa. Invalid dates are converted to nil; likewise,
a nil date is converted to an empty string.
see Date addPrintBindingsTo:language: for a format description.
Usage example(s):
|vh|
vh := Date today asValue.
(TypeConverter new monthAndYear)
model:vh;
value.
vh value
|
-
printfWithFormat: aFormatString
-
setup the converter to convert from a string to a value by reading it as number
and generates a string via printf.
Typically used with numbers.
Invalid numbers are converted to nil;
likewise, a nil value is converted to an empty string.
-
printfWithFormat: aFormatString orDefault: defaultValue
-
setup the converter to convert from a string to a value by reading it as number
and generates a string via printf.
Invalid numbers are converted to defaultValue;
likewise, a nil value is converted to an empty string.
-
time
-
-
time12H
-
setup the converter to convert from a string to a time formatted by 12 hours
and vice versa.
-
time24H
-
setup the converter to convert from a string to a time formatted by 24 hours
and vice versa.
-
timeDuration
-
setup the converter to convert from a string to a timeduration and vice versa.
-
timeDurationOrNil
-
setup the converter to convert from a string to a timeDuration and vice versa.
-
timeOfClass: timeClass withFormat: aFormatString orDefault: defaultValue
-
setup the converter to convert from a string to a time and vice versa.
TimeClass is the class that should be used (e.g. Time or Timestamp).
Invalid times are converted to defaultValue; likewise,
a nil time is converted to an empty string.
-
timeOfClass: timeClass withFormat: aFormatString orDefault: defaultValue language: langOrNil
-
setup the converter to convert from a string to a time
and vice versa. TimeClass is the class that should be used (e.g. Time or Timestamp).
Invalid times are converted to defaultValue; likewise,
a nil time is converted to an empty string.
-
timeOrNil
-
setup the converter to convert from a string to a time and vice versa.
-
timeStampOrNil
-
setup the converter to convert from a string to a timeStamp and vice versa.
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
timeToText
-
setup the converter to convert from a string to a time
and vice versa.
-
timeToTextFormattedBy: format
-
setup the converter to convert from a string to a time formatted by format
and vice versa.
-
timestamp
-
setup the converter to convert from a string to a timestamp and vice versa.
-
timestampOrNil
-
setup the converter to convert from a string to a timestamp and vice versa.
-
year
-
setup the converter to convert from a string to a date
and vice versa. Invalid dates are converted to nil; likewise,
a nil date is converted to an empty string.
see Date addPrintBindingsTo:language: for a format description.
Usage example(s):
|vh|
vh := Date today asValue.
(TypeConverter new year)
model:vh;
value.
vh value
|
standard converters-misc
-
arrayLiteralOrStringOrSymbolOrNil
-
setup the converter to convert from a string to either a literal
Array, a String or a symbol and vice versa.
Invalid strings (i.e. empty) are converted to nil;
nil values are converted to an empty string.
This is a very special converter (for the GUI builder)
- probably not belonging to here
-
arrayLiteralOrSymbolOrNil
-
setup the converter to convert from a string to either a literal
Array or a symbol and vice versa.
Invalid strings (i.e. empty) are converted to nil;
nil values are converted to an empty string.
This is a very special converter (for the GUI builder)
- probably not belonging to here
-
fileSize
-
setup the converter to convert from a fileSize string to a number
and vice versa.
A fileSize can have a scale of 1000 (k or kB), 1024 (ki or kiB),
1000*1000 (m or mB), 1024*1024 (mi or miB), 1000*1000*1000 (g or gB), 1024*1024*1024 (gi or giB).
-
frequency
-
setup the converter to convert from a frequency string to a number
and vice versa.
A frequency can have a scale of 1000 (k or kHz),
1000*1000 (m or MHz), 1000*1000*1000 (g or GHz).
-
literal
-
setup the converter to convert from a string to a literal
and vice versa. Invalid symbols (i.e. empty) are converted to nil;
nil values are converted to an empty string.
true, false, Numbers are parsed
-
objectOrNilOfType: timeOrDateClass
-
common code for timeOrNil, dateOrNil and timeStampOrNil
-
smalltalkCorner
-
setup the converter to convert from a string to a point object
and vice versa. The string used is the point's storeString.
Invalid strings (i.e. empty) are converted to nil.
-
smalltalkExtent
-
setup the converter to convert from a string to a point object
and vice versa. The string used is the point's storeString.
Invalid strings (i.e. empty) are converted to nil.
-
smalltalkObject
-
setup the converter to convert from a string to any smalltalk object
and vice versa. The string used is the objects storeString.
Invalid strings (i.e. empty) are converted to nil.
-
smalltalkObjectOrNil
-
setup the converter to convert from a string to any smalltalk object
and vice versa. The string used is the objects storeString.
Invalid strings (i.e. empty) are converted to nil.
-
smalltalkObjectOrNilWithClass: classOfObject
-
setup the converter to convert from a string to a smalltalk object
and vice versa. The string used is the objects storeString.
Invalid strings (i.e. empty) are converted to nil.
This is a very special converter (for the GUI builder)
- probably not belonging to here
-
smalltalkObjectOrNilWithClass: classOfObject comment: commentString
-
setup the converter to convert from a string to a smalltalk object
and vice versa. The string used is the objects storeString.
Invalid strings (i.e. empty) are converted to nil.
This is a very special converter (for the GUI builder)
- probably not belonging to here
-
smalltalkObjectWithClass: classOfObject
-
setup the converter to convert from a string to a smalltalk object
and vice versa. The string used is the objects storeString.
Invalid strings (i.e. empty) are converted to nil.
This is a very special converter (for the GUI builder and ImageEditor)
-
smalltalkObjectWithClass: classOfObject comment: commentString
-
setup the converter to convert from a string to a smalltalk object
and vice versa. The string used is the objects storeString.
Invalid strings (i.e. empty) are converted to nil.
This is a very special converter (for the GUI builder and ImageEditor)
-
smalltalkOrigin
-
setup the converter to convert from a string to a point object
and vice versa. The string used is the point's storeString.
Invalid strings (i.e. empty) are converted to nil.
-
smalltalkPoint
-
setup the converter to convert from a string to a point object
and vice versa. The string used is the points storeString.
Invalid strings (i.e. empty) are converted to nil.
This is a very special converter (for the GUI builder)
- probably not belonging to here
-
symbolOrBooleanOrNil
-
setup the converter to convert from a string to a symbol or boolean
and vice versa. Invalid symbols (i.e. empty) are converted to nil;
nil values are converted to an empty string.
In addition, the strings true/false convert to/from booleans.
-
symbolOrNil
-
setup the converter to convert from a string to a symbol
and vice versa. Invalid symbols (i.e. empty) are converted to nil;
nil values are converted to an empty string.
-
symbolOrNumberOrNil
-
setup the converter to convert from a string to a symbol or number
and vice versa. Invalid symbols (i.e. empty) are converted to nil;
nil values are converted to an empty string.
standard converters-numbers
-
hexadecimal
-
setup the converter to convert from a hexadecimal string to a number
and vice versa.
-
integer
-
setup the converter to convert from a string to an integer
and vice versa. Invalid integers are converted to nil.
-
integerCStyle
-
setup the converter to convert from a string to an integer
and vice versa. Supports CStyle 0x/0b and 0 prefixes
Invalid integers are converted to nil.
-
integerOrNil
-
setup the converter to convert from a string to an integer
and vice versa. Invalid numbers are converted to nil.
-
integerOrNilToTextMinValue: minVal maxValue: maxVal format: formatStringOrNil
-
setup the converter to convert from a string to an integer or nil
and vice versa, but clamping the integer into the range.
-
integerToHexTextMinValue: minVal maxValue: maxVal format: formatStringOrNil
-
setup the converter to convert from a string to a number
and vice versa, but clamping the number into the range.
-
integerToText
-
setup the converter to convert from a string to a integer
and vice versa.
-
integerToTextCStyle
-
setup the converter to convert from a string to a integer
and vice versa. SUpports C-style integers in the form 0x, 0b 0
and also Smalltalk radix integers
-
integerToTextMinValue: minVal maxValue: maxVal format: formatStringOrNil
-
setup the converter to convert from a string to a number
and vice versa, but clamping the number into the range.
-
integerToTextMinValue: minVal maxValue: maxVal radix: base format: formatStringOrNil
-
setup the converter to convert from a string to a number
and vice versa, but clamping the number into the range.
-
integerToTextStrict: strict
-
setup the converter to convert from a string to a integer
and vice versa.
If strict, no other characters are allowed after the digits
-
integerWithThousandsSeparator
-
-
integerWithThousandsSeparator: sep
-
setup the converter to convert from a string to a number with thousands separator
and vice versa.
-
number
-
setup the converter to convert from a string to a number
and vice versa. Invalid numbers are converted to nil.
-
numberOrNil
-
setup the converter to convert from a string to a number
and vice versa. Invalid numbers are converted to nil.
-
numberOrNilToTextMinValue: minVal maxValue: maxVal
-
setup the converter to convert from a string to a number or nil
and vice versa, but clamping the number into the range.
-
numberOrNilToTextMinValue: minVal maxValue: maxVal format: formatStringOrNil
-
setup the converter to convert from a string to a number or nil
and vice versa, but clamping the number into the range.
-
numberOrPointOrNil
-
setup the converter to convert from a string to a number or point
and vice versa. Invalid numbers/points are converted to nil.
-
numberOrStringOrSymbolOrNil
-
setup the converter to convert from a string to either a numeric literal
or a symbol and vice versa.
Invalid strings (i.e. empty) are converted to nil;
nil values are converted to an empty string.
This is a very special converter (for the GUI builder)
- probably not belonging to here
-
numberOrSymbolOrNil
-
setup the converter to convert from a string to either a numeric literal
or a symbol and vice versa.
Invalid strings (i.e. empty) are converted to nil;
nil values are converted to an empty string.
This is a very special converter (for the GUI builder)
- probably not belonging to here
-
numberToText
-
setup the converter to convert from a string to a number
and vice versa.
-
numberToText: numberOfPostDecimals
-
setup the converter to convert from a string to a number
and vice versa.
-
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.
-
numberToTextLeftPaddedTo: aSize with: aCharacter
-
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.
-
numberToTextMinValue: minVal maxValue: maxVal
-
setup the converter to convert from a string to a number
and vice versa, but clamping the number into the range.
-
numberToTextMinValue: minVal maxValue: maxVal format: formatStringOrNil
-
setup the converter to convert from a string to a number
and vice versa, but clamping the number into the range.
-
numberWithOptionalScale
-
setup the converter to convert from a string to a number
and vice versa. Invalid numbers are converted to nil.
The number may be followed by one of m (for million) or
k (for thousand). E.g.: '1m2k55'
-
numberWithOptionalScales: scaleDict
-
setup the converter to convert from a string to a number or nil
and vice versa; allow for scale characters (such as k for thousand).
E.g. '1m2k55'
-
numberWithOptionalScales: scaleDict andThousandsSeparator: sep
-
setup the converter to convert from a string to a number with thousands separator
and vice versa. Allow for scale characters (such as k for thousand)
-
numberWithThousandsSeparator
-
^ self numberWithThousandsSeparator:$'
-
numberWithThousandsSeparator: sep
-
setup the converter to convert from a string to a number with thousands separator
and vice versa.
testing
-
isTypeConverter
-
convert a number to a string:
|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.
| convert a number to a string with range:
|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 minValue:0 maxValue:100).
t open.
(Delay forSeconds:3) wait.
v value:2.
| convert a number to a string with printf:
|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 printfFormat:'%5.3f').
t open.
(Delay forSeconds:3) wait.
v value:2.
| convert a date to a string:
|d v|
v := nil asValue.
d := DialogBox new.
d addInputFieldOn:(TypeConverter onDateValue:v).
d addOkButton.
d open.
d accepted ifTrue:[
Transcript showCR:v value
]
| convert with thousands:
|d v|
v := 1234567 asValue.
d := DialogBox new.
d addInputFieldOn:((TypeConverter on:v) integerWithThousandsSeparator:$').
d addOkButton.
d open.
d accepted ifTrue:[
Transcript showCR:v value
]
| convert with scale character:
|d v|
v := 1234567 asValue.
d := DialogBox new.
d addInputFieldOn:((TypeConverter on:v) numberWithOptionalScale).
d addOkButton.
d open.
d accepted ifTrue:[
Transcript showCR:v value
]
| convert with scale character and thousands separator:
|d v scaleDict|
scaleDict := Dictionary new.
scaleDict at:$t put:1000.
scaleDict at:$k put:1000.
scaleDict at:$m put:1000000.
v := 1234567 asValue.
d := DialogBox new.
d addInputFieldOn:((TypeConverter on:v)
numberWithOptionalScales:scaleDict
andThousandsSeparator:$').
d addOkButton.
d open.
d accepted ifTrue:[
Transcript showCR:v value
]
|
|