/**
 *
 * ChatApplet.java - a simple JFC chat room web applet.
 *
 * @author Matthew William Coan
 *
 */
import java.io.*;
import java.net.*;

import java.applet.*;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

class ListenerThread extends Thread {
   BufferedReader in = null;
   JTextArea textArea = null;

   ListenerThread(BufferedReader bin, JTextArea txt) {
      in = bin;
      textArea = txt;
   }

   public void run() {
      String line;

      try {
         while((line = in.readLine()) != null) {
            textArea.setText(textArea.getText()+line+"\n");
            textArea.selectAll();
            int x = textArea.getSelectionEnd();
            textArea.select(x,x);
         }
      }
      catch(Exception ex) {
         ex.printStackTrace(System.err);
      }
   }
}

public class ChatApplet extends JApplet implements LayoutManager, ActionListener {
   private JButton sendButton = null;
   private JTextField textInput = null; 
   private JTextArea textArea = null;
   private JScrollPane scrollPane = null;
   private Socket socket = null;
   private PrintWriter out = null;
   private BufferedReader in = null;

   public final int HEIGHT = 400;
   public final int WIDTH = 550;

   public void actionPerformed(ActionEvent e) {
      if(e.getSource().equals(textInput)
         || e.getSource().equals(sendButton)) {
         String text = textInput.getText();
         textInput.setText("");
         out.println(text);
         out.flush();
         textInput.requestFocus();
      }
   }

   public void addLayoutComponent(String name, Component comp) 
   { 
   }

   public void layoutContainer(Container parent) 
   {
      Rectangle dim = parent.getBounds();

      scrollPane.setLocation(0,0);
      scrollPane.setSize(dim.width, dim.height-30);

      textInput.setLocation(0,dim.height-30);
      textInput.setSize(dim.width - 100, 30);

      sendButton.setLocation(dim.width - 100, dim.height - 30);
      sendButton.setSize(100, 30);
   }

   public Dimension minimumLayoutSize(Container parent)
   {
      return new Dimension(WIDTH, HEIGHT);
   }

   public Dimension preferredLayoutSize(Container parent)
   {
      return new Dimension(WIDTH, HEIGHT);
   }

   public void removeLayoutComponent(Component parent)
   {
   }

   public void init()
   {
      try {
         //String lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
         String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
         UIManager.setLookAndFeel(lookAndFeel);
      }
      catch(Exception ex) {

      }

      sendButton = new JButton("Send");
      textInput = new JTextField();
      textArea = new JTextArea();
      scrollPane = new JScrollPane(textArea);

      textInput.addActionListener(this);
      sendButton.addActionListener(this);

      textArea.setEditable(false);

      setLayout(this);

      add(scrollPane);
      add(textInput);
      add(sendButton);

      try {
         socket = new Socket(super.getCodeBase().getHost(), 8080);
         out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
         in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
         new ListenerThread(in, textArea).start();
      }
      catch(Exception ex) {
         ex.printStackTrace(System.err);
      }
   }
}
