/*
 * TalkGUI.java
 *
 * Author: Jonathan Boldiga
 * Date: July 22, 2002
 *
 * Description: This is the Talk screen for ORBChat, a CORBA based chat program.
 *				This class uses SWING and implements a method called addText() which
 *				grabs the host name from the text field and sends it to the client to
 *				initialize a chat session. To run ORBChat, execute the Client.java class file.
 *
 * Copyright (c) 1993-2001 IONA Technologies PLC.
 *  			All Rights Reserved
 *
*/

package Chat;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import org.omg.CORBA.*;


public class TalkGUI extends JFrame{

	Chat.ChatRemote cr;
	protected JTextField userText;
	protected JLabel userLabel;
	protected JButton talkButton;
	protected String user;
    protected String newline = "\n";

	public TalkGUI(Chat.ChatRemote cRemote, String currentUser){

		//Do frame stuff.
		super("Talk");
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				closeMe();
			}
		});

		//CORBA objects
		cr = cRemote;
		user = currentUser;

		JPanel contentPane = new JPanel();
		contentPane.setLayout(new GridBagLayout());
		contentPane.setBackground(UIManager.getColor("control"));
		GridBagConstraints constraints = new GridBagConstraints();

		constraints.gridx = 0;
		constraints.gridy = GridBagConstraints.RELATIVE;
		constraints.gridwidth = 1;
		constraints.gridheight = 1;
		constraints.insets = new Insets(2, 2, 2, 2);
		constraints.anchor = GridBagConstraints.EAST;

		contentPane.add(userLabel = new JLabel("Connect To:", SwingConstants.RIGHT), constraints);
		contentPane.add(talkButton = new JButton("Chat"), constraints);

		constraints.gridx = 1;
		constraints.gridy = 0;
		constraints.weightx = 1.0;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.anchor = GridBagConstraints.CENTER;

		contentPane.add(userText = new JTextField(20), constraints);
		constraints.gridx = 1;
		constraints.gridy = GridBagConstraints.RELATIVE;
		constraints.fill = GridBagConstraints.NONE;
		setContentPane(contentPane);
		addText(userText);


	}

	public void addText(final JTextField textF){
		talkButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String dest = textF.getText();
				textF.setText("");
				if(!(cr.available(dest))){
					cr.request(user, dest);
					ClientGUI.destination(dest);
				}
				else { JOptionPane.showMessageDialog(new JFrame(), dest + " is not available"); }
				closeMe();
			}
	    });
	}

	public void closeMe(){
		setVisible(false);
		dispose();
	}

}