import java.net.URL;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JEditorPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.HyperlinkEvent;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Color;

import java.io.IOException;

/**
 * simple demo for a web browser
 * 
 * @author Stefano Markidis
 *
 */
public class WEBbrowser extends JFrame implements HyperlinkListener, 
ActionListener{
	public static void main(String[] args) {
		// take the initial web page from the command line argument
		if (args.length == 0)
		  new WEBbrowser("http://www.cs.unm.edu/");
		else
		  new WEBbrowser(args[0]);
	}
	private JTextField urlField;
	private JEditorPane htmlPane;
	private String initialURL;

    public WEBbrowser(String initialURL) {
		super("Simple Browser");
		this.initialURL = initialURL;
		// add a listener to the window for closing init
		addWindowListener(new ExitListener());
		// add a top panel
		JPanel topPanel = new JPanel();
		topPanel.setBackground(Color.green);
		// create a Jlabel and JTextField for gettingURL
		JLabel urlLabel = new JLabel("URL:");
		urlField = new JTextField(30);
		urlField.setText(initialURL);
		// add a listener to JTextField
		urlField.addActionListener(this);
		
		// add url label and field to toppanel
		topPanel.add(urlLabel);
		topPanel.add(urlField);
		
		//add top panel at the top and html text at the center
		getContentPane().add(topPanel, BorderLayout.NORTH);
		try {
				htmlPane = new JEditorPane(initialURL);
				htmlPane.setEditable(false);
				htmlPane.addHyperlinkListener(this);
				JScrollPane scrollPane = new JScrollPane(htmlPane);
				getContentPane().add(scrollPane, BorderLayout.CENTER);
			} catch(IOException ioe) {
			   System.out.println("Can't build HTML pane for " + initialURL);
			}
			Dimension screenSize = getToolkit().getScreenSize();
			int width = screenSize.width * 8 / 10;
			int height = screenSize.height * 8 / 10;
			setBounds(width/8, height/8, width, height);
			setVisible(true);
		  }
		  // event handling for changing the URL
		  public void actionPerformed(ActionEvent event) {
			String url;
			url = urlField.getText();
			try {
			  htmlPane.setPage(new URL(url));
			  urlField.setText(url);
			} catch(IOException ioe) {
			  System.out.println("Can't follow link to " + url );
			}
		  }
		  // event handling when you click on a link
		  public void hyperlinkUpdate(HyperlinkEvent event) {
			if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
			  try {
				htmlPane.setPage(event.getURL());
				urlField.setText(event.getURL().toExternalForm());
			  } catch(IOException ioe) {
				System.out.println("Can't follow link to " 
						 + event.getURL().toExternalForm() );
			  }
			}
		  }
 }

