// David Tate
// A very simple web browser using Java Swing
// TODO:
//	a) History functionality
//	b) Parse out view source to .js and .htm 
// c) Refresh Button
// d) Open Local file
// e) About a million other things

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

public class Zilla extends JFrame {
	protected JEditorPane htmlPane;
	protected JTextField URLField;
	
	public Zilla (String url) {
		super ("Zilla 1.0");
		createUI(url);
		setVisible(true);		
	}
	
	//Creates the UI components and adds event plumbing	
	public void createUI (String urlString) {
		setSize(500, 600);
		addWindowListener(new WindowAdapter() {
	 		public void windowClosing (WindowEvent e) { System.exit(0); }	 	
	 	});
		                  		
		final Container content = getContentPane();
		content.setLayout(new BorderLayout());
		
		/*** Setup Menus ***/
		//Look and Feel Menu
		JMenu lnf = new JMenu ("Look & Feel", true);
		ButtonGroup buttonGroup = new ButtonGroup();
		final UIManager.LookAndFeelInfo [] info = UIManager.getInstalledLookAndFeels();
		for (int i = 0; i < info.length; i++) {
			JRadioButtonMenuItem item = new JRadioButtonMenuItem(info[i].getName(), i == 0);
			final String className = info[i].getClassName();
			item.addActionListener (new ActionListener () {
				public void actionPerformed (ActionEvent ae) {
					try { UIManager.setLookAndFeel (className); }
					catch (Exception e) { System.out.println("Whoops! " + e); }
					SwingUtilities.updateComponentTreeUI(Zilla.this);
				}
			});
			buttonGroup.add(item);
			lnf.add(item);
		}
		
		//Source Menu
		JMenu hack = new JMenu ("Hack");
		JMenuItem viewSource = new JMenuItem("View Source");
		viewSource.addActionListener (new ActionListener () {
			public void actionPerformed(ActionEvent ae) {
				System.out.println("You like to hack, no?");
				String url = URLField.getText();
				JTextArea source = new JTextArea();
				source.setWrapStyleWord (true);
				source.setLineWrap (true);
				System.out.println("Just got " + url);
				try {
					URL page = new URL (url);
					BufferedReader pageStream = new BufferedReader( new InputStreamReader (page.openStream()));
					String line;
					while (( line = pageStream.readLine()) != null) {
						/*** Parse out words here to highlight ***/
						source.append(line);		
						source.append("\n");
					}
				}
				catch (MalformedURLException ue) {
					System.out.println ("Malformed URL: " + ue);	
				}
				catch (IOException e) {
					System.out.println ("Can't get a Reader for source: " + e);
				}

				String message = "Here is the source";
				JOptionPane.showOptionDialog (content, new Object[] { message, new JScrollPane(source) }, "Source", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null ); 
			}
		});
		hack.add (viewSource);
			
		//Create MenuBar
		JMenuBar mb = new JMenuBar();
		mb.add (lnf);
		mb.add (hack);	
		setJMenuBar (mb);
		
		//Tool Bar
		//add the URL control
		JToolBar urlToolBar = new JToolBar();
		URLField = new JTextField(urlString, 40);
		urlToolBar.add(new JLabel("Location"));
		urlToolBar.add(URLField);
		content.add((urlToolBar), BorderLayout.NORTH);
		
		//add htmlPane
	 	htmlPane = new JEditorPane();
	 	htmlPane.setEditable(false);
	 	content.add(new JScrollPane(htmlPane), BorderLayout.CENTER);
	 	
	 	//open the initial URL
		System.out.println("About to add homepage:" + urlString);
	 	openURL (urlString);

	 	htmlPane.addHyperlinkListener(new LinkActivator());
	 		 	
	 	//open the URL when the text field changes (return is hit)
	 	URLField.addActionListener(new ActionListener() {
	 		public void actionPerformed (ActionEvent ae) {
	 			openURL(ae.getActionCommand());	 		
	 		}
	 	});
	}
	
	  protected void openURL (String urlString) {
	  	try {
	  		URL url = new URL (urlString);
	  		htmlPane.setPage(url); 		//the meat of this whole thing; HTML 3.2 now
			URLField.setText(url.toExternalForm());
	  	}
	  	catch ( Exception e) {
	  		System.out.println("Couldn't open " + urlString + " : " + e);
	  	}
	}
	
	  class LinkActivator implements HyperlinkListener {
	  	public void hyperlinkUpdate (HyperlinkEvent he) {
	  		HyperlinkEvent.EventType type = he.getEventType();
	  		if (type == HyperlinkEvent.EventType.ENTERED)
	  			htmlPane.setCursor (Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	  		else if (type == HyperlinkEvent.EventType.EXITED)
	  			htmlPane.setCursor(Cursor.getDefaultCursor());
	  		else if (type == HyperlinkEvent.EventType.ACTIVATED)
	  			openURL(he.getURL().toExternalForm());
	  	}
	 }
		
	  public static void main (String [] args) {
	  	String urlString = "http://www.cs.uga.edu/~tate/";
	  	if (args.length > 0 )
	  		urlString = args[0];
	  	new Zilla (urlString);
	  }
}

