import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.io.*;
import java.awt.Window.*;
import java.net.*;

class clientchat extends JFrame implements ActionListener,Runnable

{
		Color color = Color.blue;
    String s,tete,tete1;
    ServerSocket Sserver;
	Socket SSoket;
	BufferedReader br;
	BufferedWriter bw;
		JScrollPane scrolli;
	//------------panel----------
	JPanel panel = new JPanel();
	JPanel panel2 = new JPanel();
	//------------TextArea-------
	List  ta = new List();
	
	JTextArea tfiled = new JTextArea(3,30);
	//------------ScrollBar-------
	JScrollPane sp = new JScrollPane(ta);
	JScrollPane sp1 = new JScrollPane(tfiled);
	//------------TollBar----------
	JToolBar tools = new JToolBar();

	ImageIcon asave1 =new ImageIcon("pic/save.gif");
	JButton save1 = new JButton(asave1); 
	
ImageIcon tsize =new ImageIcon("pic/bullets.gif");
	JButton tsize1 = new JButton(tsize); 
	
	ImageIcon acolor1 =new ImageIcon("pic/fg.gif");
	JButton color1 = new JButton(acolor1);
	
	JButton gemy =new JButton("Gemy Color");
	ImageIcon psend1 =new ImageIcon("pic/se.gif");
	JButton psend =new JButton(psend1);
	
		ImageIcon ghd =new ImageIcon("pic/1.gif");
		JLabel ghd1=new JLabel (ghd);
	
			ImageIcon ghdd =new ImageIcon("pic/2.gif");
		JLabel ghd2=new JLabel (ghdd);
		
				ImageIcon ghddd =new ImageIcon("pic/3.jpg");
		JLabel ghd3=new JLabel (ghddd);
		
				ImageIcon ghdddd =new ImageIcon("pic/4.jpg");
		JLabel ghd4=new JLabel (ghdddd);
		
				ImageIcon ghddddd =new ImageIcon("pic/5.jpg");
		JLabel ghd5=new JLabel (ghddddd);	
	
		//============MenuBar=========
	JMenuBar menu = new JMenuBar();
	//===========File Menu========
	JMenu file = new JMenu("File");
	//----------MenuItem-------


	JMenuItem save = new JMenuItem("Save");

	JMenuItem exit = new JMenuItem("Exit");
	
	
	//=========Font Menu========
	JMenu font = new JMenu("Font");
	//----------MenuItem-------
	JMenuItem size = new JMenuItem("Size");
	JMenuItem fcolor = new JMenuItem("Font Color");
	
	JMenu help = new JMenu("Help");
	//----------MenuItem-------
	JMenuItem about = new JMenuItem("About");
	
	//------------font size
	
	JMenu fontss = new JMenu("Font Size");
	//----------MenuItem-------
	JMenuItem size12 = new JMenuItem("12");
	JMenuItem size14 = new JMenuItem("14");
	JMenuItem size16 = new JMenuItem("16");
//	JMenuItem size18 = new JMenuItem("18");
//	JMenuItem size20 = new JMenuItem("20");
//	JMenuItem size22 = new JMenuItem("22");
//	JMenuItem size24 = new JMenuItem("24");
//	JMenuItem size26 = new JMenuItem("26");
	//-------------------End Menu-----------------
	public void run()
	{
		try{SSoket.setSoTimeout(1);}catch(Exception e){}
		while (true)
		{
			try{
			ta.addItem(br.readLine());
			}catch (Exception h){}
		}
	}
	
	
	clientchat(String nn)
	{
		super (nn);
	
		setupGui();
		psend.addActionListener(this);

			addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
	});
	try{
			
			
			SSoket= new Socket("127.0.0.1",100);
			br = new BufferedReader(new InputStreamReader(
					SSoket.getInputStream()));
			bw = new BufferedWriter(new OutputStreamWriter(
					SSoket.getOutputStream()));
		/*	bw.write("Hello");*/bw.newLine();bw.flush();
			 Thread th;
			 th = new Thread(this);
			 th.start();
			 
			
		}catch(Exception e){}
	}
	public void actionPerformed ( ActionEvent e)
	{
		 /*if (e.getSource().equals(button2))
			 System.exit(0);
		 else{*/try{
			 bw.write(tfiled.getText());
			 String mywrite=tfiled.getText();
			 ta.addItem(mywrite);
			 System.out.println(mywrite);
			  fileex s=new  fileex();
			 bw.newLine();bw.flush();
			 tfiled.setText("");
			 }catch(Exception m){}
		 
				  
	}

	
	
		private void setupGui()
	{
		//ta.disable();
		
	
		//============container=============
		Container c = getContentPane();
		
		
		c.setLayout(new BorderLayout());
		panel.setLayout(new BorderLayout());
		 scrolli=new JScrollPane( );
		 scrolli.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		//===============Menu Items============
		menu.add(file);
		
		menu.add(font);
		menu.add(fontss);
		menu.add(help);
	//------------file-------------------	
	
		file.add(save);
		
		file.add(exit);
	//666666666666666666666666
	fontss.add(size12);
	fontss.add(size14);
	fontss.add(size16);
//	fontss.add(size18);
//	fontss.add(size20);
//	fontss.add(size22);
//	fontss.add(size24);
//	fontss.add(size26);
	
		//------------font------------------
		font.add(size);
		font.add(fcolor);
				//------------help------------------
		help.add(about);
		//------------tools------------------
		tools.add(save1);

		tools.add(color1);
		tools.add(gemy);
		
		ghd1.setSize(2,2);
		tools.add(ghd1);
		tools.add(ghd2);
		tools.add(ghd3);
		tools.add(ghd4);
		tools.add(ghd5);
		ta.disable();
		
		
		//****************************************************
		
		//*******************************fffffffff
			size12.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				tfiled.setFont(new Font("Tahoma",Font.ITALIC,12));
				ta.setFont(new Font("Tahoma",Font.ITALIC,12));
				//ta.clear();
				}
			});	
			
				size14.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				tfiled.setFont(new Font("Tahoma",Font.ITALIC,14));
				ta.setFont(new Font("Tahoma",Font.ITALIC,14));
				}
			});	
		//***************FFFFFFFFFFFFFFFFFFFFFFF
		
		
about.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JOptionPane.showInternalConfirmDialog(null,"");
				}
			});
			
			color1.addActionListener(new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
               color =
                  JColorChooser.showDialog( clientchat.this,
                     "Choose a color", color );

               if ( color == null )
                  color = Color.lightGray;

              
               tfiled.setForeground(color);
               tfiled.repaint();
            }
         }
      );
      
      fcolor.addActionListener(new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
               color =
                  JColorChooser.showDialog( clientchat.this,
                     "Choose a color", color );

               if ( color == null )
                  color = Color.lightGray;

              
               tfiled.setForeground(color);
               tfiled.repaint();
            }
         }
      );
      //tool.getDefaultToolkit();
		exit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JOptionPane.showConfirmDialog(null,"Do you really want to exit!");
			System.exit(0);
				}
			});
	tsize1.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				tfiled.setFont(new Font("Tahoma",Font.ITALIC,14));
				}
			});		
	gemy.setBounds(500,500,70,30);
   
    
    
    s= gemy.getActionCommand();
    gemy.addActionListener(new ActionListener()
{
	 public void actionPerformed(ActionEvent e)
      {
      	if( s.equals("Gemy Color"))
	ta.setBackground(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
}	
	});	

	
		save1.addActionListener(new ActionListener() 
			{		
	   public void actionPerformed(ActionEvent e) {
		JFileChooser chooser= new JFileChooser();
	    Frame frame = new Frame();
	   
        FileDialog fileDialog = new FileDialog(frame);
	    
	    fileDialog.setMode(FileDialog.SAVE);
	    fileDialog.show();
	    tete=fileDialog.getDirectory();
	    tete1=fileDialog.getFile();
	    System.out.println(tete+tete1);
	    try{
 File file =new File(tete+tete1);
    
    file.createNewFile();
    
   
    // Write to File   
    FileOutputStream filelow = new FileOutputStream(file);
    
    DataOutputStream datahigh= new DataOutputStream(filelow);
    String infile[]=ta.getItems();
    
   //datahigh.writeUTF(infile);
     }

     catch(IOException i)
       {
         i.printStackTrace();	

       }

				}
			});
			
		
		save.addActionListener(new ActionListener() 
			{		
	   public void actionPerformed(ActionEvent e) {
		JFileChooser chooser= new JFileChooser();
	    Frame frame = new Frame();
	   
        FileDialog fileDialog = new FileDialog(frame);
	    
	    fileDialog.setMode(FileDialog.SAVE);
	    fileDialog.show();
	    tete=fileDialog.getDirectory();
	    tete1=fileDialog.getFile();
	    System.out.println(tete+tete1);
	    try{
 File file =new File(tete+tete1);
    
    file.createNewFile();
    
   
    // Write to File   
    FileOutputStream filelow = new FileOutputStream(file);
    
    DataOutputStream datahigh= new DataOutputStream(filelow);
   String infile[]=ta.getItems();
    //datahigh.writeUTF(infile);
     }

     catch(IOException i)
       {
         i.printStackTrace();	

       }

				}
			});
		//****************************************************
		
	c.add("Center" ,new JScrollPane(ta));
		c.add("North" ,panel);
		c.add("South" ,panel2);
		panel.add("North" ,menu);
		panel.add("South" ,tools);
		panel2.add("Center" ,sp1);
		panel2.add("West" ,psend);
		
	//========================	
setSize(500,500);
setVisible(true);

		
		}
		
		
		public static void main(String[]arg)
		{
		clientchat z = new clientchat("Gamal Chat Client");
	}
	
	
	
	
	
	
	}
	class fileex
{
	public static void main(String[ ] args)
	{
	//	File in=new File("input.txt");
		File out=new File("output.txt");
	//	FileReader fr=null;
	//	BufferedReader br=null;
		FileWriter  fw=null;
		PrintWriter pw=null;
		try
		{InputStream is =System.in;
	InputStreamReader isr=new InputStreamReader(is);
		BufferedReader br=new BufferedReader(isr);
		//	fr=new FileReader (in);
		//	br=new BufferedReader(fr);
			fw=new FileWriter(out);
			pw=new PrintWriter(fw);
			String s;
		while((s=br.readLine())!=null)
		{
			pw.println(s);
			pw.flush();
		}
		br.close();
		fw.close();
		pw.close();
		}
	catch(IOException e)
		{
					System.out.println(e);
			}
			}
			}
	
