import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
 *  Genetic Algorithm Exploration
 *  Controller component: creates the model and view, 
 *  frame and control buttons, then waits for "Run" command.
 *  @author Roger Frank
 *  @version 1.1
 */

class GA extends JFrame implements ActionListener {

    private GAview view;
    private GAmodel model;
    private JButton runButton, pauseButton, resumeButton;

    public static final int GRIDX = 30;
    public static final int GRIDY = 30;
    public static final int TIMER_DELAY_MSEC = 200;
    public static final String WORLD_FILE = "world.txt";

    GA() {
        super( "Genetic Algorithm Exploration" );

        // build the buttons
        JPanel controlPanel =
            new JPanel( new FlowLayout( FlowLayout.CENTER ) );
        runButton = new JButton( "Run" );
        runButton.addActionListener( this );
        runButton.setEnabled( true );
        controlPanel.add( runButton );
        pauseButton = new JButton( "Pause" );
        pauseButton.addActionListener( this );
        pauseButton.setEnabled( false );
        controlPanel.add( pauseButton );
        resumeButton = new JButton( "Resume" );
        resumeButton.addActionListener( this );
        resumeButton.setEnabled( false );
        controlPanel.add( resumeButton );

        // build the view
        view = new GAview();
        view.setBackground( Color.white );

        // put buttons, view together
        Container c = getContentPane();
        c.add( controlPanel, BorderLayout.NORTH );
        c.add( view, BorderLayout.CENTER );

        // build the model
        model = new GAmodel( view );
    }

    public void actionPerformed( ActionEvent e ) {
        JButton b = ( JButton ) e.getSource();
        if ( b == runButton ) {
            model.run();
            runButton.setEnabled( false );
            pauseButton.setEnabled( true );
            resumeButton.setEnabled( false );
        } else if ( b == pauseButton ) {
            model.pause();
            runButton.setEnabled( false );
            pauseButton.setEnabled( false );
            resumeButton.setEnabled( true );
        } else if ( b == resumeButton ) {
            model.resume();
            runButton.setEnabled( false );
            pauseButton.setEnabled( true );
            resumeButton.setEnabled( false );
        }
    }

    public static void main( String[] args ) {
        GA ashley = new GA();
        ashley.addWindowListener( new WindowAdapter() 
                {
                  public void windowClosing( WindowEvent e ) {
                      System.exit( 0 );
                  }
                });
        ashley.setSize( 300, 350 );
        ashley.show();
    }
}
