package oracle.forms.fd;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import oracle.forms.ui.VBean;
import oracle.forms.properties.ID;

  /**
   * A javabean that displays some (very) simple html pages
   *
   * @author Francois Degrelle
   * @version 1.1
   */

public class HtmlFrame extends VBean implements HyperlinkListener, 
                                                ActionListener {

  // property to set the url
  public final static ID pSetURL      = ID.registerProperty("SETURL");  
  // private variables
  private JEditorPane htmlPane;
  private String initialURL;

  public HtmlFrame() {
    super();
    initialURL = "";
    htmlPane = new JEditorPane();
    htmlPane.setEditable(false);
    htmlPane.addHyperlinkListener(this);
    JScrollPane scrollPane = new JScrollPane(htmlPane);
    add(scrollPane);
    setVisible(true);
  }

  public void actionPerformed(ActionEvent event) {
    System.out.println("actionPerformed ");
    String url = initialURL ;
    try {
      htmlPane.setPage(new URL(url));
    } catch(IOException ioe) {
      message("Unable to follow the link to " + url + ": " + ioe);
    }
  }
  
  // try to follow the selected link
  public void hyperlinkUpdate(HyperlinkEvent event) {
    if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
      try {
        htmlPane.setPage(event.getURL());
      } catch(IOException ioe) {
        message("Unable to follow the link to " 
                 + event.getURL().toExternalForm() + ": " + ioe);
      }
    }
  }

  // warning dialog box
  private void message(String message) {
    JOptionPane.showMessageDialog(this, message, "Error", 
                                  JOptionPane.ERROR_MESSAGE);
  }

 public boolean setProperty(ID pId, Object pValue)
  {

    if (pId == pSetURL) // set and display the new URL
    {
      String sValue = (String)pValue ;
      initialURL = sValue ;
    try {
      htmlPane.setPage(new URL(initialURL));
    } catch(IOException ioe) {
      message("Unable to follow the link to " + initialURL + ": " + ioe);
    }      
      return true;
    }
    else
    {
      return super.setProperty(pId, pValue);
    }

  }


}