package oracle.forms.fd;

import java.awt.Component;
import oracle.forms.ui.*;
import oracle.forms.properties.ID;
import oracle.forms.engine.*;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
import java.awt.Point;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * A javabean that get the caret position
 * and the selection range
 *
 * @author Francois Degrelle
 * @version 1.0
 *
 */
     
public class EmbededTextArea extends VTextArea
{

  protected final static ID CASE                   = ID.registerProperty("CASE");
  protected final static ID STYLE                  = ID.registerProperty("STYLE");
  protected final static ID GET_RANGE_SELECTION    = ID.registerProperty("GET_RANGE_SELECTION");
  protected final static ID GET_CARET_POSITION     = ID.registerProperty("GET_CARET_POSITION");

  int       iStart = -1 ;
  int       iEnd   = -1 ;

  // Init
  public void init(oracle.forms.handler.IHandler param1)
	{
          super.init(param1);
	}
  
  // Constructor
  public EmbededTextArea()
  {
    super();
  } 


  public Object getProperty(ID pId)
  {
    // get the caret position
    if (pId == GET_CARET_POSITION)
    {
      oracle.ewt.lwAWT.lwText.LWTextArea ta = (oracle.ewt.lwAWT.lwText.LWTextArea) this.getComponents()[0] ;
      return "" + ta.getCaretPosition();
    }
    // get the selection range
    else if (pId == GET_RANGE_SELECTION)
    {
      oracle.ewt.lwAWT.lwText.LWTextArea ta = (oracle.ewt.lwAWT.lwText.LWTextArea) this.getComponents()[0] ;
      return "" + ta.getSelectionStart() + ',' + ta.getSelectionEnd() ;
    }
    else
    {
      return super.getProperty(pId);
    }
  } // getProperty()


  public boolean setProperty(ID pId, Object pValue)
  {
      if(pId == CASE)
      { 
        oracle.ewt.lwAWT.lwText.LWTextArea ta = (oracle.ewt.lwAWT.lwText.LWTextArea) this.getComponents()[0] ;
        String sProp = pValue.toString() ;
        iStart = ta.getSelectionStart() ;
        iEnd = ta.getSelectionEnd() ;
        String sSelect = "" ;
        if(sProp.equalsIgnoreCase("UPPER")) sSelect = ta.getSelectedText().toUpperCase() ;
        else if(sProp.equalsIgnoreCase("LOWER")) sSelect = ta.getSelectedText().toLowerCase() ;
        else return true ;
        String sText = ta.getText().substring(0,iStart)
               + sSelect
               + ta.getText().substring(iEnd,ta.getText().length()) ;
        ta.setText(sText);
        Integer n = new Integer(iStart);
        setProperty(ID.CURSOR_POSITION,n);
        return true ;
      }
      else if(pId == STYLE)
      { 
        oracle.ewt.lwAWT.lwText.LWTextArea ta = (oracle.ewt.lwAWT.lwText.LWTextArea) this.getComponents()[0] ;
        String sStyle = pValue.toString() ;
        iStart = ta.getSelectionStart() ;
        iEnd = ta.getSelectionEnd() ;
        String sText = ta.getText() ;
        String sSelect = ta.getSelectedText() ;
        String sResult = "" ;
        int iPos = 0 ;
        String sTagStart = "", sTagEnd = "" ;
        if(sStyle.equalsIgnoreCase("BOLD"))
        { 
          sTagStart = "<b>" ;
          sTagEnd   = "</b>" ;
        }
        else if(sStyle.equalsIgnoreCase("ITALIC"))
        { 
          sTagStart = "<i>" ;
          sTagEnd   = "</i>" ;       
        }
        else if(sStyle.equalsIgnoreCase("UNDERLINE"))
        { 
          sTagStart = "<u>" ;
          sTagEnd   = "</u>" ;       
        }      

        else return true ;

        iPos = sSelect.indexOf(sTagStart) ;
        if( iPos > -1)
          {
            // JRE 1.4 regular expression solution
            // uncomment if you use the Java plugin
            /*
            Pattern p ;
            Matcher m ;
            p = Pattern.compile(sTagStart,Pattern.CASE_INSENSITIVE);
            m = p.matcher(sSelect);
            sSelect = m.replaceAll("");
            p = Pattern.compile(sTagEnd,Pattern.CASE_INSENSITIVE);
            m = p.matcher(sSelect);
            sSelect = m.replaceAll("");          
            */
            // compatible JRE 1.3 code
            /**/
            sSelect = sSelect.substring(0,iPos) + sSelect.substring(iPos+3,sSelect.length());
            iPos = sSelect.indexOf(sTagEnd) ;
            if( iPos > -1) sSelect = sSelect.substring(0,iPos) + sSelect.substring(iPos+4,sSelect.length());
            /**/     

            sResult = sText.substring(0,iStart)
                    + sSelect
                    + sText.substring(iEnd,sText.length()) ;
          }
        else
          {
            sResult = sText.substring(0,iStart)
                    + sTagStart + sSelect + sTagEnd
                    + sText.substring(iEnd,sText.length()) ;
          }
        
        ta.setText(sResult);
        Integer n = new Integer(iStart);
        setProperty(ID.CURSOR_POSITION,n);
        return true ;
      }
    {
      return super.setProperty(pId, pValue);
    }

  } // setProperty()



} 

