BlockValue.st
changeset 145 cb162538d41a
child 146 9603cec98ac2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BlockValue.st	Sat Dec 16 19:36:39 1995 +0100
@@ -0,0 +1,173 @@
+"
+ 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:''
+	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)
+"
+
+!
+
+examples
+"
+    |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
+"
+! !
+
+!BlockValue class methodsFor:'instance creation'!
+
+with:aBlock
+    "return a new BlockValue computing aBlock"
+
+    ^ (super new) setBlock:aBlock
+
+    "Created: 16.12.1995 / 19:16:33 / 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.
+
+    "Created: 16.12.1995 / 19:16:59 / cg"
+    "Modified: 16.12.1995 / 19:21:22 / cg"
+!
+
+setBlock:aBlock arguments:aCollectionOfArguments
+    block := aBlock.
+    arguments notNil ifTrue:[
+        self release
+    ].
+    arguments := aCollectionOfArguments.
+    arguments do:[:arg |
+        arg addDependent:self
+    ].
+
+    "Created: 16.12.1995 / 19:21:41 / cg"
+    "Modified: 16.12.1995 / 19:29:42 / cg"
+!
+
+value
+    ^ cachedValue
+
+    "Created: 16.12.1995 / 19:23:26 / cg"
+    "Modified: 16.12.1995 / 19:27:50 / cg"
+! !
+
+!BlockValue methodsFor:'change and update'!
+
+update:something with:aParameter from:someone
+    |oldValue|
+
+    oldValue := cachedValue.
+    cachedValue := self evaluate.
+    oldValue ~~ cachedValue ifTrue:[
+        self changed:something with:aParameter
+    ].
+
+    "Created: 16.12.1995 / 19:22:54 / cg"
+    "Modified: 16.12.1995 / 19:28:00 / 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.1 1995-12-16 18:36:39 cg Exp $'
+! !