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

// This appears in Core Web Programming from
// Prentice Hall Publishers, and may be freely used
// or adapted. 1997 Marty Hall, hall@apl.jhu.edu.

public class ShowFile extends Applet {
  private TextArea fileArea;
  private TextField filename;
  
  public void init() {
    Panel topPanel = new Panel();
    topPanel.setLayout(new BorderLayout());
    topPanel.add("North",
                 new Label("Enter a URL relative to " +
                           "the current document's " +
                           "home directory",
                           Label.CENTER));
    Panel inputPanel = new Panel();
    inputPanel.add(new Label("File:"));
    filename = new TextField(20);
    inputPanel.add(filename);
    inputPanel.add(new Button("Show It"));
    topPanel.add("Center", inputPanel);
    setLayout(new BorderLayout());
    add("North", topPanel);
    fileArea = new TextArea();
    fileArea.setFont(new Font("Courier", Font.BOLD, 12));
    add("Center", fileArea);
  }

  public boolean action(Event e, Object o) {
    try {
      URL file = new URL(getDocumentBase(),
                       filename.getText());
      BufferedInputStream buffer =
        new BufferedInputStream(file.openStream());
      DataInputStream in =
        new DataInputStream(buffer);
      fileArea.setText("");
      String line;
      while ((line = in.readLine()) != null) {
        fileArea.appendText(line);
        fileArea.appendText("\n");
      }
      in.close();
    } catch(MalformedURLException mue) {
      System.out.println("Bad URL: " + mue);
    } catch(IOException ioe) {
      System.out.println("IOException: " + ioe);
    }
    return(true);
  }
}
    
