
import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

/**
 * 客户端窗体
 */
public class ClientFrame extends JFrame {

	private JPanel contentPane;
	private JLabel lblUserName;// 显示用户名
	private JTextField tfMessage; // 信息发送文本框
	private JButton btnSend;// 发送按钮
	private JTextArea textArea;// 信息接收文本域
	private String userName;// 用户名称
	private ChatRoomClient client;// 客户端连接对象

	/**
	 * 客户端窗体的构造方法
	 * 
	 * @param ip
	 *            服务器IP地址
	 * @param userName
	 *            用户名
	 */
	public ClientFrame(String ip, String userName) {
		this.userName = userName;

		try {
			client = new ChatRoomClient(ip, 4569);// 创建客户端对象
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		ReadMessageThread messageThread = new ReadMessageThread();// 创建读取客户端消息的线程类对象
		messageThread.start();// 启动读取客户端消息的线程类对象

		init();// 添加窗体初始化内容
		addListener();// 添加窗体监听内容

	}

	private void init() {
		setTitle("客户端");
		setResizable(false);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(5, 5, 434, 229);
		contentPane.add(scrollPane);

		textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		textArea.setEditable(false);// 文本域不可编辑

		JPanel panel = new JPanel();
		panel.setBounds(5, 235, 434, 32);
		contentPane.add(panel);
		panel.setLayout(null);

		lblUserName = new JLabel(userName);
		lblUserName.setHorizontalAlignment(SwingConstants.TRAILING);
		lblUserName.setBounds(2, 4, 55, 22);
		panel.add(lblUserName);

		tfMessage = new JTextField();
		tfMessage.setBounds(62, 5, 274, 22);
		tfMessage.setColumns(10);
		panel.add(tfMessage);

		btnSend = new JButton("发送");
		btnSend.setBounds(336, 4, 93, 23);
		panel.add(btnSend);
		
		tfMessage.validate();
	}

	private void addListener() {
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Date date = new Date();// 创建时间类
				SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// 设定日期格式
				client.sendMessage(userName + "  " + df.format(date) + "：\n   " + tfMessage.getText());// 向服务器发送文本内容
				tfMessage.setText("");// 输入框为空
			}
		});
		this.addWindowListener(new WindowAdapter() {// 开启窗口监听
			public void windowClosing(WindowEvent atg0) {// 窗口关闭时
				int op = JOptionPane.showConfirmDialog(ClientFrame.this, "确定要退出聊天室吗？", "确定", JOptionPane.YES_NO_OPTION);// 弹出提示框
				if (op == JOptionPane.YES_OPTION) {// 如果选择是
					client.sendMessage("%EXIT%:" + userName);// 发送消息
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					client.close();// 关闭客户端连接
					System.exit(0);// 关闭程序
				}
			}
		});
	}

	/**
	 * 读取客户端消息的线程类
	 */
	private class ReadMessageThread extends Thread {
		public void run() {// 线程主方法
			while (true) {// 无限循环
				String str = client.reciveMessage();// 客户端收到服务器发来的文本内容
				textArea.append(str + "\n");// 向文本框添加文本内容
			}
		}
	}
}
