/*
 * DiningRoom.java
 *
 * The Dining Philosophers problem using message passing
 *
 * copyright (c) 2000, 2001, 2002, 2010 - Russell C. Bjork
 */

package philosophers;
import concurrency.Message;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/** Simulation of the philosophers' dining room */

public class DiningRoom extends JFrame
{
    /** Main program for the simulation. */
     
    public static void main(String [] args)
    {
        new DiningRoom();
    }

    /** Constructor.  Create the dining room (displayed as a frame on the
     *  screen), along with the philosophers
     */
    public DiningRoom()
    {
        // Set up the frame
        
        super("Dining Philosophers");
        getContentPane().setLayout(null);
        setSize(OVERALL_WIDTH, OVERALL_HEIGHT);
                
        // Create the labels representing the philosophers on the screen
        
        JLabel [] philosopherRepresentation = new JLabel[NUMBER_OF_PHILOSOPHERS];
        for (int i = 0; i < NUMBER_OF_PHILOSOPHERS; i ++)
        {
            philosopherRepresentation[i] = new JLabel(PHILOSOPHER_NAME[i], SwingConstants.CENTER);
            philosopherRepresentation[i].setFont(new Font("SansSerif", Font.PLAIN, 24));
            getContentPane().add(philosopherRepresentation[i]);
            philosopherRepresentation[i].setSize(PHILOSOPHER_WIDTH, PHILOSOPHER_HEIGHT);
            philosopherRepresentation[i].setLocation(PHILOSOPHER_X[i], PHILOSOPHER_Y[i]);
            philosopherRepresentation[i].setForeground(Color.white);

            // This action listener will respond to a click on the philospher's name
            
            final int philosopherNumber = i;
            philosopherRepresentation[i].addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e)
                {
                    philosopher[philosopherNumber].kill();
                }
              });
        }
        
        // Create the philosophers themselves
        
        philosopher = new Philosopher[NUMBER_OF_PHILOSOPHERS];
        for (int i = 0; i < NUMBER_OF_PHILOSOPHERS - 1; i ++)
            philosopher[i] = new Philosopher(philosopherRepresentation[i], 
                                             i, 
                                             i + 1); 
        philosopher[NUMBER_OF_PHILOSOPHERS - 1] =
            new Philosopher(philosopherRepresentation[NUMBER_OF_PHILOSOPHERS - 1], 
                            0, 
                            NUMBER_OF_PHILOSOPHERS - 1);

        // Arrange to terminate the program when window is closed
        
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e)
            {
                setVisible(false);
                dispose();
                System.exit(0);
            }
          });
          
        // Show the dining room on the screen
        
        setVisible(true);
        
        // Start the threads

        Coordinator.getInstance().start();

        for (int i = 0; i < NUMBER_OF_PHILOSOPHERS; i ++)
            philosopher[i].start();
        
    }

    /** The number of philosophers
     */
    public static final int NUMBER_OF_PHILOSOPHERS = 5;
    
    // Constants defining the visible layout
    
    private static final int OVERALL_WIDTH = 650;
    private static final int OVERALL_HEIGHT = 350;
    private static final String [] PHILOSOPHER_NAME =
        { "SOCRATES", "PLATO", "NIETZSCHE", "MARX", "HEGEL" };
    private static final int PHILOSOPHER_WIDTH = 150;
    private static final int PHILOSOPHER_HEIGHT = 50;
    private static final int [] PHILOSOPHER_X = { 250, 450, 400, 100, 50 };
    private static final int [] PHILOSOPHER_Y = { 50, 150, 250, 250, 150 };
    
    // The philosophers
    
    private Philosopher [] philosopher;
}
