/**
 * Minibrowse is a simple web browser that uses Java's rendering engine.
 *
 * @author Dave Musicant
 * @author Jack Goldfeather
 * @author Amy Csizmar Dalal
 * @author David Liben-Nowell
 */

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.net.*;

class Minibrowse implements ActionListener {

    private JFrame frame;
    private JEditorPane htmlPane;
    private JTextField url;
    private JButton homeButton;

    /**
     * Inner class that makes links "live" on a web page.  Precludes
     * us from having to write our own listener to handle hyperlink
     * clicking.
     */
    public class myHyperlinkListener  implements HyperlinkListener  {
        public void hyperlinkUpdate(HyperlinkEvent e) {
            if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                JEditorPane pane = (JEditorPane) e.getSource();
                try {
                    URL myURL  = e.getURL();
                    String s = myURL.toString();
                    pane.setPage(myURL);
                    url.setText(s);
                } catch (Throwable t) {
                    System.out.println("Not a valid link");
                }
            }
        }
    }

    /**
     * Constructor:  Instantiates the window, sets up the toolbar,
     * makes everything purty.
     */
    public Minibrowse() {

        // Set up the main window.
        frame = new JFrame("Minibrowse");
        frame.setLocation(50,50);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create a component that can render HTML. Wrap a scollbar
        // component around it.
        htmlPane = new JEditorPane();
        htmlPane.setContentType("text/html");
        htmlPane.setEditable(false);
        myHyperlinkListener myH= new myHyperlinkListener();
        htmlPane.addHyperlinkListener(myH);

        JScrollPane scrollPane = new JScrollPane(htmlPane);
        scrollPane.setPreferredSize(new Dimension(500,500));

        // Create a Home button
        homeButton = new JButton("Home");
        homeButton.addActionListener(this);

        // Create a "text field" for the user to type in a URL.
        url = new JTextField(30);
        url.setActionCommand("url");
        url.addActionListener(this);

        // Create a "toolbar" to hold the Home button and the text field
        // (and, later, other buttons as well).
        JPanel toolbar = new JPanel();
        toolbar.add(homeButton);
        toolbar.add(url);

        // Add the "toolbar" panel to the top of the window, and the
        // scrollable HTML window to the middle of the window.
        frame.add(toolbar,BorderLayout.NORTH);
        frame.add(scrollPane,BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    /**
     * Graphics programs usually contain a method to handle "user 
     * interaction" events, such as clicking buttons or selecting items
     * from a menu.  This method is defined in the ActionListener 
     * interface.  This method handles all such events for the Minibrowse
     * program.  In this case, "all events" mean button clicks, menu
     * selections, and pressing "enter" after typing something in the 
     * URL text area.  
     * @param event  The interaction that triggered this method call.
     */
    public void actionPerformed(ActionEvent event) {
        String actionCommand = event.getActionCommand();

        // If the user pressed the "Home" button, set the current 
        // url to be that of the home page.
        if (actionCommand.equals("Home")) {
            url.setText("http://www.carleton.edu");
        }

        // If the user types in a URL or presses the "Home" button, 
        // go to that page.
        if (actionCommand.equals("Home") || actionCommand.equals("url")) {
            try {
                htmlPane.setPage(url.getText());
            } catch (IOException e) {
                JOptionPane.showMessageDialog(frame,"Bad page","Error:  ",
                                              JOptionPane.ERROR_MESSAGE);
            }
        } else {
            // Any other interaction is not defined (or not defined yet),
            // so throw an exception rather than trying to deal with it.
            throw new UnsupportedOperationException(actionCommand);
        }
    }


    /**
     * The main method.  Instantiates a Minibrowse window.
     * @param args  No command line arguments needed for this program.
     */
    public static void main(String[] args ) {

        Minibrowse minibrowse = new Minibrowse();

    }
}
