/*
 * HeartView
 *
 * It is a simple view for the heart beat model.  This just displays
 * the number of heart beats done so far...
 *
 * 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.Observer;
import java.util.Observable;

import java.awt.*;

public class HeartView 
	extends Frame
	implements Observer
{
	public HeartView()
	{
		// This code is automatically generated by Visual Cafe when you add
		// 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.LEFT,15,5));
		setVisible(false);
		setSize(215,47);
		label1 = new java.awt.Label("Heart beats : ");
		label1.setBounds(15,5,83,25);
		add(label1);
		heartbeatText = new java.awt.Label("                0",Label.RIGHT);
		heartbeatText.setBounds(113,5,55,25);
		add(heartbeatText);
		setTitle("Heart View");
		setResizable(false);
		//}}

		//{{INIT_MENUS
		//}}

		//{{REGISTER_LISTENERS
		SymWindow aSymWindow = new SymWindow();
		this.addWindowListener(aSymWindow);
		//}}
	}

	public HeartView(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, 150);
		}
		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.Label label1;
	java.awt.Label heartbeatText;
	//}}

	//{{DECLARE_MENUS
	//}}

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

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

	
	/** 
	 * Constructor that takes a model.  Calls the default
	 * constructor and then registers with the model in order
	 * to be notified on updates.
	 *
	 *@param model The model this view will observe.
	 */
	
	public HeartView(Observable model) {
		this();
		model.addObserver(this);
	}
	
	/**
	 * This is the method required by the Observer interface.
	 * This method will be called whenever the model calls
	 * notifyObservers().
	 *
	 *@param model The model that this object is watching.
	 *@param data The updated data object.
	 */
	public void update(Observable model, Object data) {
		 heartbeatText.setText("" + data);
	}
}

