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

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

public class CellInterface extends    Panel
                           implements ActionListener
{
  private final Label TITLE = new Label("Game of Life", Label.CENTER);
  private Button startButton, stopButton, resetButton;	// Control buttons
  private Cell aCell[] = new Cell[100];
  private ActionListener myListener;
  private Font fontType  = new Font  ("Helvetica", Font.BOLD, 20);
	
  static final String DISABLE = "disable";
  static final String ENABLE = "enable";
	
  public CellInterface( ActionListener sendHere)
  {
		
    TITLE.setFont(fontType);

    myListener = sendHere;
		
    // Create start button
    startButton = new Button(    "Start");
    startButton.setActionCommand("start");
    startButton.addActionListener( sendHere);
    startButton.setEnabled(false);
	
    // Create stop button
    stopButton = new Button(     "Stop");
    stopButton.setActionCommand( "stop");
    stopButton.addActionListener( sendHere);
    stopButton.setEnabled( false);
		
    // Create reset button
    resetButton = new Button(    "Reset");
    resetButton.setActionCommand("reset");	
    resetButton.addActionListener( sendHere);	
    resetButton.setEnabled(false);
		
    // Create control panel and add buttons to it
    Panel controlPanel = new Panel(new FlowLayout());
    controlPanel.add(startButton);
    controlPanel.add(stopButton);
    controlPanel.add(resetButton);
		
    // Create panel for world (cell grid)
    Panel world = new Panel (new GridLayout(10, 10, 0, 0));
		
    for ( int i=0; i <= 99; i++)
    {
      aCell[i] = new Cell();                  // Initialise all cells...
      try
      {
        aCell[i].addActionListener( this, i); // ...and register as listener
      }
      catch ( java.util.TooManyListenersException exception)
      {
         // do nothing
      }
		
    }
		

    for (int i=0; i <= 99; i++)
    world.add(aCell[i]);                      // Add cells to grid
		
		
    this.setLayout(new BorderLayout());           // Create main border layout
    this.add( BorderLayout.NORTH, TITLE);         // Add the title on top
    this.add( BorderLayout.CENTER, world);        // Add the cell grid at the center
    this.add( BorderLayout.SOUTH, controlPanel);  // Add the control panel at the bottom
  } // End CellInterface
	
	
  public void setResetState()
  {
    startButton.setEnabled( false);
    stopButton.setEnabled(  false);
    resetButton.setEnabled( false);
		
    for (int i=0; i <= 99; i++)
      aCell[i].setState(false);                   // reset all cells
  }
	
  public void setEditState()
  {
    startButton.setEnabled( true);
    stopButton.setEnabled(  false);
    resetButton.setEnabled( true);
  }

  public void setRunningState()
  {
    startButton.setEnabled( false);
    stopButton.setEnabled(  true);
    resetButton.setEnabled( false);
  }
	
  public synchronized void actionPerformed( ActionEvent event)
  {
    String theCommand = event.getActionCommand();
		
    ActionEvent theEvent = new ActionEvent( this,
                                            ActionEvent.ACTION_PERFORMED,
                                            theCommand);
    myListener.actionPerformed( theEvent);
  }
	
	
  public void modifyGUICell( int cellID, boolean newState)    // updates single cell in GUI
  {
    aCell[cellID].setState(newState);
  }
	
  public void writeNewGeneration( WorldArray aGeneration)     // updates all the GUI cells with new generation
  {
    for (int i = 0; i <= 99; i++)
    {
      if( aCell[i].getState() != aGeneration.array[i])    // if GUI cell state different
        aCell[i].setState( aGeneration.array[i]);         // update GUI cell state
    }
  }
		
	
} // End CellInterface
	