import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.*;
import java.io.*;

/**
 * Sample application using Frame.
 *
 * @author 
 * @version 1.00 04/02/10
 */
public class WFGUI extends JFrame
{
  public static int WIDTH = 600; //default interface width
  public static int HEIGHT = 500; //default interface height
  public static String TITLE = "Wafers";
  
  //two bevel border objects one with a raised edge, one with a lowered edge
  BevelBorder bevelL = new BevelBorder(BevelBorder.LOWERED);
  BevelBorder bevelR = new BevelBorder(BevelBorder.RAISED);
  
  //content manager
  JLayeredPane layers = new JLayeredPane();
  
  //Empty component backdrop. Prevents nasty refresh issues with JFrame background.
  WComponent background;
   
  //Menu Bar
  WafersMenu menu;
  
  //buttons
  QuestionEditor qbutton;
	SurveyEditor survbutton;
	SurveyEngine ebutton;
  
  
  /**
   * The constructor.
   */  
   public WFGUI() 
   {
    try
    {
      //Setup Internal Class Properties
      setTitle(TITLE); //title of the main window
      addWindowListener(new WindowHandler()); //adds the window listener to allow you to close the window
      
      setContentPane(layers);

      
      //Instantiate external class objects
      background = new WComponent(layers,WIDTH,HEIGHT);
      menu = new WafersMenu(this);
      
      qbutton = new QuestionEditor(layers,bevelR);
      survbutton = new SurveyEditor(layers,bevelR);
      ebutton = new SurveyEngine(layers,bevelR);
      
      SetupLayout();
      SetupEventHandlers();
      
      setResizable(false);
      pack();
      setSize(WIDTH,HEIGHT); //sets the size of the main window
      setVisible(true);
      show();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
   }
  
  /*
  *UNUSED FUNCTION!!
  */
  public void SetupLayout()
  { 
    /* LAYOUT MANAGEMENT PROBLEM!!!
     * was too complex to complete before deadline of friday, perhaps will work on after deadline
     * switching back to manual layout.
     *
    //***************************
    //Setup layout of StimDisplay
    //***************************
    
    //Adjust constraints for the stimDisplay so that it's located at (5,5)
    layout.putConstraint(SpringLayout.WEST, stimDisplay.stimDisplay, 5, SpringLayout.WEST, layers);
    //layout.putConstraint(SpringLayout.WEST, layers, -5, SpringLayout.WEST, stimDisplay.stimDisplay);
    layout.putConstraint(SpringLayout.NORTH, stimDisplay.stimDisplay, 5, SpringLayout.NORTH, layers);
    layout.putConstraint(SpringLayout.WEST, stimPole.canvas, Spring.constant(5,5,1000), SpringLayout.EAST, stimDisplay.stimDisplay);
    //layout.putConstraint(SpringLayout.EAST, stimDisplay.stimDisplay, Spring.constant(-5), SpringLayout.WEST, stimPole.canvas);
    layout.putConstraint(SpringLayout.NORTH, stimDisplay.poleCue1, 5, SpringLayout.SOUTH, stimDisplay.stimDisplay);
    
    //Adjust constraints for poleCue1 so that it's located at 'stimDisplay' bottom edge
    //and the left edge of the layout.
    layout.putConstraint(SpringLayout.WEST, stimDisplay.poleCue1, 5, SpringLayout.WEST, layers);
    layout.putConstraint(SpringLayout.NORTH, stimDisplay.poleCue1, 5, SpringLayout.SOUTH, stimDisplay.stimDisplay);
    layout.putConstraint(SpringLayout.WEST, stimDisplay.pole1, 5, SpringLayout.EAST, stimDisplay.poleCue1);
    //layout.putConstraint(SpringLayout.NORTH, stimDisplay.poleCue2, 5, SpringLayout.SOUTH, stimDisplay.poleCue1);
    
    //Adjust constraints for pole1 so that it's located at 'stimDispaly' bottom edge
    //and the right edge of 'poleCue1'
    layout.putConstraint(SpringLayout.WEST, stimDisplay.pole1, 5, SpringLayout.EAST, stimDisplay.poleCue1);
    layout.putConstraint(SpringLayout.NORTH, stimDisplay.pole1, 5, SpringLayout.SOUTH, stimDisplay.stimDisplay);
    layout.putConstraint(SpringLayout.EAST, stimPole.canvas, 5, SpringLayout.EAST, stimDisplay.pole1);
    //layout.putConstraint(SpringLayout.EAST, stimDisplay.pole1, -5, SpringLayout.WEST, stimPole.canvas);
    layout.putConstraint(SpringLayout.NORTH, stimDisplay.pole2, 5, SpringLayout.SOUTH, stimDisplay.pole1);
    
    //Adjust constraints for poleCue2 so that it's located at 'poleCue1' bottom edge
    //and the left edge of the layout.
    layout.putConstraint(SpringLayout.WEST, stimDisplay.poleCue2, 5, SpringLayout.WEST, layers);
    layout.putConstraint(SpringLayout.NORTH, stimDisplay.poleCue2, 5, SpringLayout.SOUTH, stimDisplay.poleCue1);
    layout.putConstraint(SpringLayout.WEST, stimDisplay.pole2, 5, SpringLayout.EAST, stimDisplay.poleCue2);
    //layout.putConstraint(SpringLayout.NORTH, stimDisplay.poleCue3, 5, SpringLayout.SOUTH, stimDisplay.poleCue2);
    
    //Adjust constraints for pole2 so that it's located at 'pole1' bottom edge
    //and the right edge of 'poleCue2'
    layout.putConstraint(SpringLayout.WEST, stimDisplay.pole2, 5, SpringLayout.EAST, stimDisplay.poleCue2);
    layout.putConstraint(SpringLayout.NORTH, stimDisplay.pole2, 5, SpringLayout.SOUTH, stimDisplay.poleCue1);
    layout.putConstraint(SpringLayout.EAST, stimPole.canvas, 5, SpringLayout.EAST, stimDisplay.pole2);
    layout.putConstraint(SpringLayout.NORTH, stimDisplay.pole3, 5, SpringLayout.SOUTH, stimDisplay.pole2);
    
    //Adjust constraints for poleCue3 so that it's located at 'poleCue2' bottom edge
    //and the left edge of the layout.
    layout.putConstraint(SpringLayout.WEST, stimDisplay.poleCue3, 5, SpringLayout.WEST, layers);
    layout.putConstraint(SpringLayout.NORTH, stimDisplay.poleCue3, 5, SpringLayout.SOUTH, stimDisplay.poleCue2);
    layout.putConstraint(SpringLayout.WEST, stimDisplay.pole3, 5, SpringLayout.EAST, stimDisplay.poleCue3);
    //layout.putConstraint(SpringLayout.SOUTH, layers, 5, SpringLayout.SOUTH, stimDisplay.poleCue3);
    
    //Adjust constraints for pole3 so that it's located at 'pole2' bottom edge
    //and the right edge of 'poleCue3'
    layout.putConstraint(SpringLayout.WEST, stimDisplay.pole3, 5, SpringLayout.EAST, stimDisplay.poleCue3);
    layout.putConstraint(SpringLayout.NORTH, stimDisplay.pole3, 5, SpringLayout.SOUTH, stimDisplay.poleCue2);
    layout.putConstraint(SpringLayout.EAST, stimPole.canvas, 5, SpringLayout.EAST, stimDisplay.pole3);
    //layout.putConstraint(SpringLayout.EAST, stimDisplay.pole3, Spring.constant(-5), SpringLayout.WEST, stimPole.canvas);
    layout.putConstraint(SpringLayout.SOUTH, layers, 5, SpringLayout.SOUTH, stimDisplay.pole3);
    
    //***************************
    //Setup layout of SRPole
    //***************************
    
    //layout.putConstraint(SpringLayout.WEST, stimPole.canvas, 5, SpringLayout.EAST, stimDisplay.stimDisplay);
    layout.putConstraint(SpringLayout.EAST, stimPole.canvas, -5, SpringLayout.EAST, layers);
    layout.putConstraint(SpringLayout.SOUTH, stimPole.canvas, -5, SpringLayout.SOUTH, layers);
    */
  }
  
  public void SetupEventHandlers()
  {

    menu.fileExit.addActionListener(new MenuItemHandler());
    menu.toolsQuestionEditor.addActionListener(new MenuItemHandler());
    menu.toolsSurveyEditor.addActionListener(new MenuItemHandler());
    menu.toolsLaunchSurvey.addActionListener(new MenuItemHandler());
    
    //stimPole.canvas.addMouseMotionListener(new MouseMotionHandler());
    //stimPole.canvas.addMouseListener(new MouseHandler());
	
    //buttons
    qbutton.quest.addActionListener(new ButtonItemHandler());
    survbutton.survedit.addActionListener(new ButtonItemHandler());
    ebutton.engine.addActionListener(new ButtonItemHandler());
	
  }
  
  /**
  Sets up the Window Closing event handlers for the Wafers interface.
  */
  public class WindowHandler extends WindowAdapter
  {
    /**
    Handles closing of the main program window.
    */
    public void windowClosing(WindowEvent e)
    {
      System.exit(0);
    }
    
    public void windowActivated(WindowEvent e)
    {
      validate();
    }
  
    public void windowDeiconified(WindowEvent e)
    {
      validate();
    }
  
    public void windowGainedFocus(WindowEvent e)
    {
      validate();
    }
  
    public void windowOpened(WindowEvent e)
    {
      validate();
    }
  }

  public class MouseHandler extends MouseAdapter
  {
    public void mouseClicked(MouseEvent e)
    {
      //stimPole.update(e.getX(),e.getY());
    }
  }

  public class MouseMotionHandler extends MouseMotionAdapter
  {
    public void mouseDragged(MouseEvent e)
    {
      //stimPole.update(e.getX(),e.getY());
    }
  }
  
  
  public class ButtonItemHandler implements ActionListener
  {
   public void actionPerformed(ActionEvent e)
    {
      String cmd = e.getActionCommand();
     
       	if(cmd.equals("Question Editor"))
        {
         
		WafersEdit application = new WafersEdit();
     	}//end if
	else if(cmd.equals("Survey Editor"))
	{
		UserEdit application3 = new UserEdit();
	}
	else if(cmd.equals("Launch Survey"))
	{
	WafersSurvey application4 = new WafersSurvey();
	}			
		
   }
 }		
  
  
  public class MenuItemHandler implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      String cmd = e.getActionCommand();
      if(cmd.equals("Exit"))
        System.exit(0);
      else if(cmd.equals("Question Editor"))
      {
        WafersEdit application = new WafersEdit();
      }
      else if(cmd.equals("Survey Editor"))
      {
        UserEdit application3 = new UserEdit();
      }
      else if(cmd.equals("Launch Survey"))
      {
        WafersSurvey application4 = new WafersSurvey();
      }
    }
  }
  
}
