/*
 * HeartController
 *
 * A controller for the heart beat model. Has two buttons for
 * the user to change the speed that which the heart beats.
 *
 * Most of this code was generated by Visual Cafe's drag and
 * drop GUI builder.  The code that I added is located at the
 * end of the file.
 *
 *@author <a href="http://www.cs.indiana.edu/~cbaray/">cristobal baray</a> cbaray@cs.indiana.edu 
 */
 
 
import java.util.Observable;

import java.awt.*;

public class HeartController 
	extends Frame
{
	public HeartController()
	{
		// This code is automatically generated by Visual Cafe when you addExcitement
		// components to the visual environment. It instantiates and initializes
		// the components. To modify the code, only use code syntax that matches
		// what Visual Cafe can generate, or Visual Cafe may be unable to back
		// parse your Java file into its visual environment.
		//{{INIT_CONTROLS
		setLayout(new FlowLayout(FlowLayout.CENTER,15,5));
		setVisible(false);
		setSize(213,40);
		removeExcitement = new java.awt.Button();
		removeExcitement.setLabel("bore");
		removeExcitement.setBounds(19,5,46,20);
		add(removeExcitement);
		addExcitement = new java.awt.Button();
		addExcitement.setLabel("excite");
		addExcitement.setBounds(80,5,55,20);
		add(addExcitement);
		stopButton = new java.awt.Button();
		stopButton.setLabel("stop");
		stopButton.setBounds(150,5,44,20);
		add(stopButton);
		setTitle("Heart Controller");
		setResizable(false);
		//}}

		//{{INIT_MENUS
		//}}

		//{{REGISTER_LISTENERS
		SymWindow aSymWindow = new SymWindow();
		this.addWindowListener(aSymWindow);
		SymAction lSymAction = new SymAction();
		removeExcitement.addActionListener(lSymAction);
		addExcitement.addActionListener(lSymAction);
		stopButton.addActionListener(lSymAction);
		//}}
	}

	public HeartController(String title)
	{
		this();
		setTitle(title);
	}

    /**
     * Shows or hides the component depending on the boolean flag b.
     * @param b  if true, show the component; otherwise, hide the component.
     * @see java.awt.Component#isVisible
     */
    public void setVisible(boolean b)
	{
		if(b)
		{
			setLocation(50, 50);
		}
		super.setVisible(b);
	}

	public void addNotify()
	{
	    // Record the size of the window prior to calling parents addNotify.
	    Dimension d = getSize();
	    
		super.addNotify();

		if (fComponentsAdjusted)
			return;

		// Adjust components according to the insets
		setSize(insets().left + insets().right + d.width, insets().top + insets().bottom + d.height);
		Component components[] = getComponents();
		for (int i = 0; i < components.length; i++)
		{
			Point p = components[i].getLocation();
			p.translate(insets().left, insets().top);
			components[i].setLocation(p);
		}
		fComponentsAdjusted = true;
	}

    // Used for addNotify check.
	boolean fComponentsAdjusted = false;

	//{{DECLARE_CONTROLS
	java.awt.Button removeExcitement;
	java.awt.Button addExcitement;
	java.awt.Button stopButton;
	//}}

	//{{DECLARE_MENUS
	//}}

	class SymWindow extends java.awt.event.WindowAdapter
	{
		public void windowClosing(java.awt.event.WindowEvent event)
		{
			Object object = event.getSource();
			if (object == HeartController.this)
				HeartController_WindowClosing(event);
		}
	}
	
	void HeartController_WindowClosing(java.awt.event.WindowEvent event)
	{
		setVisible(false);		 // hide the Frame
	}

	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == removeExcitement)
				removeExcitement_ActionPerformed(event);
			else if (object == addExcitement)
				addExcitement_ActionPerformed(event);
			else if (object == stopButton)
				stopButton_ActionPerformed(event);
		}
	}


/////////////////////////////////////////////////////////////////////


	
	/** How much to increase the excitement level */
	private static final long INCREASE_AMOUNT = 2;
	
	/** How much to decrase the excitement level */
	private static final long DECREASE_AMOUNT = -1;
	
	/** A reference to the model */
	private HeartBeat myHeart;
	
	
	/** 
	 * Constructor that takes a model.  Calls the default
	 * constructor and then stores a reference to the model
	 * that this object will control.
	 *
	 *@param model The model this controller will control.
	 */
	public HeartController(Observable model) {
		this();
		myHeart = (HeartBeat) model;
	}
	 
	/**
	 * When the user hits the remove button, tell the model to 
	 * adjust its excitementLevel appropriately.
	 */
	void removeExcitement_ActionPerformed(java.awt.event.ActionEvent event)
	{
		myHeart.adjustExcitementLevel(INCREASE_AMOUNT);
	}
	
	/**
	 * When the user hits the excite button, tell the model to 
	 * adjust its excitementLevel appropriately.
	 */
	void addExcitement_ActionPerformed(java.awt.event.ActionEvent event)
	{
		myHeart.adjustExcitementLevel(DECREASE_AMOUNT);
	}

	/**
	 * When the user hits the stop button, tell the model to halt.
	 */
	void stopButton_ActionPerformed(java.awt.event.ActionEvent event)
	{
		myHeart.stopHeart();
	}
}

