//-----------------------------------------------------------------------------
//
// $Id: ButtonSquareLayout.java 8 2006-02-01 16:58:12Z jnross $
// $HeadURL: svn+ssh://wopr.csl.mtu.edu/major/jnross/svn_repos/cs4760/TdaSimulator/app-src/ButtonSquareLayout.java $
//
//-----------------------------------------------------------------------------

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

import javax.swing.*;

public class ButtonSquareLayout extends SimulatorLayoutPanel implements MouseListener, KeyListener{

	TdaApp app;
	
	private JButton buttonOne = new JButton("1");
	private JButton buttonTwo = new JButton("2");
	private JButton buttonThree = new JButton("3");
	private JButton buttonFour = new JButton("4");
	
	public void setupDisplay(JPanel appPanel, TdaApp app) {
		if (app.isWide())
		{
			setupVertical(appPanel);
		}
		else
		{
			setupHorizontal(appPanel);
		}
	}
	
	public void setupVertical(JPanel appPanel)
	{
		setSize(320, 432);
		setLayout(null);
        
        appPanel.setBounds(0,0,320,240);
        add(appPanel);
        
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(2,2));
        buttonPanel.add(buttonOne);
        buttonPanel.add(buttonTwo);
        buttonPanel.add(buttonThree);
        buttonPanel.add(buttonFour);
        buttonPanel.setBounds(0,240,320, 192);
        buttonOne.setBackground(Color.red);
        buttonOne.setForeground(Color.white);
        buttonTwo.setBackground(Color.green);
        buttonTwo.setForeground(Color.black);
        buttonThree.setBackground(Color.yellow);
        buttonThree.setForeground(Color.black);
        buttonFour.setBackground(Color.blue);
        buttonFour.setForeground(Color.white);
        add(buttonPanel);
	}
	
	private void setupHorizontal(JPanel appPanel)
    {
    	setSize(432, 320);
    	setLayout(null);
        
        appPanel.setBounds(0,0,240,320);
        add(appPanel);
        
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(2,2));
        buttonPanel.add(buttonTwo);
        buttonPanel.add(buttonFour);
        buttonPanel.add(buttonOne);
        buttonPanel.add(buttonThree);
        buttonPanel.setBounds(240, 0, 192, 320);
        buttonOne.setBackground(Color.red);
        buttonOne.setForeground(Color.white);
        buttonTwo.setBackground(Color.green);
        buttonTwo.setForeground(Color.black);
        buttonThree.setBackground(Color.yellow);
        buttonThree.setForeground(Color.black);
        buttonFour.setBackground(Color.blue);
        buttonFour.setForeground(Color.white);
        
        add(buttonPanel);
    }
	
	public void setupButtonListeners(TdaApp app) {
		this.app = app;
		buttonOne.addMouseListener(this);
        buttonTwo.addMouseListener(this);
        buttonThree.addMouseListener(this);
        buttonFour.addMouseListener(this);
        buttonOne.addKeyListener(this);
        buttonTwo.addKeyListener(this);
        buttonThree.addKeyListener(this);
        buttonFour.addKeyListener(this);
	}
	
	public void mousePressed(MouseEvent e){
    	if (e.getSource() == buttonOne)
    	{
    		app.buttonOneDown(e);
    	}
    	if (e.getSource() == buttonTwo)
    	{
    		app.buttonTwoDown(e);
    	}
    	if (e.getSource() == buttonThree)
    	{
    		app.buttonThreeDown(e);
    	}
    	if (e.getSource() == buttonFour)
    	{
    		app.buttonFourDown(e);
    	}
    }
	public void mouseReleased(MouseEvent e){
    	if (e.getSource() == buttonOne)
    	{
    		app.buttonOneUp(e);
    	}
    	if (e.getSource() == buttonTwo)
    	{
    		app.buttonTwoUp(e);
    	}
    	if (e.getSource() == buttonThree)
    	{
    		app.buttonThreeUp(e);
    	}
    	if (e.getSource() == buttonFour)
    	{
    		app.buttonFourUp(e);
    	}
    }
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mouseClicked(MouseEvent e){}
	
    public void keyPressed(KeyEvent e) {
		switch(e.getKeyCode()) {
			case KeyEvent.VK_1:
				app.buttonOneDown(e);
				break;
			case KeyEvent.VK_2:
				app.buttonTwoDown(e);
				break;
			case KeyEvent.VK_3:
				app.buttonThreeDown(e);
				break;
			case KeyEvent.VK_4:
				app.buttonFourDown(e);
				break;		
		}
	}

	public void keyReleased(KeyEvent e) {
		switch(e.getKeyCode()) {
			case KeyEvent.VK_1:
				app.buttonOneUp(e);
				break;
			case KeyEvent.VK_2:
				app.buttonTwoUp(e);
				break;
			case KeyEvent.VK_3:
				app.buttonThreeUp(e);
				break;
			case KeyEvent.VK_4:
				app.buttonFourUp(e);
				break;		
		}
	}

	public void keyTyped(KeyEvent e) {}
}
