// Filename Cell.java
// Daniel Farinha
// January 1999

import java.awt.*;
import java.awt.event.*;

public class Cell extends Canvas {
	
  private boolean        state;               // the present state of the cell (dead=0 or alive=1)
  private int            cellID;              // the ID is given by the listener when it registers
  private ActionListener itsListener = null;  // the Cell listener identifier

  public Cell()
	{
    super();
    state  = false;
    cellID = 0;
    setBackground( Color.white);
    this.enableEvents( AWTEvent.MOUSE_EVENT_MASK);
  }

  public void setState( boolean newState)
  {
    state = newState;
		
    if ( state)
      this.setBackground( Color.black);
    else
      this.setBackground( Color.white);
		
    repaint();
  }
	
  public boolean getState()
  {
    return state;
  }
	
  public void paint(Graphics g)
  {
    g.drawRect(0,0,19,19);
  }
	
  public Dimension getPreferredSize()
  {
    return new Dimension(20,20);
  }
	
	
  protected void processMouseEvent( MouseEvent event)
  {
    if ( event.getID() == MouseEvent.MOUSE_PRESSED)
    {
			
      if( itsListener != null)
      {
        String cellIDString = new String( new Integer( cellID).toString());
				
        ActionEvent theEvent = new ActionEvent( this,
                                                ActionEvent.ACTION_PERFORMED,
                                                cellIDString);
        itsListener.actionPerformed( theEvent);
      }
    }
  }
	
	
  public void addActionListener( ActionListener listener, int newCellID) 
                 throws java.util.TooManyListenersException
  { 
    if ( itsListener == null)
    { 
      itsListener = listener;
      cellID = newCellID;
    }
    else
    { 
      throw new java.util.TooManyListenersException();
    } 
 }


  public void removeActionListener( ActionListener listener)
  { 
    if ( itsListener == listener)
    { 
      itsListener = null;
    }
  }
		
}