/**
 * 
 * OrChatApp sets up a graphical space for an
 * OrChat client and initiates the connection
 * to the server using the inner class OrChatClient
 *
 * @see	OrChatServer
 * @version	1.0
 * @author	James McCabe
 */

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import java.net.*;
import java.util.*;
import com.supercede.forms.*;

public class OrChatApp extends SuperCedeFrame implements Serializable
{
	OrChat.OrChatServerI server = null;
	String username = null;

	void orChatAppPreInit()
	{
		// You can add code anywhere in this method.
		// This method is called PRIOR to form initialization.
		// NOTE: The form has NOT been initialized yet. Do not modify the form itself in this method.
	}

	void orChatAppPostInit()
	{
		// You can add code anywhere in this method.
		// This method is called AFTER form initialization.
	}

	/**
	 Initializes the ORB
	 and connects to the server
	 */
	public void initConnection(String username) {	 

	 	try {
	 		// create the object request broker
	 		// and a base object adapter.
			System.out.println("initConnection begin " + this.username);
	 		org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init();
	 		org.omg.CORBA.BOA boa = orb.BOA_init();
			System.out.println("initConnection after init");

			server = (OrChat.OrChatServerI) OrChat.OrChatServerIHelper.bind(orb,"oranda");
			System.out.println("new client");
 		
	 		OrChatClient client = new OrChatClient(this.username);             
            		boa.obj_is_ready(client);	
			System.out.println("obj is ready");
//			server.addClient(client);	
			server.addClient(client, this.username);	

			System.out.println("client: addClient username" + this.username);

			server.sendMessage(this.username + " has logged on");		
	 	} catch (Exception e) {
	 		e.printStackTrace();
	 	} 
		System.out.println("initConnection end");
	}

	// CORBA functions 
	
	/**
	 * send a message to the server
	 */
	public void sendMessage() {	
		server.sendMessage(this.username + ": " + textField1.getText());
		System.out.println("sendMessage");
	}



	
	public static void main(String args[])
	{
		// You can add code anywhere in this method.

		try
		{
			OrChatApp app = new OrChatApp(args[0]);
			app.setVisible(true);
			app.textAreaSB1.append("Welcome to Oranda Chat.\n");
		       app.textAreaSB1.append("Type your messages in the lower box." + "\n\n");
			app.initConnection(args[0]);
		}
		catch(Throwable t)
		{
			System.out.println("Cannot construct the form: " + t);
			System.exit(1);
		}
	}

	public class OrChatClient extends OrChat._OrChatClientIImplBase
	{
		String username;

		public OrChatClient(String username) {
			super();
			this.username = username;
		}
		/**
		 * receive a message from the server
	 	 */
		public void receiveMessage(String message) {
			System.out.println("received");
			textAreaSB1.append(message + "\n");
		}
	}



	public boolean orChatAppWindowClosing(WindowEvent arg0)
	{
		// Put event handler code here. Return false for normal processing, true to override

		return false;
	}

	public boolean orChatAppWindowClosed(WindowEvent arg0)
	{
		// Put event handler code here. Return false for normal processing, true to override

		return false;
	}

	public void button1MouseReleased(MouseEvent arg0)
	{
		// Put event handler code here...
		//textAreaSB1.append(textField1.getText());
		sendMessage();
	}

	// SuperCede Begin 2.0 Form Members
	// Do not remove the Begin and End markers.
	// The editor will rewrite the contents of this section each time the form is saved.

	// References to Beans within the Form.

	com.supercede.beans.stdawt.TextAreaSB textAreaSB1;
	Button button1;
	TextField textField1;
	Label label1;

	private void SuperCedeConstructor() throws IOException, ClassNotFoundException, ClassCastException, SuperCedeInvalidStateException
	{
		// Construct the actual connectors to give to our base class.

		Vector connectors = new Vector(3);
		connectors.addElement(new OrChatAppEventConnector2(this));
		connectors.addElement(new OrChatAppWindowClosingConnector3(this));
		connectors.addElement(new OrChatAppWindowClosedConnector4(this));

		super.initializeThis(connectors);

		// Make references to Beans within the Form.

		int i = 0;

		textAreaSB1 = (com.supercede.beans.stdawt.TextAreaSB) getComponent(i++);
		button1 = (Button) getComponent(i++);
		textField1 = (TextField) getComponent(i++);
		label1 = (Label) getComponent(i++);
	}

	public OrChatApp(String username) throws IOException, ClassNotFoundException, ClassCastException, SuperCedeInvalidStateException
	{
		super();
		this.username = username;
		orChatAppPreInit();
		SuperCedeConstructor();
		orChatAppPostInit();
	}

	public void SuperCedeWindowClosing(WindowEvent arg0)
	{
		if (orChatAppWindowClosing(arg0) == false)
		{
			dispose();
		}
	}

	public void SuperCedeWindowClosed(WindowEvent arg0)
	{
		if (orChatAppWindowClosed(arg0) == false)
		{
			System.exit(0);
		}
	}

	// SuperCede End 2.0 Form Members
}

// SuperCede Begin 2.0 Form Connectors
// Do not remove the Begin and End markers.
// The editor will rewrite the contents of this section each time the form is saved.
// Connections for OrChatApp:
//	OrChatAppEventConnector2: from button1.mouseReleased to OrChatApp.button1MouseReleased
//	OrChatAppWindowClosingConnector3: from OrChatApp.windowClosing to OrChatApp.SuperCedeWindowClosing
//	OrChatAppWindowClosedConnector4: from OrChatApp.windowClosed to OrChatApp.SuperCedeWindowClosed

final class OrChatAppEventConnector2 extends MouseAdapter implements SuperCedeConnector, Serializable
{
	private OrChatApp target;

	public OrChatAppEventConnector2(OrChatApp target)
	{
		this.target = target;
	}

	public void mouseReleased(MouseEvent arg0)
	{
		target.button1MouseReleased(arg0);
	}
}

final class OrChatAppWindowClosingConnector3 extends WindowAdapter implements SuperCedeConnector, Serializable
{
	private OrChatApp target;

	public OrChatAppWindowClosingConnector3(OrChatApp target)
	{
		this.target = target;
	}

	public void windowClosing(WindowEvent arg0)
	{
		target.SuperCedeWindowClosing(arg0);
	}
}

final class OrChatAppWindowClosedConnector4 extends WindowAdapter implements SuperCedeConnector, Serializable
{
	private OrChatApp target;

	public OrChatAppWindowClosedConnector4(OrChatApp target)
	{
		this.target = target;
	}

	public void windowClosed(WindowEvent arg0)
	{
		target.SuperCedeWindowClosed(arg0);
	}
}

// The following line must be the last line in the file.
// SuperCede End 2.0 Form Connectors