package oracle.forms.fd;

import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;
import javax.swing.BorderFactory;
import javax.swing.JTextField;
import javax.swing.border.Border;
import oracle.forms.ui.CustomEvent;
import oracle.forms.ui.VBean;
import oracle.forms.handler.IHandler;
import oracle.forms.properties.ID;

  /**
   * A javabean that fires for each character pressed
   * in a JtextField
   *
   * @author Francois Degrelle
   * @version 1.1
   * return the correct modifier code (Ctrl, Alt, Shift, ...)
   * in the sModifier String via the GETMODIFIER property
   */

  public class KeyTyped   extends VBean implements FocusListener, KeyListener
  {
    public final static ID SETTEXT      = ID.registerProperty("SETTEXT"); 
    public final static ID SETPOS       = ID.registerProperty("SETPOS");   
    public final static ID SETBG        = ID.registerProperty("SETBG");   
    public final static ID SETFG        = ID.registerProperty("SETFG");       
    public final static ID GETTEXT      = ID.registerProperty("GETTEXT");   
    public final static ID GETCHAR      = ID.registerProperty("GETCHAR");       
    public final static ID GETMODIFIER  = ID.registerProperty("GETMODIFIER");           
    public final static ID KEYTYPED     = ID.registerProperty("KEYTYPED");   
    private   IHandler    m_handler; 
    protected JTextField  jText ;
    private   int         iStringLength = 0 ;
    private   char        cChar ;
    private   int         iModifier = 0 ;
    private   String      sModifier = "" ;
   
    public KeyTyped ()
    {
       super();
       jText = new JTextField() ;
       jText.addKeyListener(this);
       Border border = BorderFactory.createLoweredBevelBorder();
       jText.setBorder(border);
       add(jText) ;

    }
     
    public void init(IHandler handler)
    {
      m_handler = handler;
      super.init(handler);
      addFocusListener(this);
      jText.addFocusListener(this);
    }     
   
   
    public boolean setProperty(ID property, Object value)
    {
      if (property == SETTEXT)  // set the text of the JTextField
      {
        jText.setText(value.toString());
        jText.setCaretPosition(0);
        return true;
      }
      else if (property == SETPOS) // set the position of the cursor
      {
        int iPos = new Integer((String)value).intValue() ;
        jText.setCaretPosition(iPos);
        return true;
      }
      else if (property == SETBG) // set the background color
      {
        String color = value.toString().trim();
        int r=-1, g=-1, b=-1, c=0 ;
        StringTokenizer st = new StringTokenizer(color,",");
        while (st.hasMoreTokens()) {
                 c = new Integer((String)st.nextToken()).intValue()  ;
                 if( (c <0) || (c > 255) ) c = 0 ;
                 if( r == -1 ) r = c ;
                 else if( g == -1 ) g = c ;
                 else if( b == -1 ) b = c ;
               }
        jText.setBackground(new Color(r, g, b) );
        return true;
      }
      else if (property == SETFG) // set the foreground color
      {
        String color = value.toString().trim();
        int r=-1, g=-1, b=-1, c=0 ;
        StringTokenizer st = new StringTokenizer(color,",");
        while (st.hasMoreTokens()) {
                 c = new Integer((String)st.nextToken()).intValue()  ;
                 if( (c <0) || (c > 255) ) c = 0 ;
                 if( r == -1 ) r = c ;
                 else if( g == -1 ) g = c ;
                 else if( b == -1 ) b = c ;
               }
        jText.setForeground(new Color(r, g, b) );
        return true;
      }     
      else
      {
       return super.setProperty(property, value);
      }
    }

    /**
     * Get the result string from Forms
     **/
    public Object getProperty(ID pId) // get the whole string
    {
      if (pId == GETTEXT)
      {
        return "" + jText.getText();
      }
      else if (pId == GETCHAR) // get the last character typed
      {
        return "" + cChar;
      }     
      else if (pId == GETMODIFIER) // get the last character typed
      {
        return "" + sModifier;
      }           
      else
      {
        return super.getProperty(pId);
      }
    }
   
    /** Handle the key typed event from the text field. */
    public void keyTyped(KeyEvent e) {
          ;
        }
        
    /** Handle the key pressed event from the text field. */
    public void keyPressed(KeyEvent e) {
       iStringLength = jText.getText().length();
       int iKey = (int)(e.getKeyChar()) ;
       // get the character
       cChar = e.getKeyChar() ;
       // get the modifier
       iModifier = e.getModifiers();
       sModifier = KeyEvent.getKeyModifiersText(iModifier);
       /*
        * Inform Forms that a character was typed
        */
       CustomEvent ce = new CustomEvent(m_handler, KEYTYPED);
       dispatchCustomEvent(ce);     
    }

    /** Handle the key released event from the text field. */
    public void keyReleased(KeyEvent e) {
    }

    public void focusGained(FocusEvent e)
     {
         if (e.getComponent() == this)
         {
             // put the focus on the component
             jText.requestFocus();
         }
   
         try
         {
             m_handler.setProperty(FOCUS_EVENT, e);
         }
         catch ( Exception ex )
         {
           ;
         }
     }
   
   
     public void focusLost(FocusEvent e)
     {
     }
   
  }
  