Model.st
author Claus Gittinger <cg@exept.de>
Sat, 27 Jan 1996 19:36:37 +0100
changeset 158 16f2237474fe
parent 153 0a44627e1712
child 163 80f4fe3bcc71
permissions -rw-r--r--
only init once

"
 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.
    (deps isNil and:[anObject isCollection not]) ifTrue:[
	dependents := anObject
    ] ifFalse:[
	deps isCollection ifTrue:[
	    deps add:anObject
	] ifFalse:[
	    dependents := IdentitySet with:dependents with:anObject
	]
    ]
!

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

    ^ dependents
!

dependents:aCollection
    "set the collection of dependents"

    dependents := aCollection
!

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
    dependents := nil
!

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

    |deps sz|

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

!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.18 1996-01-15 17:37:08 cg Exp $'
! !