/**
 * @author	Emmanuel Languillat
 */

import com.sun.java.swing.*;
import com.sun.java.swing.event.*;
import com.sun.java.swing.text.*;
import com.sun.java.accessibility.*;

import java.awt.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.*;
import com.sun.java.swing.text.html.*;

public class HtmlPanel extends JPanel implements HyperlinkListener {
  
  JEditorPane html;
  HTMLEditorKit edit;
  
  public void setText(Reader in) {
    html.setContentType ("text/html");
    edit = new HTMLEditorKit();
    Document doc = edit.createDefaultDocument();
    html.setEditorKit(edit);
    html.setDocument(doc);
    edit.install(html);

    try {
      edit.read(in, doc, 0);
    }
    catch(IOException e) {
      System.err.println("Unable to communicate with HTML on the fly");
    }
    catch(BadLocationException e) {
      System.err.println("Using a wrong location for HTML");
    }
    catch(NullPointerException ignore) {} // HTML parser is very simple
                                          // and can crash anytime
  }
  
  public HtmlPanel() {
    setBackground(Color.white);
    setLayout(new BorderLayout());
    getAccessibleContext().setAccessibleName("HTML panel");
    getAccessibleContext().setAccessibleDescription("A panel for viewing HTML documents, and following their links");
     
    try {
      URL url = new URL("http://www.essi.fr");
      html = new JEditorPane();
      html.setEditable(false);
      html.addHyperlinkListener(this);
      JScrollPane scroller = new JScrollPane();
      JViewport vp = scroller.getViewport();
      vp.add(html);
      vp.setBackingStoreEnabled(true);
      add(scroller, BorderLayout.CENTER);
    } catch (MalformedURLException e) {
      System.out.println("Malformed URL: " + e);
    } catch (IOException e) {
      System.out.println("IOException: " + e);
    }
  }

  public HtmlPanel(String site) {
    setBackground(Color.white);
    setLayout(new BorderLayout());
    getAccessibleContext().setAccessibleName("HTML panel");
    getAccessibleContext().setAccessibleDescription("A panel for viewing HTML documents, and following their links");
     
    try {
      URL url = new URL(site);
      html = new JEditorPane(url);
      html.setEditable(false);
      html.addHyperlinkListener(this);
      JScrollPane scroller = new JScrollPane();
      JViewport vp = scroller.getViewport();
      vp.add(html);
      vp.setBackingStoreEnabled(true);
      add(scroller, BorderLayout.CENTER);
    } catch (MalformedURLException e) {
      System.out.println("Malformed URL: " + e);
    } catch (IOException e) {
      System.out.println("IOException: " + e);
    }
  }
  
  public void hyperlinkUpdate(HyperlinkEvent e) {
    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
      linkActivated(e.getURL());
    }
  }
  
  protected void linkActivated(URL u) {
    Cursor c = html.getCursor();
    Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
    html.setCursor(waitCursor);
    SwingUtilities.invokeLater(new PageLoader(u, c));
  }
  
  class PageLoader implements Runnable {
    
    PageLoader(URL u, Cursor c) {
      url = u;
      cursor = c;
    }
    
    public void run() {
      if (url == null) {
	// restore the original cursor
	html.setCursor(cursor);
	
	// PENDING(prinz) remove this hack when 
	// automatic validation is activated.
	Container parent = html.getParent();
	parent.repaint();
      } else {
	Document doc = html.getDocument();
	try {
	  html.setPage(url);
	} catch (IOException ioe) {
	  html.setDocument(doc);
	  getToolkit().beep();
	} finally {
	  // schedule the cursor to revert after
	  // the paint has happended.
	  url = null;
	  SwingUtilities.invokeLater(this);
	}
      }
    }
    
    URL url;
    Cursor cursor;
  }
}
