/*
 * TalkGUI.java
 *
 * Author: Jonathan Boldiga
 * Date: July 22, 2002
 *
 * Description: This is the Kick screen for the ORBChat moderator. 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 Kick extends JFrame{

	Chat.ChatRemote cr;
	protected JTextField userText;
	protected JLabel userLabel;
	protected JButton kickButton;
	protected String user = "ORBChat Server";
    protected String newline = "\n";

	public Kick(Chat.ChatRemote cRemote){

		//Do frame stuff.
		super("Kick");
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				closeMe();
			}
		});

		//CORBA objects
		cr = cRemote;

		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("Kick Client:", SwingConstants.RIGHT), constraints);
		contentPane.add(kickButton = new JButton("Kick"), 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){
		kickButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String dest = textF.getText();
				textF.setText("");
				if(!(cr.available(dest))){
					cr.remove(dest);
					closeMe();
				}
				else {
					JOptionPane.showMessageDialog(new JFrame(), dest + " is not available");
					closeMe();
				}
			}
	    });
	}

	public void closeMe(){
		setVisible(false);
		dispose();
	}

}