import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;

/*
1.Socket »ý¼ºÀÚ¿¡ ¼­¹öÀÇIP¿Í ¼­¹öÀÇ µ¿ÀÛ Æ÷Æ®°ªÀ»(10001)À» ÀÎÀÚ·Î ³Ö¾î »ý¼ºÇÑ´Ù.
¼ÒÄÏÀÌ ¼º°øÀûÀ¸·Î »ý¼ºµÇ¾ú´Ù¸é ¼­¹ö¿Í Á¢¼ÓÀÌ ¼º°øÀûÀ¸·Î µÇ¾ú´Ù´Â °ÍÀ» ÀÇ¹ÌÇÑ´Ù.
2.»ý¼±µÈ socketÀ¸·ÎºÎÅÍ InputStream °ú OutputStreamÀ» ±¸ÇÑ´Ù.
3.InputStream ÀºBufferReader Çü½ÄÀ¸·Î º¯È¯ÇÏ°í OutputStreamÀº PrintWriter Çü½ÄÀ¸·Î º¯È¯ÇÑ´Ù.
4.Å°º¸µå·Î ºÎÅÍ ÇÑÁÙ ¾¿ ÀÔ·Â´Ý´Â BufferReader°´Ã¼¸¦ »ý¼ºÇÑ´Ù.
5.Å°º¸µå·ÎºÎÅÍ ÇÑÁÙÀ» ÀÔ·Â¹Þ¾Æ PrintWriter¿¡ ÀÖ´Â Printl()¸Þ¼Òµå¸¦ ÀÌ¿ëÇØ¼­ ¼­¹ö¿¡°Ô Àü¼ÛÇÑ´Ù.
6.¼­¹ö°¡ ´Ù½Ã ºÂÈ¯ÇÏ´Â ¹®ÀÚ¿­À» BufferReader¿¡ ÀÖ´Â readLine()¸Þ¼Òµå¸¦ ÀÌ¿ëÇØ¼­ ÀÐ¾î µéÀÎ´Ù.
ÀÐ¾îµéÀÎ ¹®ÀÚ¿­Àº È­¸é¿¡ Ãâ·ÂÇÑ´Ù.
7.4,5,6À» Å°º¸µå·ÎºÎÅÍ quit¹®ÀÚ¿­À» ÀÔ·Â¹ÞÀ» ¶§±îÁö ¹Ýº¹ÇÑ´Ù.
8.Å°º¸µå·ÎºÎÅÍ quit¹®ÀÚ¿­ÀÌ ÀÔ·ÂµÇ¸é IO°´Ã¼¿Í ¼ÒÄÏÀÇ close()¸Þ¼Òµå¸¦ È£ÃâÇÑ´Ù.
*/
public class ChattingClient 
{
	public static void main(String[] args) 
	{
		CreateChattingUI ccu =new CreateChattingUI();

		ccu.setSize(500,500);
		ccu.pack();
		ccu.setVisible(true);
	}
}

//Ã¤ÆÃ¹æUI ¸¸µå´Â Å¬·¡½º
class CreateChattingUI extends JFrame
{
	JPanel jp,bottom_jp;
	BorderLayout bl;
	static JTextArea top_jta;
	JTextArea member_jta;
	static	JTextField username_jtf,msg_jtf;
//	JComponent msg_jtf;
	JButton msgSend_jb;
	static Socket soc;

	public CreateChattingUI()
	{
		super("ÀÚ¹Ù·Î ¸¸µç Ã¤ÆÃ¹æ");
		//¼ÒÄÏÁ¢¼Ó
		try
		{
			soc = new Socket("127.0.0.1",10001);
			//InputStream in = new ByteArrayInputStream();

		}
		catch (ConnectException ce)
		{
			System.out.println("È£½ºÆ®¿Í ¿¬°á¾ÈµÊ");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}		

		this.setLayout(new BorderLayout());

		jp = new JPanel();
		bl = new BorderLayout(2,2);
		top_jta = new JTextArea();//¿ÞÂÊÀ§ ÅØ½ºÆ®Ã¢
		member_jta = new JTextArea(2,12);
		username_jtf = new JTextField("´ëÈ­¸í",10);
		msg_jtf = new JTextField("¸Þ¼¼Áö¸¦ ÀÔ·ÂÇÏ½Ã±æ ¹Ù¶ø´Ï´Ù.",42);
		bottom_jp = new JPanel(new FlowLayout());
		msgSend_jb = new JButton("¸Þ¼¼Áöº¸³»±â");

		top_jta.setEditable(false);
		top_jta.setFont(new Font("±¼¸²",Font.BOLD,12));
		
		//System.out.println(msg_jtf.isRequestFocusEnable());

		/*
		textarea¿¡ ½ºÅ©·Ñ ºÙÀÌ±â
		*/
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setPreferredSize(new Dimension(600,300));
		scrollPane.setBorder(BorderFactory.createTitledBorder(""));
		scrollPane.getViewport().add(top_jta, null);
		
		
		jp.setLayout(bl);
		jp.add(scrollPane,BorderLayout.WEST);

		jp.add(member_jta,BorderLayout.EAST);
		
		//msg_jtf.setSize(350,30);
		bottom_jp.add(username_jtf);
		bottom_jp.add(msg_jtf);
		bottom_jp.add(msgSend_jb);
		this.add(jp,BorderLayout.NORTH);
		this.add(bottom_jp,BorderLayout.SOUTH);
				
		//msg_jtf.addFocusListener((new EventManager()).focusGained(new FocusEvent()));
		/*
		ÅØ½ºÆ® ÇÊµå Å¬¸¯½Ã ÃÊ±âÈ­ ÀÌº¥Æ®
		*/
		
		msg_jtf.addFocusListener(new FocusAdapter()
		{
			public void focusGained(FocusEvent fe)
			{	
				JTextField tepJtf = (JTextField)fe.getSource();
				
				tepJtf.setText("");
				//System.out.println(((JTextField)fe.getSource()).getText());
			}
		});

		username_jtf.addFocusListener(new FocusAdapter()
		{
			public void focusGained(FocusEvent fe)
			{	
				JTextField tepJtf = (JTextField)fe.getSource();
				
				
				tepJtf.setText("");
				//System.out.println(((JTextField)fe.getSource()).getText());
			}
		});
		
		/*
		1.³»¿ëÀÔ·ÂÇÏ°í º¸³¾½Ã ¼­¹ö¿¡ Àü´ÞÇÑ´Ù.2¹ø
		2.ÀÚ±â ¸Þ¼¼Áö´Â ¹Ù·Î ÅØ½ºÆ®¿¡¾î¸®¾îÃ¢¿¡ Ã¹ÁÙ¿¡ ±â·ÏÇÑ´Ù.1¹ø
		3.±×¸®°í ´Ù¸¥ Å¬¶óÀÌ¾ðÆ® ¸Þ¼¼Áö ÀÔ·Â½Ã Ã¢¿¡ ¶ß°Ô ÇÑ´Ù.3¹ø
		*/
		
		ActionListener al = new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				//JTextField jtd = (JTextField)ae.getSource();
				
				String username = username_jtf.getText();
				String msg = msg_jtf.getText();
				
				if(username.equals("") || username.equals("´ëÈ­¸í"))
				{
					username = "¼Õ´Ô";
				}

				//username = (Font.getFont(username,new Font("¹ÙÅÁ",Font.ITALIC,12))).getFontName();
				top_jta.append(username+" : "+msg+"\n");
				//top_jta.setLineWrap(true);
				msg_jtf.setText("");

				sendMsg(username,msg,soc);
			}
		};
		msg_jtf.addActionListener(al);
		msgSend_jb.addActionListener(al);
	}
	
	static void sendMsg(String username,String msg,Socket soc)
	{
		InputStream in = null;
		OutputStream out = null;
		OutputStream out2 = null;
		PrintWriter pw = null;
		BufferedReader br = null;
		OutputStreamWriter osw = null;
		BufferedReader br2 = null;

		
		
		try
		{
			//in = new ByteArrayInputStream(msg.getBytes());
			//in = 
			//System.out.println("11111111111111111");
			
			if(soc!=null)
			{
				//System.out.println("2222222222222");
				in = soc.getInputStream();
				out = soc.getOutputStream();
				
//				out2 = new ByteArrayOutputStream();
//				out2.write(msg.getBytes());
				//InputStream in2 = new InputStream();
				//br2 = new BufferedReader(new InputStreamReader(in));

				//System.out.println(msg.getBytes());
				
				br = new BufferedReader(new InputStreamReader(in));
				pw = new PrintWriter(new OutputStreamWriter(out));
				pw.println(msg.trim());
				pw.flush();
				String line=null;
				
				//System.out.println("11111111");
				//System.out.println("ÁÙ : "+out2.readLine());
				
				//System.out.println(out2.readLine());
				
				while((line=br.readLine())!=null)
				{
					//pw.println(line);
					//pw.flush();
					
					//String echo = br.readLine();
					System.out.println(line);
					//System.out.println(msg);
					//System.out.println("¼­¹ö : "+line);
				}
				
				//pw.flush();
			}

		}
		catch (Exception ioe)
		{
			ioe.printStackTrace();
		}
		finally
		{

			try
			{
				if(br!=null)
					br.close();
				if(soc!=null)
				{
					soc.close();
				}
			}                                                                                                                                                        
			catch (IOException ioe)
			{

			}
				
		}
	}
}
