//******************************************************************************
// javahelloauclient.java:	Applet
//
//******************************************************************************
import java.applet.*;
import java.awt.*;
import javahelloauclientFrame;

// import the classes from the type library

import hello32.*;
import com.ms.com.* ;

//==============================================================================
// Main Class for applet javahelloauclient
//
//==============================================================================
public class javahelloauclient extends Applet
{
	// the interface to the COM object

	IDHello m_hello ;


	// user interface components

	TextField m_HelloMessageText ;
	Button m_SetMessageButton ;
	Button m_GetMessageButton ;
	Button m_SayHelloButton ;


	// STANDALONE APPLICATION SUPPORT:
	//		m_fStandAlone will be set to true if applet is run standalone
	//--------------------------------------------------------------------------
	private boolean m_fStandAlone = false;

	// STANDALONE APPLICATION SUPPORT
	// 	The main() method acts as the applet's entry point when it is run
	// as a standalone application. It is ignored if the applet is run from
	// within an HTML page.
	//--------------------------------------------------------------------------
	public static void main(String args[])
	{
		// Create Toplevel Window to contain applet javahelloauclient
		//----------------------------------------------------------------------
		javahelloauclientFrame frame = new javahelloauclientFrame("javahelloauclient");

		// Must show Frame before we size it so insets() will return valid values
		//----------------------------------------------------------------------
		frame.show();
        frame.hide();
		frame.resize(frame.insets().left + frame.insets().right  + 320,
					 frame.insets().top  + frame.insets().bottom + 240);

		// The following code starts the applet running within the frame window.
		// It also calls GetParameters() to retrieve parameter values from the
		// command line, and sets m_fStandAlone to true to prevent init() from
		// trying to get them from the HTML page.
		//----------------------------------------------------------------------
		javahelloauclient applet_javahelloauclient = new javahelloauclient();

		frame.add("Center", applet_javahelloauclient);
		applet_javahelloauclient.m_fStandAlone = true;
		applet_javahelloauclient.init();
		applet_javahelloauclient.start();
        frame.show();
	}

	// javahelloauclient Class Constructor
	//--------------------------------------------------------------------------
	public javahelloauclient()
	{
		// TODO: Add constructor code here
	}

	// APPLET INFO SUPPORT:
	//		The getAppletInfo() method returns a string describing the applet's
	// author, copyright date, or miscellaneous information.
    //--------------------------------------------------------------------------
	public String getAppletInfo()
	{
		return "Name: javahelloauclient\r\n" +
		       "Author: David S. Platt\r\n" +
		       "Created with Microsoft Visual J++ Version 1.1";
	}


	// The init() method is called by the AWT when an applet is first loaded or
	// reloaded.  Override this method to perform whatever initialization your
	// applet needs, such as initializing data structures, loading images or
	// fonts, creating frame windows, setting the layout manager, or adding UI
	// components.
    //--------------------------------------------------------------------------
	public void init()
	{
        // If you use a ResourceWizard-generated "control creator" class to
        // arrange controls in your applet, you may want to call its
        // CreateControls() method from within this method. Remove the following
        // call to resize() before adding the call to CreateControls();
        // CreateControls() does its own resizing.
        //----------------------------------------------------------------------
    	resize(640, 480);

		// Place the components in the applet

		m_HelloMessageText = new TextField (20) ;

		m_SetMessageButton = new Button("Set String") ;
		m_GetMessageButton = new Button("Get String") ;
		m_SayHelloButton = new Button("Say Hello") ;

		add(m_SetMessageButton);
		add(m_GetMessageButton);
		add(m_SayHelloButton);
		add(m_HelloMessageText) ;

	}

	// Place additional applet clean up code here.  destroy() is called when
	// when you applet is terminating and being unloaded.
	//-------------------------------------------------------------------------
	public void destroy()
	{
	}

	// javahelloauclient Paint Handler
	//--------------------------------------------------------------------------
	public void paint(Graphics g)
	{
		g.drawString("Essence of COM and ActiveX Java Automation Client Demo", 10, 70);
	}

	//		The start() method is called when the page containing the applet
	// first appears on the screen. The AppletWizard's initial implementation
	// of this method starts execution of the applet's thread.
	//--------------------------------------------------------------------------
	public void start()
	{
		// Create the COM object

		m_hello = (IDHello) new HelloApplication ( );
	}
	
	//		The stop() method is called when the page containing the applet is
	// no longer on the screen. The AppletWizard's initial implementation of
	// this method stops execution of the applet's thread.
	//--------------------------------------------------------------------------
	public void stop()
	{
		m_hello = null ;
	}

	// Handle events from the components in the applet
	public boolean handleEvent(Event evt)
	{
		// Event from the button

		if(evt.target == m_SayHelloButton)
		{
			m_hello.SayHello ( ) ;
			return true;
		}
		else if (evt.target == m_SetMessageButton) 
		{
			m_hello.putHelloMessage(m_HelloMessageText.getText()) ;
			return true;
		}
		else if (evt.target == m_GetMessageButton)
		{
			try
			{
				m_HelloMessageText.setText (m_hello.getHelloMessage()) ;
			}
			catch (ComFailException cfe)
			{
				System.out.println( "COM Exception:" );
				System.out.println( cfe.getHResult() );
				System.out.println( cfe.getMessage() );
			}
			return true;
		}

		// Otherwise, pass the event on to super
		return false;
	}



	// TODO: Place additional applet code here

}
