/**
 * Notscape.java
 *
 * @author <a href="mailto:ricky@brics.dk">Mikkel Ricky</a>
 */

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

public class Notscape extends JFrame {
    JButton back, forward;
    JTextField location;
    JScrollPane scroller;
    JEditorPane document;
    Stack history;

    private void setLocation(String spec) {
	try {
	    URL url = new URL(spec);
	    setLocation(url);
	} catch (MalformedURLException e) {
	    JOptionPane.showMessageDialog(null, "URL `"+spec+"' is invalid.",
					  "Invalid URL", JOptionPane.ERROR_MESSAGE);
	}
    }

    private void setLocation(URL url) {
	setLocation(url, false);
    }

    private void setLocation(URL url, boolean push) {
	try {
	    URL oldUrl = document.getPage();
	    if (!push && (oldUrl != null))
		history.push(oldUrl);
	    if (history.size() > 0) {
		back.setText("Back");
		back.setToolTipText("Go to `"+history.peek()+"'");
	    } else {
		back.setText("Quit");
		back.setToolTipText("Quit");
	    }
	    location.setText(url.toString());
	    document.setPage(url);
	    document.requestFocus();
	} catch (IOException e) {
	    JOptionPane.showMessageDialog(null, "Unable to open `"+url+"'.",
					  "Error", JOptionPane.ERROR_MESSAGE);
	}
    }

    public Notscape(String url) throws Exception {
	super("Notscape");
	history = new Stack();
	back = new JButton("Back");
	back.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent event) {
		    if (history.size() > 0)
			setLocation((URL)history.pop(), true);
		    else 
			System.exit(0);
		}
	    });
	Container content = getContentPane();
	content.setLayout(new BorderLayout());

	location = new JTextField();
	location.addActionListener(new ActionListener () {
		public void actionPerformed(ActionEvent event) {
		    setLocation(location.getText());
		}
	    });
	JPanel top = new JPanel(new BorderLayout());
	top.add(back, BorderLayout.WEST);
	top.add(location, BorderLayout.CENTER);
	content.add(top, BorderLayout.NORTH);

	document = new JEditorPane();
	document.setEditable(false);
	document.addHyperlinkListener(new HyperlinkListener() {
		public void hyperlinkUpdate(HyperlinkEvent e) {
		    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
			JEditorPane pane = (JEditorPane)e.getSource();
			if (e instanceof HTMLFrameHyperlinkEvent) {
			    HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent)e;
			    HTMLDocument doc = (HTMLDocument)pane.getDocument();
			    doc.processHTMLFrameHyperlinkEvent(evt);
			} else {
			    setLocation(e.getURL());
			}
		    }
		}
	    });

	scroller = new JScrollPane(document);
	content.add(scroller, BorderLayout.CENTER);
	setSize(500, 400);
	setVisible(true);
	setLocation(url);

	addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent e) {
		    System.exit(0);
		}
	    });
    }

    public static void main(String[] args) throws Exception {
	if (args.length != 1) {
	    System.err.println("Usage: java Notscape <url>");
	    System.exit(1);
	}
	new Notscape(args[0]);
    }
}
