//TT4DSquare.java
 
/** 
 *
 * @author  Sean Bridges
 * @version 1.0
 *
 * A TT4DSquare holds the contents of 1 square.  It can inform its listeners 
 * when it changes.
 */

import java.util.*;

public class TT4DSquare
{

//------------------------------------------
    //instance variables
    private String contents = TT4DBoard.NULL_PLAYER_STRING;
    private Vector listeners = new Vector();
    private TT4DPoint point;
    
    
//------------------------------------------
    //constructors

    /** 
     * Creates new TT4DSquare 
     */
    public TT4DSquare(TT4DPoint point) 
    {
        this.point = point;
    }

//------------------------------------------
    //listeners
    public void addListener(TT4DSquareListener l)
    {
        listeners.addElement(l);
    }
    
    public void remmoveListener(TT4DSquareListener l)
    {
        listeners.removeElement(l);
    }    
    
    private void notifyListeners(String oldContents, String newContents)
    {
        Enumeration e = listeners.elements();
        while(e.hasMoreElements())
        {
            ((TT4DSquareListener) e.nextElement() ).contentsChanged(oldContents, newContents);
        }
    }


//------------------------------------------
    //instance methods
  
    public String getContents()
    {
        return contents;
    }

    public void setContents(String aString)
    {
        String old = contents;
        contents = aString;
        notifyListeners(old, contents);
    }

    public TT4DPoint getPosition()
    {
        return point;
    }

    public String toString()
    {
        return "Square holding:" + contents + " at:" + point;
    }
  
}//end class TT4DSquare