org.scopemvc.model.basic
Class BasicModel

java.lang.Object
  |
  +--org.scopemvc.model.basic.BasicModel
All Implemented Interfaces:
ModelChangeEventSource, ModelChangeListener, ModelChangeTypes
Direct Known Subclasses:
ArrayModel, ListModel, ListModelAdaptor, SetModel

public abstract class BasicModel
extends java.lang.Object
implements ModelChangeEventSource, ModelChangeTypes

A simple implementation of ModelChangeEventSource for use as a base class. An alternative (JavaBeans style) is to use ModelChangeEventSupport as a delegate in all models.

To implement a BasicModel:

  1. extend BasicModel,
  2. Notify interested listeners of changes in Model state at the end of set methods by: fireModelChange(...)
  3. Register as a ModelChangeListener with submodels in the appropriate set methods to ensure event propagation up the tree of models. For example:
       public final static Selector NAME = Selector.fromString("name");
       public final static Selector ADDRESS = Selector.fromString("address");
    
       public void setAddress(AddressModel inAddress) throws ModelException {
           if (inAddress == address) {
               return;
           }
         unlistenOldSubmodel(ADDRESS); address =
       inAddress; listenNewSubmodel(ADDRESS);
       fireModelChange(VALUE_CHANGE, ADDRESS); } 

    This ensures that any change in the Address's state is received by this parent which will then fire its own ModelChangeEvent to its listeners. See ModelChangeEventSource for more discussion. Note the use of the Selector constants: this is for convenience (and performance).
  4. Use Scope's collection models instead of the regular Java collections: Scope collections propagate changes to submodels as above and are compliant ModelChangeEventSources. These collections must be listened to in the same way as other submodel properties. Note that Scope collections are just thin wrappers on native Java collections.

Version:
$Revision: 1.11 $ $Date: 2002/09/12 10:51:03 $
Author:
Steve Meyfroidt

Field Summary
 
Fields inherited from interface org.scopemvc.core.ModelChangeTypes
ACCESS_CHANGED, VALUE_ADDED, VALUE_CHANGED, VALUE_REMOVED
 
Constructor Summary
BasicModel()
           
 
Method Summary
 void addModelChangeListener(ModelChangeListener inListener)
          Adds a listener for ModelChangeEvent
 void fireModelChange(int inChangeType, Selector inSelector)
          Fire a ModelChangeEvent to all listeners
protected  void listenNewSubmodel(Selector inSelector)
          Convenience for BasicModel implementors: call this at the end of setters for submodel properties (ie properties of type ModelChangeEventSource) to register this as a ModelChangeListener to the current submodel for event propagation.
 void makeActive(boolean inActive)
           Control whether this BasicModel broadcasts ModelChangeEvents.
 void modelChanged(ModelChangeEvent inEvent)
           Handle changes to children ModelChangeEventSources by firing a change event from this (and propagating the original Selector).
 void removeModelChangeListener(ModelChangeListener inListener)
          Removes a listener for ModelChangeEvent
protected  void unlistenOldSubmodel(Selector inSelector)
          Convenience for BasicModel implementors: call this at the start of setters for submodel properties (ie properties of type ModelChangeEventSource) to deregister this as a ModelChangeListener to the current submodel (about to be set to another Model).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BasicModel

public BasicModel()
Method Detail

addModelChangeListener

public final void addModelChangeListener(ModelChangeListener inListener)
Adds a listener for ModelChangeEvent

Specified by:
addModelChangeListener in interface ModelChangeEventSource
Parameters:
inListener - The listener to be added

removeModelChangeListener

public final void removeModelChangeListener(ModelChangeListener inListener)
Removes a listener for ModelChangeEvent

Specified by:
removeModelChangeListener in interface ModelChangeEventSource
Parameters:
inListener - The listener to be removed

fireModelChange

public final void fireModelChange(int inChangeType,
                                  Selector inSelector)
Fire a ModelChangeEvent to all listeners

Specified by:
fireModelChange in interface ModelChangeEventSource
Parameters:
inChangeType - The type of the change. One of the ModelChangeTypes values
inSelector - The Selector for the property of the model affected by the change

modelChanged

public void modelChanged(ModelChangeEvent inEvent)

Handle changes to children ModelChangeEventSources by firing a change event from this (and propagating the original Selector). If the event's source is no longer a child property of this parent then there is no need to continue propagating the event.

Specified by:
modelChanged in interface ModelChangeEventSource
Parameters:
inEvent - the event received from a child ModelChangeEventSource.

makeActive

public void makeActive(boolean inActive)

Control whether this BasicModel broadcasts ModelChangeEvents. Make sure nested calls are properly matched to fully re-activate a BasicModel that was deactivated.

Subclasses may override this to propagate the activation state to child BasicModel properties if necessary.

Parameters:
inActive - true to activate this model, false to desactivate it

unlistenOldSubmodel

protected final void unlistenOldSubmodel(Selector inSelector)
Convenience for BasicModel implementors: call this at the start of setters for submodel properties (ie properties of type ModelChangeEventSource) to deregister this as a ModelChangeListener to the current submodel (about to be set to another Model).

Parameters:
inSelector - The Selector for the submodel

listenNewSubmodel

protected final void listenNewSubmodel(Selector inSelector)
Convenience for BasicModel implementors: call this at the end of setters for submodel properties (ie properties of type ModelChangeEventSource) to register this as a ModelChangeListener to the current submodel for event propagation.

Parameters:
inSelector - The Selector for the submodel


Copyright © 2000-2002 The Scope Team. All Rights Reserved.