package oracle.forms.fd;

import java.awt.Event;
import java.awt.Font;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.text.html.HTMLEditorKit;
import oracle.forms.handler.IHandler;
import oracle.forms.ui.*;
import oracle.forms.properties.ID;

  /**
   * A simple HTML Styled JTextPane
   *
   * @author Francois Degrelle
   * @version 1.2
   */

public class HtmlPane extends VBean implements FocusListener
{
  protected static final ID pSetText    = ID.registerProperty("SET_TEXT");
  protected static final ID pSetEdit    = ID.registerProperty("SET_EDIT");
  protected static final ID pGetText    = ID.registerProperty("GET_TEXT");
  protected static final ID pLostFocus  = ID.registerProperty("BEAN_QUITTED");  
  private   IHandler      m_handler;  
  protected JTextPane     m_editor;
  protected HTMLEditorKit m_kit;  
    
        public void init(IHandler handler)
        {
    super.init(handler);   
    m_handler = handler;
          addFocusListener(this);
          m_editor.addFocusListener(this);

        }
  
  /**
   * Constructor 
   */
  public HtmlPane()
  {
    super();
    
    // new JTextpane
    m_editor = new JTextPane();
    m_kit = new HTMLEditorKit();
    m_editor.setEditorKit(m_kit);

    // Bind some standard actions
    InputMap inputMap = m_editor.getInputMap();
    
    // Set the Bold action
    Action a = new HTMLEditorKit.BoldAction() ;
    m_editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_B, Event.CTRL_MASK), "Bold");
    m_editor.getActionMap().put("Bold", a);
    
    // Set the Italic action
    a = new HTMLEditorKit.ItalicAction() ;
    m_editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_I, Event.CTRL_MASK), "Italic");
    m_editor.getActionMap().put("Italic", a);
    
    // Set the Underline action
    a = new HTMLEditorKit.UnderlineAction() ;
    m_editor.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_U, Event.CTRL_MASK), "Underline");
    m_editor.getActionMap().put("Underline", a);    

    JScrollPane ps = new JScrollPane(m_editor);
    add(ps);
    ps.setVisible(true);

  } 

  /******************************************
   *  Get the text content to return to Forms
   ******************************************/
  public Object getProperty(ID pId)
  {
    if (pId == pGetText)
    {
      return "" + m_editor.getText();
    }
    else
    {
      return super.getProperty(pId);
    }
  } 

  /**********************************
   *  Set the text content from Forms
   **********************************/
  public boolean setProperty(ID pId, Object pValue)
  {
    if (pId == pSetText) // set the user value
    {
      String s = (String)pValue ;
      m_editor.setText(s);
      return true;
    }    
    if (pId == pSetEdit) // set the edit flag
    {
      String s = (String)pValue ;
      if(s.equalsIgnoreCase("true")) m_editor.setEditable(true);
      else  m_editor.setEditable(false);
      return true;
    }        
    else
    {
      return super.setProperty(pId, pValue);
    }
  } 

  public void focusGained(FocusEvent e)
     {
         if (e.getComponent() == this)
         {
             // put the focus on the component
             m_editor.requestFocus();
         }
    
         try
         {
             m_handler.setProperty(FOCUS_EVENT, e);
         }
         catch ( Exception ex )
         {
           ;
         }
     }
    
  public void focusLost(FocusEvent e)
     {     
       CustomEvent ce = new CustomEvent(m_handler, pLostFocus);
       dispatchCustomEvent(ce);     
     }

} // HtmlPane