package oracle.forms.fd;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import oracle.forms.ui.*;
import oracle.forms.properties.ID;
    
    /**
     * A javabean that get the caret position
     * and the selection range
     *
     * @author Francois Degrelle
     * @version 1.0
     *
     */
     
public class EmbedeTextField extends VTextField
{

  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 EmbedeTextField()
  {
    super();
  } 


  public Object getProperty(ID pId)
  {
    // get the caret position
    if (pId == GET_CARET_POSITION)
    {
      return "" + this.getCaretPosition();
    }
    // get the selection range
    else if (pId == GET_RANGE_SELECTION)
    {
      return "" + this.getSelectionStart() + ',' + this.getSelectionEnd() ;
    }
    else
    {
      return super.getProperty(pId);
    }
  } 

  public boolean setProperty(ID pId, Object pValue)
  {
    if(pId == CASE)
    { 
      String sProp = pValue.toString() ;
      iStart = this.getSelectionStart() ;
      iEnd = this.getSelectionEnd() ;
      String sSelect = "" ;
      if(sProp.equalsIgnoreCase("UPPER")) sSelect = this.getSelectedText().toUpperCase() ;
      else if(sProp.equalsIgnoreCase("LOWER")) sSelect = this.getSelectedText().toLowerCase() ;
      else return true ;
      String sText = this.getText().substring(0,iStart)
             + sSelect
             + this.getText().substring(iEnd,this.getText().length()) ;
      this.setText(sText);
      Integer n = new Integer(iStart);
      setProperty(ID.CURSOR_POSITION,n);
      return true ;
    }
    else if(pId == STYLE)
    { 
      String sStyle = pValue.toString() ;
      iStart = this.getSelectionStart() ;
      iEnd = this.getSelectionEnd() ;
      String sText = this.getText() ;
      String sSelect = this.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()) ;
        }
      
      this.setText(sResult);
      Integer n = new Integer(iStart);
      setProperty(ID.CURSOR_POSITION,n);
      return true ;
    }
    else 
    {
      return super.setProperty(pId, pValue);
    }

  }

} 

