Model.st
author claus
Wed, 30 Aug 1995 01:43:11 +0200
changeset 98 ab8ed9e213d0
parent 96 948318b2fbd4
child 99 a656b0c9dd21
permissions -rw-r--r--
.

"
 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 comment:'
COPYRIGHT (c) 1992 by Claus Gittinger
	      All Rights Reserved

$Header: /cvs/stx/stx/libview2/Model.st,v 1.13 1995-08-29 17:44:15 claus Exp $
'!

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

version
"
$Header: /cvs/stx/stx/libview2/Model.st,v 1.13 1995-08-29 17:44:15 claus Exp $
"
!

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:'dependents access'!

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

    ^ dependents
!

dependents:aCollection
    "set the collection of dependents"

    dependents := aCollection
!

release
    dependents := nil
!

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

    |deps|

    deps := dependents.
    deps isNil ifTrue:[
	dependents := anObject
    ] ifFalse:[
	(deps isMemberOf:IdentitySet) ifTrue:[
	    deps add:anObject
	] ifFalse:[
	    dependents := IdentitySet with:dependents with:anObject
	]
    ]
!

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

    |deps|

    deps := dependents.
    deps notNil ifTrue:[
	(deps isMemberOf:IdentitySet) ifTrue:[
	    deps do:aBlock
	] ifFalse:[
	    aBlock value:deps
	]
    ]
!

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

    |deps sz|

    deps := dependents.
    deps notNil ifTrue:[
	(deps isMemberOf:IdentitySet) ifTrue:[
	    deps remove:anObject ifAbsent:[].
	    (sz := deps size) == 1 ifTrue:[
		dependents := deps first
	    ] ifFalse:[
		(sz == 0) ifTrue:[
		    dependents := nil
		]
	    ]
	] ifFalse:[
	    dependents := nil
	]
    ]
! !

!Model methodsFor:'copying'!

postCopy
    "release dependents after copying"

    self dependents:nil
! !

!Model methodsFor:'drawing'!

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

    ^ self subclassResponsibility
!

displayOn:aGraphicsContext
    "display myself in aGraphicsContext"

    ^ self subclassResponsibility
! !