import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;

public class ChatWindow extends JFrame implements ActionListener {

  ChatJPanel panel;
  JTextField tfield;
  JTextArea chatArea;
  JTextArea outputArea; // Ausgabe vom chatpartner 
  ChatArea chatPane;
             
 
 ChatWindow(String name){
   
   setTitle(name);
   setSize(400,400);
    
   
   tfield = new JTextField();
   // Menu kreieren
   JMenuBar menubar = new JMenuBar();
   
   JMenu menu1 = new JMenu("ColorMenu");
   
   JMenuItem item1_1 = new JMenuItem("roter Hintergrund");   
      item1_1.setActionCommand("redBackground");
      item1_1.addActionListener(this);   
      
   JMenuItem item1_2 = new JMenuItem("weisser Hintergrund");
      item1_2.setActionCommand("whiteBackground");
      item1_2.addActionListener(this);
   
   JMenuItem item1_3 = new JMenuItem("gruener Hintergrund");
      item1_3.setActionCommand("greenBackground");
      item1_3.addActionListener(this);
      
   JMenuItem item1_4 = new JMenuItem("gelber Hintergrund");
      item1_4.setActionCommand("yellowBackground");
      item1_4.addActionListener(this);
   
   JMenuItem item1_5 = new JMenuItem("Caret Position");
      item1_5.setActionCommand("CaretPosition");
      item1_5.addActionListener(this);
   
   JMenu menu2 = new JMenu("ChatMenu2");
   JMenu menu3 = new JMenu("ChatMenu3");
   menu1.add(item1_1);menu1.add(item1_2);menu1.add(item1_3);
   menu1.add(item1_4);menu1.add(item1_5);
   menubar.add(menu1);menubar.add(menu2);menubar.add(menu3);
   setJMenuBar(menubar);
   
         
   addWindowListener( new WindowAdapter()
 	           {
 		    public void windowClosing(WindowEvent e){System.exit(0);}
                   });
                   
   getContentPane().setLayout(new GridBagLayout());
                   
  
 
   /* --------------------- Komponenten an GUI hinzufuegen------------------- */
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH; 
        gbc.weightx = 100;
        gbc.weighty = 100;
        chatArea = new JTextArea();
        chatPane = new ChatArea();
        outputArea = new JTextArea();
        panel = new ChatJPanel();
          
        add( new JScrollPane(chatPane), gbc, 0,3,2,1);
        add( panel, gbc, 0,4,2,1);  
        add( new JScrollPane(outputArea), gbc,0,5,2,1);
        
          
  show();
  
 } // Ende von Konstruktor
 
 
 /* Methode actionPerformed wegen ActionListener. 
    Muss ausserhalb von Konstruktor deklariert sein */
 public void actionPerformed(ActionEvent ae){
   	
    String command = ae.getActionCommand();
   
    if(command.equals("redBackground")){
       chatPane.setBackground(Color.red);    
    }	
    if(command.equals("whiteBackground")){
       chatPane.setBackground(Color.white);    
    }
    if(command.equals("greenBackground")){
       chatPane.setBackground(Color.green);    
    }
    if(command.equals("yellowBackground")){
       chatPane.setBackground(Color.yellow);                  
    }   
    if(command.equals("CaretPosition")){
      System.out.println(chatPane.getCaret());
    }	
 }
 
 
 /*----------------------- METHODEN DEFINIEREN------------------------------ */
 
 private void add(Component c, GridBagConstraints gbc, 
            int x, int y, int w, int h)
        { gbc.gridx = x;
          gbc.gridy = y;
          gbc.gridwidth = w;
          gbc.gridheight = h;
          getContentPane().add(c, gbc);
        }

 
 
 private boolean firstTime = true;
 private String alt = "",
                neu = "",
                alles = "";
                 
                
 public String getNewString(){ 	
   	            
   alles = chatPane.getText();
 
   if(!firstTime){
    // neu = alles - alt	
       neu = alles.substring(alt.length());
       System.out.println(alt.length());
   }else{ neu = alles;
    }
   
   alt = alt + neu;
   firstTime = false;
   
   return neu;   
 }
   	
 
  
 
 public void drawText(String out){
   outputArea.append(out+"\n");   
 }
 
 
  
} // Ende von ChatWindow

/* Die Klasse ChatJPanel --------------------------------------------------------- */

class ChatJPanel extends JPanel{

ChatJPanel(){}

// ueberschreibe die Methode damit dieser String immer gezeichnet wird
public void paintComponent(Graphics g){

 g.setColor(Color.red);
 g.drawString(" Dein Eingabe Window:   ", 15, 15);
 g.setColor(Color.black);
 g.drawLine(1, this.getHeight()/2 - 20, getWidth(),this.getHeight()/2-20);
 g.setFont( new Font("Titelfont", 10, 20));
 g.setColor(Color.green);
 g.drawString( " LINKTAGON CHAT PROGRAM ", 
                     this.getWidth()/5, 
                       this.getHeight()/2 );
 g.setColor(Color.black);
 g.drawLine(1, this.getHeight()/2+ 10, getWidth(),this.getHeight()/2+10);
 g.setColor(Color.blue);
 g.setFont(new Font("", 9, 10));
 g.drawString("     Dein Chat Partner sagt:  ",15, this.getHeight()-10);  
}

} //ChatJPanel



/* Die Klasse ChatArea ---------------------------------------------------------- */
class ChatArea extends JEditorPane {

  CaretEvent caret;
  EditorKit editorkit;
  
  ChatArea(){
   	
   //this.setEditorKit(editorkit);	
  	
  }

  			
  	
} // ChatArea

