edu.cmu.sun.model
Class Model

java.lang.Object
  extended by edu.cmu.sun.model.Model
Direct Known Subclasses:
ColumnModel, ColumnModel.Stacked, ItemModel, SceneModel, WindowModel

public class Model
extends java.lang.Object

A basic Observer pattern that holds off update notification. All models can have observers (or listeners) who listen for changes to the model. Most models notify listeners when the change happens. This model works slightly differently. The models mark when they need an update. Then, when the static method notifyAllListeners() is called, every Model object then notifys it's listeners if a change has occoured. There is probably a better way to handle this change, but we use this to hold off updates from applying to the scene graph untill all of the models have been updated first.

Author:
Braden Kowitz

Field Summary
private static java.util.List<Model> allModels
          Pointers to every Model object created in the system.
private  java.util.Set<ModelListener> listeners
          A set of listeners for this Model.
private  boolean modelNeedsUpdate
          True if the model has changed that the listeners need an update.
 
Constructor Summary
Model()
          Create a new model.
 
Method Summary
 void addModelListener(ModelListener l)
          Add a listener to this model.
protected  void needsUpdate()
          Called by subclasses to signial that a model has changed and needs updating.
static void notifyAllListeners()
          Calls notifyListeners() on every Model object in the system.
 void notifyListeners()
          Sends a modelChanged(...) call to all listeners of this model.
 void removeModelListener(ModelListener l)
          Remove a listener from this model.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

allModels

private static java.util.List<Model> allModels
Pointers to every Model object created in the system. (I know this creates a memory leak, but this is just a prototype)


listeners

private java.util.Set<ModelListener> listeners
A set of listeners for this Model. NOTE TO LG3D Developers: I ran into a bug where LG3D's event model appeared to NOT use a set for it's observer pattern. An object that added itslef as a listener twice, received double updates. I believe this behavior is wrong, and this Model class implements the observer pattern correctly (other than the hold-off feature).


modelNeedsUpdate

private boolean modelNeedsUpdate
True if the model has changed that the listeners need an update.

Constructor Detail

Model

public Model()
Create a new model.

Method Detail

addModelListener

public void addModelListener(ModelListener l)
Add a listener to this model. If the model changes, it will update all of the listeners when notifyAllListeners() is called.


removeModelListener

public void removeModelListener(ModelListener l)
Remove a listener from this model. The listener will no longer receive modelChanged() updates from this Model.


notifyAllListeners

public static void notifyAllListeners()
Calls notifyListeners() on every Model object in the system. (This is a bit of a hack)


notifyListeners

public void notifyListeners()
Sends a modelChanged(...) call to all listeners of this model.


needsUpdate

protected void needsUpdate()
Called by subclasses to signial that a model has changed and needs updating. Really, the views need updating, the model needs to send out modelChanged(...) updates.