import objectdraw.*;

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

import java.io.*;

import java.net.*;

import javax.swing.*;


// A class that allows one to fetch an HTML file from the web and
// view its contents
//@width 800
//@height 600
public class Moozilla extends Controller implements ActionListener,
    ItemListener {
    // Width of area for URL entry
    private static final int URLFIELDWIDTH = 45;

    // Dimensions for HTML display area
    private static final int HTMLWIDTH = 60;
    private static final int HTMLHEIGHT = 24;

    // Dimensions for links display area
    private static final int LINKSWIDTH = 60;
    private static final int LINKSHEIGHT = 10;

    // Large font for GUI controls
    private Font bigFont = new Font("Times", Font.PLAIN, 16);

    // Area in which HTML is displayed
    private JTextArea htmlText;

    // The "load" button
    private JButton doit;

    // Url controls
    private JTextField urlName;
    private JComboBox favorites;

    // Set up all GUI controls and their listeners
    public void begin() {
        resize(800,600);
        Container panel = getContentPane();
        panel.setLayout(new FlowLayout());

        // Create a "bookmarks" menu
        panel.add(new JLabel("Favorites:"));
        favorites = new JComboBox();
        favorites.setFont(bigFont);
        favorites.addItem("http://wso.williams.edu");
        favorites.addItem("http://www.williams.edu");
        favorites.addItem("http://www.cs.williams.edu");
        favorites.addItem("http://www.cs.williams.edu/~cs134/index.html");
        favorites.addItem("file:///Volumes/freund/simple.html");

        favorites.addItemListener(this);
        panel.add(favorites);

        // Create a text area into which a URL may be entered
        urlName = new JTextField("Enter URL", URLFIELDWIDTH);
        urlName.setFont(bigFont);
        panel.add(urlName);

        // Create the button used to start fetching a web page
        doit = new JButton("Fetch HTML");
        doit.setFont(bigFont);
        doit.addActionListener(this);
        panel.add(doit);

        // Create the text areas in which HTML and links will be displayed.
        htmlText = new JTextArea(HTMLHEIGHT, HTMLWIDTH);
        htmlText.setFont(bigFont);
        panel.add(new JScrollPane(htmlText), BorderLayout.CENTER);

        panel.validate();
    }

    // If item is selected from "favorites" menu, enter it as current URL
    public void itemStateChanged(ItemEvent e) {
        urlName.setText(favorites.getSelectedItem().toString());
    }

    // Fetch the web page at "urlString" and return its contents as a String
    private String getWebPage(String urlString) {
        String page = "";

        try {
            URL url = new URL(urlString);
            BufferedReader input = 
                new BufferedReader(new InputStreamReader(url.openStream()));

            String line = input.readLine();

            while (line != null) {
                page = page + line + "\n";
                line = input.readLine();
            }

            input.close();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, e);
        }

        return page;
    }

    // When the "Fetch" button is pressed, 
    // try to get the HTML, display and search it.
    public void actionPerformed(ActionEvent e) {
        String url = urlName.getText();
        String fullpage = getWebPage(url);
        htmlText.setText(fullpage);
    }
}
