

/*
 * HeartBeatMain
 *
 * The driver for the application and applet.  The applet just waits
 * for the user to hit the button before calling the main function,
 * (and esentially becoming an application).
 *
 * Actually, I decided to make the Applet another view - a view that
 * just plays a sound in response to the updates...enjoy.
 *
 * Again, much of this was generated by Visual Cafe - my code comes
 * at the end of the file.
 *
 *@author <a href="http://www.cs.indiana.edu/~cbaray/">cristobal baray</a> cbaray@cs.indiana.edu  
 */

import java.awt.*;
import java.applet.*;

import java.net.URL;
import java.net.MalformedURLException;

import java.util.Observer;
import java.util.Observable;

public class HeartBeatMain 
	extends Applet
	implements Observer
	
{
	public void init()
	{
		// Take out this line if you don't use symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller
		symantec.itools.lang.Context.setApplet(this);
	
		// 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.CENTER,5,5));
		setSize(186,40);
		button1 = new java.awt.Button();
		button1.setLabel("start heart beat app");
		button1.setBounds(21,5,144,20);
		add(button1);
		//}}
	
		//{{REGISTER_LISTENERS
		SymAction lSymAction = new SymAction();
		button1.addActionListener(lSymAction);
		//}}
	}
	
	//{{DECLARE_CONTROLS
	java.awt.Button button1;
	//}}

	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == button1)
				button1_ActionPerformed(event);
		}
	}




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




	/**
	 * When the user wants to start the app up, use the main function,
	 * then add this applet as an observer to the heartbeat.
	 *
	 */
	void button1_ActionPerformed(java.awt.event.ActionEvent event)
	{
		main(null);

		theHeart.addObserver(this);
		
	}
	
	/**
	 * The update method for this View - it will take advantage of
	 * it's Java 1.1.x applet status and play a sound file.  I couldn't
	 * find a good EKG sound, so pretend it is the Tell Tale Heart...
	 * If the sound hasn't been loaded, load it.
	 *
	 *@param model The model that this object is watching.
	 *@param data The updated data object.
	 */
	public void update(Observable model, Object data) {
		
		if (soundClip == null) {
			soundClip = getAudioClip(getCodeBase(), "click.au");
		}
			
		if (soundClip != null) {
			soundClip.play();		
		}
	}
	
	/**
	 * Acts as a controller as well - to stop the heart from continuing
	 * to beat after we leave the page.
	 *
	 */
	public void stop() {
		theHeart.stopHeart();	
	}
	
	/** The soundclip to play */
	private static AudioClip soundClip;
	
	
	/** the heartbeat that we'll be monitoring */
	private static HeartBeat theHeart;	



	/**
	 * The driver for the program - is absorbed into the 
	 * applet class to make things easier.  To create an MVC
	 * application, generally one creates a Model, then attaches
	 * Views and Controllers to it.  Any number of Views and
	 * Controllers can be added...
	 *
	 */
	
	public static void main(String args[]) {

		theHeart = new HeartBeat();
		
		new HeartView(theHeart).setVisible(true);
		new HeartController(theHeart).setVisible(true);
		
	}	
	
}

