import com.ms.wfc.app.*;
import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;
import com.ms.wfc.html.*;

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

/**
 * This class can take a variable number of parameters on the command
 * line. Program execution begins with the main() method. The class
 * constructor is not invoked unless an object of type 'Form1' is
 * created in the main() method.
 */
public class Form1 extends Form
{

/*
This member variable represents the interface we use to control
the client app.
*/
	
	IDHello m_Hello ;

/*
"Create" button clicked. Create a new HelloApplication object,
which is Java's representation of an abstract connection to the
server. Assign it into an object which represents the interface
so we can actually access its methods.
*/

	private void btn_Create_click(Object source, Event e)
	{
		try
		{
			m_Hello = new HelloApplication() ;
		}
		catch (com.ms.com.ComError ce)
		{
			MessageBox.show  (ce.getMessage(), "create Exception") ;
		}
	}

/*
"Release" button clicked. Set the interface pointer to null. The
external object will be released and the server shut down at the
next system garbage collection.
*/	
	private void btn_Release_click(Object source, Event e)
	{
		m_Hello = null ;		
	}

/*
"System.gc" button clicked. Force an immediate system garbage
collection which will release any unused objects.
*/
	private void btn_SystemGc_click(Object source, Event e)
	{
		System.gc( ) ;
	}

/*
"SayHello" button clicked. Call that method on the object.
*/
	private void btn_SayHello_click(Object source, Event e)
	{
		try
		{
			m_Hello.SayHello() ;
		}
		catch (ComFailException cfe)
		{
			MessageBox.show (cfe.getMessage(), "SayHello") ;	
		}
	}

/*
"Set" button clicked. Get the string from the edit control and set
its value into the object's "HelloMessage" property.
*/
	private void btn_SetProp_click(Object source, Event e)
	{
		m_Hello.setHelloMessage (edit1.getText()) ;
	}

/*
"Get" button clicked. Get the value of the "HelloMessage" property
and display it in the edit control.
*/
	private void btn_GetProp_click(Object source, Event e)
	{
		edit1.setText (m_Hello.getHelloMessage( )) ;
	}

/*
User has clicked the ComLib.release( ) button. Call that library
function to force an immediate release of the object without doing
a full system garbage collection. 
*/

	private void btn_ComLibRelease_click(Object source, Event e)
	{
		ComLib.release(m_Hello) ;
	}

/* 
"Bad QI" button clicked. Intentionally query for an interface that
we know the object doesn't support. The JVM will map the COM error code
E_NOINTERFACE into a Java exception of class java.lang.ClassCastException.
We trap the exception and pop up a message box to report it to the user.
*/
	
	private void btn_BadQI_click(Object source, Event e)
	{
		try
		{
			IEnumVARIANT m_enum ;
			m_enum = (IEnumVARIANT) m_Hello ;
		}
		catch (ClassCastException ex)
		{
			MessageBox.show (ex.getMessage(), "you idiot") ;	
		}
	}

	
	public Form1()
	{
		// Required for Visual J++ Form Designer support
		initForm();		

		// TODO: Add any constructor code after initForm call
	}

	/**
	 * Form1 overrides dispose so it can clean up the
	 * component list.
	 */
	public void dispose()
	{
		super.dispose();
		components.dispose();
	}


	/**
	 * NOTE: The following code is required by the Visual J++ form
	 * designer.  It can be modified using the form editor.  Do not
	 * modify it using the code editor.
	 */
	Container components = new Container();
	Edit edit1 = new Edit();
	Label label1 = new Label();
	Button btn_Create = new Button();
	Button btn_Release = new Button();
	Button btn_SystemGc = new Button();
	Button btn_SayHello = new Button();
	Button btn_SetProp = new Button();
	Button btn_GetProp = new Button();
	Button btn_ComLibRelease = new Button();
	Button btn_BadQI = new Button();

	private void initForm()
	{
		this.setText("Essence of COM Java Client");
		this.setAutoScaleBaseSize(new Point(5, 13));
		this.setClientSize(new Point(336, 272));

		edit1.setLocation(new Point(24, 32));
		edit1.setSize(new Point(288, 20));
		edit1.setTabIndex(0);
		edit1.setText("");

		label1.setLocation(new Point(24, 8));
		label1.setSize(new Point(248, 24));
		label1.setTabIndex(1);
		label1.setTabStop(false);
		label1.setText("Value of \'HelloMessage\' Property");

		btn_Create.setLocation(new Point(24, 72));
		btn_Create.setSize(new Point(80, 32));
		btn_Create.setTabIndex(2);
		btn_Create.setText("Create");
		btn_Create.addOnClick(new EventHandler(this.btn_Create_click));

		btn_Release.setLocation(new Point(128, 72));
		btn_Release.setSize(new Point(80, 32));
		btn_Release.setTabIndex(3);
		btn_Release.setText("Release");
		btn_Release.addOnClick(new EventHandler(this.btn_Release_click));

		btn_SystemGc.setLocation(new Point(232, 72));
		btn_SystemGc.setSize(new Point(80, 32));
		btn_SystemGc.setTabIndex(4);
		btn_SystemGc.setText("System.gc()");
		btn_SystemGc.addOnClick(new EventHandler(this.btn_SystemGc_click));

		btn_SayHello.setLocation(new Point(24, 128));
		btn_SayHello.setSize(new Point(80, 32));
		btn_SayHello.setTabIndex(5);
		btn_SayHello.setText("SayHello");
		btn_SayHello.addOnClick(new EventHandler(this.btn_SayHello_click));

		btn_SetProp.setLocation(new Point(128, 128));
		btn_SetProp.setSize(new Point(80, 32));
		btn_SetProp.setTabIndex(6);
		btn_SetProp.setText("Set Prop");
		btn_SetProp.addOnClick(new EventHandler(this.btn_SetProp_click));

		btn_GetProp.setLocation(new Point(232, 128));
		btn_GetProp.setSize(new Point(80, 32));
		btn_GetProp.setTabIndex(7);
		btn_GetProp.setText("Get Prop");
		btn_GetProp.addOnClick(new EventHandler(this.btn_GetProp_click));

		btn_ComLibRelease.setLocation(new Point(24, 184));
		btn_ComLibRelease.setSize(new Point(80, 32));
		btn_ComLibRelease.setTabIndex(8);
		btn_ComLibRelease.setText("ComLib.release");
		btn_ComLibRelease.addOnClick(new EventHandler(this.btn_ComLibRelease_click));

		btn_BadQI.setLocation(new Point(128, 184));
		btn_BadQI.setSize(new Point(80, 32));
		btn_BadQI.setTabIndex(9);
		btn_BadQI.setText("Bad QI");
		btn_BadQI.addOnClick(new EventHandler(this.btn_BadQI_click));

		this.setNewControls(new Control[] {
							btn_BadQI, 
							btn_ComLibRelease, 
							btn_GetProp, 
							btn_SetProp, 
							btn_SayHello, 
							btn_SystemGc, 
							btn_Release, 
							btn_Create, 
							label1, 
							edit1});
	}

	/**
	 * The main entry point for the application. 
	 *
	 * @param args Array of parameters passed to the application
	 * via the command line.
	 */
	public static void main(String args[])
	{
		Application.run(new Form1());
	}
}
