BlockValue.st
author Claus Gittinger <cg@exept.de>
Tue, 14 Jan 1997 18:02:37 +0100
changeset 365 47e0d48fd769
parent 347 2a795646e985
child 382 d64197ef1c9b
permissions -rw-r--r--
commentary

"
 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.
"


ValueModel subclass:#BlockValue
	instanceVariableNames:'cachedValue arguments block'
	classVariableNames:'NeverComputed'
	poolDictionaries:''
	category:'Interface-Support-Models'
!

!BlockValue class methodsFor:'documentation'!

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.
"

!

documentation
"
    BlockValues depend on multiple other objects (typically valueHolders)
    and recompute a value whenever one of them changes.
    If the new value is different, it triggers itself a change to its dependents.

    Example use is to base an enableChannels value on multiple other boolean values.
    (See example for how this is done)

    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
"
    checkToggle 3 shows the value of toggle1 AND toggle2
                                                                        [exBegin]
        |val1 val2 both box|

        val1 := false asValue.
        val2 := false asValue.
        both := BlockValue 
                    with:[:v1 :v2 | 
                            Transcript showCR:'evaluating ...'.
                            v1 value and:[v2 value]
                         ] 
                    arguments:(Array with:val1 with:val2).

        box := Dialog new.
        box addCheckBox:'one' on:val1.
        box addCheckBox:'two' on:val2.
        box addHorizontalLine.
        box addCheckBox:'both' on:both.
        box open
                                                                        [exEnd]

    the same, using a convenient instance creation message:
                                                                        [exBegin]
        |val1 val2 both box|

        val1 := false asValue.
        val2 := false asValue.
        both := BlockValue forLogical:val1 and:val2.

        box := Dialog new.
        box addCheckBox:'one' on:val1.
        box addCheckBox:'two' on:val2.
        box addHorizontalLine.
        box addCheckBox:'both' on:both.
        box open
                                                                        [exEnd]

    logical or:
                                                                        [exBegin]
        |val1 val2 both box|

        val1 := false asValue.
        val2 := false asValue.
        both := BlockValue forLogical:val1 or:val2.

        box := Dialog new.
        box addCheckBox:'one' on:val1.
        box addCheckBox:'two' on:val2.
        box addHorizontalLine.
        box addCheckBox:'both' on:both.
        box open
                                                                        [exEnd]

    example use: enabling an element depending on two others:
                                                                        [exBegin]
        |val1 val2 enabler val3 box|

        val1 := false asValue.
        val2 := false asValue.
        val3 := false asValue.
        enabler := BlockValue forLogical:val1 and:val2.

        box := Dialog new.
        box addCheckBox:'one' on:val1.
        box addCheckBox:'two' on:val2.
        box addHorizontalLine.
        (box addCheckBox:'three' on:val3) enableChannel:enabler.
        box open
                                                                        [exEnd]
"
! !

!BlockValue class methodsFor:'initialization'!

initialize
    NeverComputed isNil ifTrue:[
	NeverComputed := Object new.
    ]
! !

!BlockValue class methodsFor:'instance creation'!

forLogical:arg1 and:arg2
    "return a new BlockValue computing the logical AND of its args"

    ^ (super new) 
	setBlock:[:a :b | a value and:[b value]]
	arguments:(Array with:arg1 with:arg2) 

    "Created: 16.12.1995 / 19:20:14 / cg"
!

forLogical:arg1 or:arg2
    "return a new BlockValue computing the logical OR of its args"

    ^ (super new) 
	setBlock:[:a :b | a value or:[b value]]
	arguments:(Array with:arg1 with:arg2) 

    "Created: 16.12.1995 / 19:20:14 / cg"
!

with:aBlock
    "return a new BlockValue computing aBlock"

    ^ (super new) setBlock:aBlock

    "Created: 16.12.1995 / 19:16:33 / cg"
!

with:aBlock argument:anArgument
    "return a new BlockValue computing aBlock"

    ^ (super new) setBlock:aBlock arguments:(Array with:anArgument)

    "Created: 16.12.1995 / 19:20:14 / cg"
!

with:aBlock arguments:aCollectionOfArguments
    "return a new BlockValue computing aBlock"

    ^ (super new) setBlock:aBlock arguments:aCollectionOfArguments

    "Created: 16.12.1995 / 19:20:14 / cg"
! !

!BlockValue methodsFor:'accessing'!

dependOn:someObject
    arguments isNil ifTrue:[
	arguments := Array with:someObject
    ] ifFalse:[
	arguments := arguments copyWith:someObject
    ].
    someObject addDependent:self

    "Modified: 16.12.1995 / 19:18:31 / cg"
!

evaluate
    arguments isNil ifTrue:[
	^ block value
    ].
    ^ block valueWithArguments:(arguments asArray)

    "Created: 16.12.1995 / 19:27:40 / cg"
!

setBlock:aBlock
    block := aBlock.
    arguments notNil ifTrue:[
	self release
    ].
    arguments := nil.
    cachedValue := NeverComputed.

    "Created: 16.12.1995 / 19:16:59 / cg"
    "Modified: 16.12.1995 / 19:51:23 / cg"
!

setBlock:aBlock arguments:aCollectionOfArguments
    block := aBlock.
    arguments notNil ifTrue:[
	self release
    ].
    arguments := aCollectionOfArguments.
    arguments do:[:arg |
	arg addDependent:self
    ].
    cachedValue := NeverComputed.

    "Created: 16.12.1995 / 19:21:41 / cg"
    "Modified: 16.12.1995 / 19:51:28 / cg"
!

setValue:newValue 
    "physically set my value, without change notifications.
     This is a noop here, since my value is computed."

    ^ self


!

value
    cachedValue == NeverComputed ifTrue:[
	cachedValue := self evaluate
    ].
    ^ cachedValue

    "Created: 16.12.1995 / 19:23:26 / cg"
    "Modified: 17.12.1995 / 15:59:16 / cg"
! !

!BlockValue methodsFor:'change & update'!

update:something with:aParameter from:someone
    |oldValue|

    oldValue := cachedValue.
    cachedValue := self evaluate.
    oldValue ~~ cachedValue ifTrue:[
	self changed:#value
    ].

    "Created: 16.12.1995 / 19:22:54 / cg"
    "Modified: 17.12.1995 / 15:58:56 / cg"
! !

!BlockValue methodsFor:'release'!

release
    arguments notNil ifTrue:[
	arguments do:[:arg | arg removeDependent:self].
    ].

    "Modified: 16.12.1995 / 19:21:11 / cg"
! !

!BlockValue class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview2/BlockValue.st,v 1.8 1996-10-27 14:13:34 cg Exp $'
! !
BlockValue initialize!