import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.*;

public class es extends Applet
{
    TextField tfUrl = new TextField("es.java", 20);
    Button bUrl = new Button("Fichier ASCII");
    TextArea taUrl = new TextArea("Hello\n", 10, 40);
    Button bHtml = new Button("New Netscape");
    Button bDate = new Button("Date de Jessica");

    public void init() {
        tfUrl.setEditable(true);
        add(tfUrl);
        resize(tfUrl.preferredSize());

	add(bUrl);
        add(bHtml);
        add(bDate);
	add(taUrl);

        resize(400, 300);

        show();
        addItem("initializing...");
    }

    public void start() {
        addItem("starting...");
    }

    public void stop() {
        addItem("stopping...");
    }

    public void destroy() {
        addItem("preparing for unloading...");
    }

    public boolean action(Event event, Object what) {
        String strUrl = new String();
        String strBuffer = new String();
        URL uUrl; 
        URLConnection ucUrl;
        DataInputStream disUrl;
	Socket sDate;

        if (event.target instanceof Button) 
        {
	        addItem("Boutton poussoir " + what + " appuye");
        } 
        if (event.target == bUrl) 
        {
		strUrl = tfUrl.getText();
	        System.out.println(strUrl);
                try {
		    uUrl = new URL(getDocumentBase(), strUrl);
		    try {
		        ucUrl = uUrl.openConnection();
		        disUrl = new DataInputStream(ucUrl.getInputStream());
                        while((strBuffer = disUrl.readLine()) != null)
                        { 
                            taUrl.appendText(strBuffer+"\n");
                        } 
		    } catch (IOException e) {
		      addItem("I/O error");
		    }
                } catch (MalformedURLException e) {
                    addItem("Malformed URL error");
                }
         } 

        if (event.target == bHtml) 
        {
		strUrl = tfUrl.getText();
	        System.out.println(strUrl);
                try {
		    uUrl = new URL(getDocumentBase(), strUrl);
                    getAppletContext().showStatus("Loading "+strUrl);
                    getAppletContext().showDocument(uUrl, "_blank");
		} catch (IOException e) {
		    addItem("I/O error");
		}
        }

        if (event.target == bDate) 
        {
                try {
		    sDate = new Socket("www.essi.fr", 13);
		    disUrl = new DataInputStream(sDate.getInputStream());
                    strBuffer = disUrl.readLine();
                    taUrl.appendText("Date : "+strBuffer+"\n");
		} catch (Exception e) {
		    addItem("Exception error");
		}
        }

        return false;
    }
     
    public void addItem(String newWord) {
        System.out.println(newWord);
    }
}

