/***************************************************************
 * Source: @(#)$Id$
 * Scope:  Model base class
 *
 *  12/14/1999  GVT  First Implementation
 ***************************************************************/

package gvt.mvc;


import java.util.*;

/**
 * This class is the superclass of all models
 *
 * @author   Giuseppe Vitillaro
 * @version  $Id$
 */
public class Model {
    private boolean changed = false;
    private Vector views = new Vector();

    
    /**
     * Add a view to the model list.
     */
    public void addView ( View view ) {
	views.addElement ( view );
	view.updateView ( this );
    }

    
    /**
     * Remove a view from the model list.
     */
    public void removeView ( View view ) {
	views.removeElement ( view );
    }

    
    /**
     * Set the changed flag.
     */
    public void setChanged ( ) {
	changed = true;
    }

    
    /**
     * Clear the changed flag.
     */
    public void clearChanged ( ) {
	changed = false;
    }


    /**
     * Query the changed flag.
     */
    public boolean hasChanged ( ) {
	return changed;
    }


    /**
     * Updated all views attached to the model.
     */
    public void notifyViews ( ) {
	if ( hasChanged() ) {
	    for ( int i = 0; i < views.size(); i++ )
		((View) views.elementAt(i)).updateView ( this );
	    clearChanged();
	}
    }
}
