eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'PrintConverter':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: PrintConverter


Inheritance:

   Object
   |
   +--PrintConverter

Package:
stx:libview2
Category:
Interface-Support
Version:
rev: 1.25 date: 2023/06/15 18:39:10
user: cg
file: PrintConverter.st directory: libview2
module: stx stc-classLibrary: libview2

Description:


printConverters can be used with labels and editFields to convert 
an object to/from some printed representation.

Conversion is done via two blocks, which can be set at instance
creation time - either as custom blocks or as one of the
standard conversions. 

There are a number of standard setups for common conversions; 
if none of them fits your needs, 
create a custom converter by defining its two conversion blocks.

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 to the ST/X team.

copyright

COPYRIGHT (c) 1995 by Claus Gittinger 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.

Class protocol:

instance creation
o  new
(comment from inherited method)
return an instance of myself without indexed variables

utilities
o  print: aNumber formattedBy: formatString
take the digits of aNumber's printString, and squash them
into formatString, where #-characters are replaced by
successive characters from the printString.
Warning: use with care - it does not check for decimal points etc.
the printString of aNumber must have enough digits for all
#-characters to be replaced.
Therefore, precheck the number's value and use appropriate format
strings then.
For number formatting, see also: printfPrintString: implementations.

Usage example(s):

     PrintConverter print:'123456789' formattedBy:'US$ ###,###.##' 

    fails for:
     PrintConverter print:'1234' formattedBy:'US$ ###,###.##' 

    invalid string for:
     PrintConverter print:'123456789' formattedBy:'US$ ###' 


Instance protocol:

accessing
o  type
return the type if it's one of the standard converters,
#number, #string etc. nil otherwise

converting
o  printStringFor: aValue
sent when an inputField wants a models value to be converted to a string
for display

o  readValueFrom: aString
sent when an inputField wants a string to be converted to a value
to be returned as its contents value

initialization
o  initFor: aTypeSymbol
initialize to convert to/from objects as specified by aTypeSymbol,
which may be one of #number, #string, #symbol, #date or #password ...

o  initForDate
initialize to convert to/from a date
- if the string is empty or invalid, convert to the current date

o  initForDateOrNil
initialize to convert to/from a date
- if the string is empty or invalid, convert to nil

o  initForFloat
initialize to convert to/from an float
- if the string is empty or invalid, convert to 0

o  initForFloatRoundedToPrecision: nDigits
initialize to convert to/from an float with nDigits after the decimal point
(rounding the last digit).
- if the string is empty or invalid, convert to 0

o  initForFloatWithPrecision: nDigits
initialize to convert to/from an float with nDigits after the decimal point
(truncating remaining digits).
- if the string is empty or invalid, convert to 0

o  initForInteger
initialize to convert to/from an integer
- if the string is empty or invalid, convert to 0

o  initForIntegerOrNil
initialize to convert to/from an integer
- if the string is empty or invalid, convert to nil

o  initForNumber
initialize to convert to/from a number
- if the string is empty or invalid, convert to 0

o  initForNumberOrNil
initialize to convert to/from a number
- if the string is empty or invalid, convert to nil

o  initForString
initialize for a string - this is trivial

o  initForTime
initialize to convert to/from a time
- if the string is empty or invalid, convert to the current time

o  initForTimeOrNil
initialize to convert to/from a time
- if the string is empty or invalid, convert to nil

o  initForYesNo
initialize to convert a 'yes'/'no' string to/from a boolean
The string is supposed to be in the current Language
(i.e. if german, ja/nein has to be entered.
Invalid entries are converted to false.

o  toPrint: printBlock toRead: readBlock
initialize to convert using two custom blocks.
printBlock is supposed to get the objects value as argument,
and to return a string.
readBlock is supposed to get a string as argument, and return
the corresponding object.


Examples:


stupid examples: convert date <-> string:
    |conv|

    conv := (PrintConverter new)
                toPrint:[:date | date printString]
                toRead:[:string | Date readFromString:string].
    (conv printStringFor:(Date today)) inspect.
    (conv readValueFrom:(Date today printString)) inspect
convert number <-> string:
    |conv|

    conv := (PrintConverter new) initForNumber.
    (conv printStringFor:12345) inspect.
    (conv readValueFrom:'12345') inspect
convert boolean <-> string:
    |conv|

    conv := (PrintConverter new) initForYesNo.
    (conv printStringFor:true).  
    (conv printStringFor:false).    
    (conv readValueFrom:'yes').  
    (conv readValueFrom:'no').  
    'if language is #de:'.
    (conv readValueFrom:'ja').    
    (conv readValueFrom:'nein')  
concrete examples: convert in an inputField:
    |dialog field|

    dialog := Dialog new.
    dialog addTextLabel:'a number (and only numbers):'.
    dialog addVerticalSpace.
    field := dialog addInputFieldOn:(0 asValue).
    field converter:(PrintConverter new initForNumber).
    field immediateAccept:true.
    dialog addOkButton.
    dialog open.
    dialog accepted ifTrue:[
        Transcript showCR:field editValue
    ]
convert a models value for a label:
    |top v1 v2 l1 l2|

    v1 := 0 asValue.
    v2 := Date today asValue.

    top := StandardSystemView new.
    top extent:200@200.

    l1 := Label origin:0.0@0.0 corner:1.0@0.5 in:top.
    l1 converter:(PrintConverter new initForInteger).
    l1 model:v1; labelMessage:#value; aspect:#value.
    l1 level:-1; inset:10.

    l2 := Label origin:0.0@0.5 corner:1.0@1.0 in:top.
    l2 converter:(PrintConverter new initForDate).
    l2 model:v2; labelMessage:#value; aspect:#value.
    l2 level:-1; inset:10.

    top open.

    'now, change the values ...'.
    [
      1 to:50 do:[:i|
          v1 value:(v1 value + 1).
          v2 value:(v2 value addDays:1).
          (Delay forSeconds:0.5) wait
      ]
    ] fork
convert a models value for a label with limited precision float conversion:
    |top v l1 l2|

    v := 0.0 asValue.

    top := StandardSystemView new.
    top extent:200@200.

    l1 := Label origin:0.0@0.0 corner:1.0@0.5 in:top.
    l1 converter:(PrintConverter new initForFloat).
    l1 model:v; labelMessage:#value; aspect:#value.
    l1 level:-1; inset:10.

    l2 := Label origin:0.0@0.5 corner:1.0@1.0 in:top.
    l2 converter:(PrintConverter new initForFloatWithPrecision:2).
    l2 model:v; labelMessage:#value; aspect:#value.
    l2 level:-1; inset:10.

    top open.

    'now, change the values ...'.
    [
      1 to:100 do:[:i|
          v value:(v value + 0.005).
          (Delay forSeconds:0.5) wait
      ]
    ] fork
a custom converter, converting a number to either 'odd' or 'even' strings (we only need a one-way converter for labels):
    |top v l1 l2|

    v := 0 asValue.

    top := StandardSystemView new.
    top extent:200@200.

    l1 := Label origin:0.0@0.0 corner:1.0@0.5 in:top.
    l1 converter:(PrintConverter new initForInteger).
    l1 model:v; labelMessage:#value; aspect:#value.
    l1 level:-1; inset:10.

    l2 := Label origin:0.0@0.5 corner:1.0@1.0 in:top.
    l2 converter:(PrintConverter 
                    new 
                        toPrint:[:num | num even ifTrue:['even'] 
                                                 ifFalse:['odd']]
                        toRead:[:string | ]).
    l2 model:v; labelMessage:#value; aspect:#value.
    l2 level:-1; inset:10.

    top open.

    'now, change the values ...'.
    [
      1 to:100 do:[:i|
          v value:(v value + 1).
          (Delay forSeconds:0.5) wait
      ]
    ] fork
see more examples in the EditField examples.

ST/X 7.7.0.0; WebServer 1.702 at 20f6060372b9.unknown:8081; Sat, 27 Jul 2024 02:28:46 GMT