Model.st
author Claus Gittinger <cg@exept.de>
Mon, 22 Apr 1996 13:02:23 +0200
changeset 191 cb2815b77100
parent 190 f8de925054bf
child 223 b65dc250db8d
permissions -rw-r--r--
commentary

"
 COPYRIGHT (c) 1992 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.
"

Object subclass:#Model
	instanceVariableNames:'dependents'
	classVariableNames:''
	poolDictionaries:''
	category:'Interface-Support-Models'
!

!Model class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1992 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
"
    Models are things that are presented in views. Instances keep track of 
    which views are dependent of them and inform them of any changes.
    Actually the Model class is not really needed; since the dependency
    mechanism is inherited by Object, you can take any object as a model.
    However, instances of Model (and subclasses) keep the dependents locally
    in an instance variable; thus speeding up access a bit.

    Instance variables:
	dependents      Collection      those objects which depend on me.
					To save some storage, the dependent is
					kept directly here, IFF there os only one.
					Otherwise, a collection of dependents is
					held here.
"
! !

!Model methodsFor:'copying'!

postCopy
    "release dependents after copying"

    self dependents:nil
! !

!Model methodsFor:'dependents access'!

addDependent:anObject
    "make the argument, anObject be a dependent of the receiver"

    [
        |deps|

        deps := dependents.
        "/
        "/ store the very first dependent directly in
        "/ the dependents instVar
        "/
        (deps isNil and:[anObject isCollection not]) ifTrue:[
            dependents := anObject
        ] ifFalse:[
            "/
            "/ store more dependents in the dependents collection
            "/
            deps isCollection ifTrue:[
                deps add:anObject
            ] ifFalse:[
                dependents := IdentitySet with:dependents with:anObject
            ]
        ]
    ] valueUninterruptably

    "Modified: 19.4.1996 / 12:24:46 / cg"
!

dependents
    "return a Collection of dependents - nil if there is none"

    dependents isNil ifTrue:[^ nil].
    dependents isCollection ifTrue:[
        ^ dependents
    ].
    ^ IdentitySet with:dependents

    "Modified: 19.4.1996 / 12:22:04 / cg"
!

dependents:aCollection
    "set the collection of dependents"

    |dep|

    aCollection size == 1 ifTrue:[
        dep := aCollection first.
        dep isCollection ifFalse:[
            dependents := aCollection first.
            ^ self
        ]
    ].
    dependents := aCollection

    "Modified: 19.4.1996 / 12:23:05 / cg"
!

dependentsDo:aBlock
    "evaluate aBlock for all of my dependents"

    |deps|

    deps := dependents.
    deps notNil ifTrue:[
	deps isCollection ifTrue:[
	    deps do:aBlock
	] ifFalse:[
	    aBlock value:deps
	]
    ]
!

release
    "remove all dependencies from the receiver"

    dependents := nil

    "Modified: 19.4.1996 / 10:31:35 / cg"
!

removeDependent:anObject
    "make the argument, anObject be independent of the receiver"

    [
        |deps sz dep|

        deps := dependents.
        deps notNil ifTrue:[
            deps isCollection ifTrue:[
                deps remove:anObject ifAbsent:[].
                (sz := deps size) == 0 ifTrue:[
                    dependents := nil
                ] ifFalse:[
                    sz == 1 ifTrue:[
                        (dep := deps first) isCollection ifFalse:[
                            dependents := dep
                        ]
                    ]
                ]
            ] ifFalse:[
                dependents := nil
            ]
        ]
    ] valueUninterruptably

    "Modified: 19.4.1996 / 12:24:24 / cg"
! !

!Model methodsFor:'dependents access (non weak)'!

addNonWeakDependent:anObject
    "make the argument, anObject be a dependent of the receiver.
     Since all dependencies are nonWeak in Model, this is simply
     forwarded to addDependent:"

    ^ self addDependent:anObject

    "Created: 19.4.1996 / 10:28:53 / cg"
!

interrests
    "return a Collection of interrests - nil if there is none.
     Here, we use the normal dependents collection for interrests."

    ^ self dependents

    "Created: 19.4.1996 / 12:28:23 / cg"
    "Modified: 19.4.1996 / 18:07:12 / cg"
!

nonWeakDependents
    "return a Collection of dependents - nil if there is none.
     Since all dependencies are nonWeak in Model, this is a dummy."

    ^ self dependents

    "Created: 19.4.1996 / 10:29:43 / cg"
    "Modified: 19.4.1996 / 18:07:31 / cg"
!

removeNonWeakDependent:anObject
    "make the argument, anObject be independent of the receiver.
     Since all dependencies are nonWeak in Model, this is simply
     forwarded to removeDependent:"

    ^ self removeDependent:anObject

    "Created: 19.4.1996 / 12:19:40 / cg"
! !

!Model methodsFor:'drawing'!

displayOn:aGraphicsContext
    "display myself in aGraphicsContext"

    ^ self subclassResponsibility
!

displayOn:aGraphicsContext clippingBox:aRectangle
    "display a part of me in aGraphicsContext"

    ^ self subclassResponsibility
! !

!Model class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview2/Model.st,v 1.22 1996-04-19 16:07:39 cg Exp $'
! !