package oracle.forms.fd;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.StringTokenizer;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import oracle.forms.handler.IHandler;
import oracle.forms.ui.VBean;
import oracle.forms.properties.ID;
import oracle.forms.ui.CustomEvent;
import javax.swing.border.*;

  /**
   * A Bean to add GUIs component at runtime
   *
   * @author Francois Degrelle
   * @version 1.1
   * this version allows to create buttons, TextFields and TextAreas
   */
   
public class AddItem2 extends VBean implements ActionListener, FocusListener, KeyListener
{
  // variables
  public final static ID AddItem       = ID.registerProperty("ADD_ITEM");      
  public final static ID SetFrame      = ID.registerProperty("SET_FRAME");  
  public final static ID SetText       = ID.registerProperty("SET_TEXT");    
  public final static ID SetLabel      = ID.registerProperty("SET_LABEL");  
  public final static ID SetFrameColor = ID.registerProperty("SET_FRAME_COLOR");    
  public final static ID SetFont       = ID.registerProperty("SET_FONT");  
  public final static ID SetAlign      = ID.registerProperty("SET_ALIGN");    
  public final static ID SetBevel      = ID.registerProperty("SET_BEVEL");  
  public final static ID SetBGColor    = ID.registerProperty("SET_BG_COLOR");  
  public final static ID SetFGColor    = ID.registerProperty("SET_FG_COLOR");    
  public final static ID GetBtPressed  = ID.registerProperty("GET_BT_PRESSED");      
  public final static ID SetMaxLength  = ID.registerProperty("SET_MAXLENGTH");  
  public final static ID GetSelItem    = ID.registerProperty("GET_SELITEM");        
  public final static ID GetText       = ID.registerProperty("GET_TEXT");  
  public final static ID ButtonPressed = ID.registerProperty("BUTTONPRESSED");  
  public final static ID RemoveItem    = ID.registerProperty("REMOVE_ITEM");    
  public final static ID SetSeparator  = ID.registerProperty("SET_SEPARATOR");  
  public final static ID FocusChanged  = ID.registerProperty("FOCUSCHANGED"); 
  public final static ID SetFocus      = ID.registerProperty("SET_FOCUS");  
  public final static ID SetDebug      = ID.registerProperty("SET_DEBUG");      
  public final static ID SetDebugComp  = ID.registerProperty("SET_DEBUG_COMP");        
  private final static int BUTTON    = 0 ;
  private final static int TEXTFIELD = 1 ;
  private final static int TEXTAREA  = 2 ;
  private IHandler      m_handler;  
  private JButton[]     tabButtons       = new JButton[100];
  private JTextField[]  tabJtextField    = new JTextField[100];
  private JTextArea[]   tabJtextArea     = new JTextArea[100];
  private JScrollPane[] tabScrollPane   = new JScrollPane[100] ;
  private int[]         tabLengthTF      = new int[100];
  private int[]         tabLengthTA      = new int[100];  
  private String        sButtonPressed  = "" ;  
  private Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
  private Border raisedBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED);
  private Border loweredBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED);
  private Border noneBorder = BorderFactory.createEmptyBorder();
  private Border defBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED);
  private TitledBorder[] tabTitleTF = new TitledBorder[100] ;
  private TitledBorder[] tabTitleTA = new TitledBorder[100] ;
  private boolean[][] tabDebug = new boolean[3][100] ;
  private Font defFont = new Font("Arial", Font.PLAIN, 12) ;
  private int  defAlign = JTextField.LEFT ;
  private Color defCanvasColor, defFieldColor = new Color(255,255,255) ;
  private boolean bDebug = false ;
  private int iCType=0, iCNum=0 ;
  private String separator = "^" ;
  
  public void actionPerformed(ActionEvent e) {
        // set the pressed button's ID
        sButtonPressed = e.getActionCommand() ;
        output( "Button pressed="+sButtonPressed) ;
        // send message to Forms that a button was pressed
        CustomEvent ce = new CustomEvent(m_handler, ButtonPressed);
        dispatchCustomEvent(ce);
    }
    
  public void init(IHandler handler)
  {
    m_handler = handler;
    super.init(handler);
    addFocusListener(this);
    try
    {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      SwingUtilities.updateComponentTreeUI(this);
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }     
  }  
  
  public boolean setProperty(ID property, Object value)
  {
    /*******************************************
     *  Add a component on the current canvas  *
     ******************************************/
    if (property == AddItem)
    {
      if( defCanvasColor == null) defCanvasColor = this.getParent().getBackground() ;
      int iNum = 0, x = 0, y = 0, w = 0, h = 0, iMax, iAlign = defAlign ;
      String sValue="", sLabel="", sAction="", sObject="", sName="", sAlign="" ;
      sValue = value.toString();
      StringTokenizer st = new StringTokenizer(sValue,",");
      sObject = st.nextToken() ;                 // Item type
      if( sObject.equalsIgnoreCase("BT")) iAlign = JTextField.CENTER ;
      iNum = Integer.parseInt(st.nextToken()) ; // button number in the table
      setCurrentObject(sObject+iNum);
      sName = sObject + iNum ;
      sLabel = st.nextToken() ;                 // label
      sAction = st.nextToken() ;                // action name
      x = Integer.parseInt(st.nextToken()) ;    // X pos
      y = Integer.parseInt(st.nextToken()) ;    // Y pos
      w = Integer.parseInt(st.nextToken()) ;    // Width
      h = Integer.parseInt(st.nextToken()) ;    // Height
      if (st.hasMoreTokens())
      {
        sAlign = st.nextToken() ;                // Alignement
        if( sAlign.equalsIgnoreCase("LEFT"))   iAlign = JTextField.LEFT ;
        if( sAlign.equalsIgnoreCase("RIGHT"))  iAlign = JTextField.RIGHT ;
        if( sAlign.equalsIgnoreCase("CENTER")) iAlign = JTextField.CENTER ;
      }
      if(sObject.equalsIgnoreCase("BT") )
      {
        // add the new button to the canvas
        tabButtons[iNum] = new JButton(sLabel) ;
        tabButtons[iNum].setActionCommand(sAction);
        tabButtons[iNum].addActionListener(this);
        tabButtons[iNum].setBounds(x,y,w,h);
        tabButtons[iNum].setFont(defFont);
        tabButtons[iNum].setHorizontalAlignment(iAlign);
        tabButtons[iNum].addFocusListener(this);
        tabButtons[iNum].setName(sName);
        this.getParent().add(tabButtons[iNum]) ;
      }
      if(sObject.equalsIgnoreCase("TF") )
      {
        // add the new Text field to the canvas
        iMax = Integer.parseInt(sAction) ;
        tabTitleTF[iNum] =  BorderFactory.createTitledBorder(loweredetched);
        tabJtextField[iNum] = new JTextField() ;
        tabJtextField[iNum].setBorder(tabTitleTF[iNum]);
        tabJtextField[iNum].setBounds(x,y,w,h);
        tabJtextField[iNum].setFont(defFont);
        tabJtextField[iNum].setBackground(defFieldColor);
        if( sAlign != null )
           tabJtextField[iNum].setHorizontalAlignment(iAlign);
        tabJtextField[iNum].addFocusListener(this);
        tabJtextField[iNum].addKeyListener(this);
        sName="TF"+iNum ;
        tabJtextField[iNum].setName(sName);
        tabLengthTF[iNum] = iMax ;
        this.getParent().add(tabJtextField[iNum]) ;
      }
      if(sObject.equalsIgnoreCase("TA") )
      {
        // add the new Text field to the canvas
        iMax = Integer.parseInt(sAction) ;
        tabTitleTA[iNum] =  BorderFactory.createTitledBorder(loweredetched);
        tabJtextArea[iNum] = new JTextArea() ;
        tabJtextArea[iNum].setBounds(x,y,w,h);
        tabJtextArea[iNum].setFont(defFont);
        tabJtextArea[iNum].setBackground(defFieldColor);
        tabJtextArea[iNum].addFocusListener(this);
        tabJtextArea[iNum].addKeyListener(this);
        sName="TA"+iNum ;
        tabJtextArea[iNum].setName(sName);
        tabLengthTA[iNum] = iMax ;
        JScrollPane scroll = new JScrollPane(tabJtextArea[iNum]) ;
        scroll.setBackground(defCanvasColor);
        scroll.setBorder(tabTitleTA[iNum]);
        scroll.setBounds(x,y,w,h);
        scroll.setVisible(true);
        tabScrollPane[iNum] = scroll ;
        this.getParent().add(scroll) ;
      }      
      return true;
    }

    /**********************************************
     * Set the Framed Text Field title attributes *
     *********************************************/
    else if (property == SetFrame)
    {
      int iNum = 0;
      String sTitle="", sFont="", sColor="", sJustif="", string="", sObject="", sWeight="" ;
      int iFontSize = 10 ;
      Font fFont = null ;
      string = value.toString();
      StringTokenizer st = new StringTokenizer(string,",");
      sObject      = st.nextToken() ;           // frame Title
      iNum = Integer.parseInt(st.nextToken()) ; // button number in the table
      setCurrentObject(sObject+iNum);
      if (st.hasMoreTokens())
        sTitle     = st.nextToken() ;          // frame Title
      if (st.hasMoreTokens())
        sFont      = st.nextToken() ;          // frame Font
      if (st.hasMoreTokens())
        iFontSize  = Integer.parseInt(st.nextToken()) ;  // frame Font size      
      if (st.hasMoreTokens())
        sWeight      = st.nextToken() ;        // frame Font weight
      if (st.hasMoreTokens())
        sJustif    = st.nextToken() ;          // frame Justification
      output("Set Frame ["+ sObject+iNum + "] " + sTitle + sFont+ " " +iFontSize+"("+sWeight+")");
      if( sFont != null ) 
      { 
         if(sWeight.equalsIgnoreCase("N"))  fFont = new Font(sFont, Font.PLAIN, iFontSize) ; 
         if(sWeight.equalsIgnoreCase("B"))  fFont = new Font(sFont, Font.BOLD, iFontSize) ; 
         if(sWeight.equalsIgnoreCase("I"))  fFont = new Font(sFont, Font.ITALIC, iFontSize) ; 
         if(sWeight.equalsIgnoreCase("BI")) fFont = new Font(sFont, Font.BOLD+Font.ITALIC, iFontSize) ; 
         if(sObject.equalsIgnoreCase("TF")) tabTitleTF[iNum].setTitleFont(fFont);
         if(sObject.equalsIgnoreCase("TA")) tabTitleTA[iNum].setTitleFont(fFont);
      }
      if ( sJustif != null )
      {
        if(sObject.equalsIgnoreCase("TF")) 
        {
          if( sJustif.equalsIgnoreCase("left") )   tabTitleTF[iNum].setTitleJustification(TitledBorder.LEFT);
          if( sJustif.equalsIgnoreCase("right") )  tabTitleTF[iNum].setTitleJustification(TitledBorder.RIGHT);
          if( sJustif.equalsIgnoreCase("center") ) tabTitleTF[iNum].setTitleJustification(TitledBorder.CENTER);
          tabTitleTF[iNum].setTitle(sTitle);
        }
        if(sObject.equalsIgnoreCase("TA")) 
        {
          if( sJustif.equalsIgnoreCase("left") )   tabTitleTA[iNum].setTitleJustification(TitledBorder.LEFT);
          if( sJustif.equalsIgnoreCase("right") )  tabTitleTA[iNum].setTitleJustification(TitledBorder.RIGHT);
          if( sJustif.equalsIgnoreCase("center") ) tabTitleTA[iNum].setTitleJustification(TitledBorder.CENTER);
          tabTitleTA[iNum].setTitle(sTitle);
        }        
      }
      return true;
    } 
    /***********************************
     * Set the Framed Text Field color *
     **********************************/
    else if (property == SetFrameColor)
    {
      String sColor = value.toString().trim(), sObject="";
      int r=-1, g=-1, b=-1, iNum = 0 ;
      Color color = defCanvasColor ;
      StringTokenizer st = new StringTokenizer(sColor,",");
      sObject  = st.nextToken() ;
      iNum = Integer.parseInt(st.nextToken()) ;
      setCurrentObject(sObject+iNum);
      if (st.hasMoreTokens())
      {
        r = Integer.parseInt(st.nextToken()) ;
        g = Integer.parseInt(st.nextToken()) ;
        b = Integer.parseInt(st.nextToken()) ;    
        color = new Color(r, g, b) ;
        output("SetFrameColor ["+sObject+iNum+"] ("+r+","+g+","+b+")") ;
      } else output("SetFrameColor ["+sObject+iNum+"] (default canvas color") ;
      if(sObject.equalsIgnoreCase("TF")) tabTitleTF[iNum].setTitleColor(color);
      if(sObject.equalsIgnoreCase("TA")) tabTitleTA[iNum].setTitleColor(color);
      return true;
    }        
    /*****************
     *  Set the Font *
     ****************/
    else if (property == SetFont)
    {
      String sObject, string, sFont, sWeight ;
      int iFontSize = 10 , iNum = 0, iWeight = Font.PLAIN ;
      string = value.toString();
      StringTokenizer st = new StringTokenizer(string,",");
      sObject = st.nextToken() ;      
      iNum = Integer.parseInt(st.nextToken()) ;
      setCurrentObject(sObject+iNum);
      sFont = st.nextToken() ;
      output("SetFont ["+sObject+iNum+"] ("+sFont+")") ;
      if (st.hasMoreTokens())
        iFontSize = Integer.parseInt(st.nextToken()) ;
      if (st.hasMoreTokens())
        {
          sWeight = st.nextToken() ;
          if( sWeight.equalsIgnoreCase("N")) iWeight = Font.PLAIN ;
          if( sWeight.equalsIgnoreCase("B")) iWeight = Font.BOLD ;
          if( sWeight.equalsIgnoreCase("I")) iWeight = Font.ITALIC ;
          if( sWeight.equalsIgnoreCase("BI")) iWeight = Font.BOLD + Font.ITALIC ;
        }
      Font fFont = new Font(sFont, iWeight, iFontSize) ;
      if(sObject.equalsIgnoreCase("BT")) tabButtons[iNum].setFont(fFont);
      if(sObject.equalsIgnoreCase("TF")) tabJtextField[iNum].setFont(fFont);
      if(sObject.equalsIgnoreCase("TA")) tabJtextArea[iNum].setFont(fFont);
      return true ;
    }
    /*****************************
     *  Set the background color *
     ****************************/
    else if (property == SetBGColor)
    {
      String sObject, sColor = value.toString().trim();
      Color color = defCanvasColor ;
      int r=-1, g=-1, b=-1, iNum = 0 ;
      StringTokenizer st = new StringTokenizer(sColor,",");
      sObject = st.nextToken() ;      
      iNum = Integer.parseInt(st.nextToken()) ;
      setCurrentObject(sObject+iNum);
      if (st.hasMoreTokens())
      {
        r = Integer.parseInt(st.nextToken()) ;
        g = Integer.parseInt(st.nextToken()) ;
        b = Integer.parseInt(st.nextToken()) ;    
        color = new Color(r, g, b) ;
        output("SetBGColor ["+sObject+iNum+"] "+r+","+g+","+b) ;
      }
      else output("SetBGColor ["+sObject+iNum+"] default canvas color") ;
      if(sObject.equalsIgnoreCase("BT")) tabButtons[iNum].setBackground(color) ;
      if(sObject.equalsIgnoreCase("TF")) tabJtextField[iNum].setBackground(color) ;
      if(sObject.equalsIgnoreCase("TA")) tabJtextArea[iNum].setBackground(color) ;
      return true;
    }
    /*****************************
     *  Set the foreground color *
     ****************************/
    else if (property == SetFGColor)
    {
      String sObject, sColor = value.toString().trim();
      Color color = defCanvasColor ;
      int r=-1, g=-1, b=-1, iNum = 0 ;
      StringTokenizer st = new StringTokenizer(sColor,",");
      sObject = st.nextToken() ;      
      iNum = Integer.parseInt(st.nextToken()) ;
      setCurrentObject(sObject+iNum);
      if (st.hasMoreTokens())
      {
        r = Integer.parseInt(st.nextToken()) ;
        g = Integer.parseInt(st.nextToken()) ;
        b = Integer.parseInt(st.nextToken()) ;    
        color = new Color(r, g, b) ;
        output("SetFGColor ["+sObject+iNum+"] "+r+","+g+","+b) ;
      }
      else output("SetFGColor ["+sObject+iNum+"] default canvas color") ;
      if(sObject.equalsIgnoreCase("BT")) tabButtons[iNum].setForeground(color) ;
      if(sObject.equalsIgnoreCase("TF")) tabJtextField[iNum].setForeground(color) ;
      if(sObject.equalsIgnoreCase("TA")) tabJtextArea[iNum].setForeground(color) ;
      return true;
    }    
    /**********************************************
     *  Set the text                              *
     *  text for Text fields - label for buttons  *
     *********************************************/
    else if (property == SetText)
    {
      String sText, sObject, sValue = value.toString().trim();
      int r=-1, g=-1, b=-1, iNum = 0 ;
      StringTokenizer st = new StringTokenizer(sValue,"^");
      sObject = st.nextToken() ;      
      iNum = Integer.parseInt(st.nextToken()) ;
      setCurrentObject(sObject+iNum);
      sText = st.nextToken() ;      
      output("SetText ["+sObject+iNum+"]  "+sText) ;
      if(sObject.equalsIgnoreCase("BT")) tabButtons[iNum].setText(sText);
      if(sObject.equalsIgnoreCase("TF")) tabJtextField[iNum].setText(sText) ;
      if(sObject.equalsIgnoreCase("TA")) tabJtextArea[iNum].setText(sText) ;
      return true;
    }        
    /***************************************
     *  Set max length for text component  *
     **************************************/
    else if (property == SetMaxLength)
    {
      String sAlign, sObject, sValue = value.toString().trim();
      int iNum = 0, iMax = 0 ;
      StringTokenizer st = new StringTokenizer(sValue,",");
      sObject = st.nextToken() ;      
      iNum = Integer.parseInt(st.nextToken()) ;
      setCurrentObject(sObject+iNum);
      iMax = Integer.parseInt(st.nextToken()) ;
      output("SetMax "+sObject+iNum+" ("+iMax+")") ;      
      if(sObject.equalsIgnoreCase("TF"))
      {
        tabLengthTF[iNum] = iMax ;
        if( tabJtextField[iNum].getText().length() > iMax)
          tabJtextField[iNum].setText(tabJtextField[iNum].getText().substring(0,iMax)) ;
      }
      if(sObject.equalsIgnoreCase("TA"))
      {
        tabLengthTA[iNum] = iMax ;
        if( tabJtextArea[iNum].getText().length() > iMax)
          tabJtextArea[iNum].setText(tabJtextArea[iNum].getText().substring(0,iMax)) ;
      }      
      return true;
    } 
    /********************
     *  Set Alignement  *
     *******************/
    else if (property == SetAlign)
    {
      String sAlign, sObject, sValue = value.toString().trim();
      int iNum = 0, iAlign = JTextField.LEFT ;
      StringTokenizer st = new StringTokenizer(sValue,",");
      sObject = st.nextToken() ;      
      iNum = Integer.parseInt(st.nextToken()) ;
      setCurrentObject(sObject+iNum);
      sAlign = st.nextToken() ; 
      output("SetAlign "+sObject+iNum+" ("+sAlign+")") ;      
      if( sAlign.equalsIgnoreCase("LEFT")) iAlign = JTextField.LEFT ;
      if( sAlign.equalsIgnoreCase("RIGHT")) iAlign = JTextField.RIGHT ;
      if( sAlign.equalsIgnoreCase("CENTER")) iAlign = JTextField.CENTER ;
      if(sObject.equalsIgnoreCase("BT")) tabButtons[iNum].setHorizontalAlignment(iAlign);
      if(sObject.equalsIgnoreCase("TF")) tabJtextField[iNum].setHorizontalAlignment(iAlign);
      return true;
    }     
    /***************
     *  Set Bevel  *
     **************/
    else if (property == SetBevel)
    {
      String sBorder, sObject, sValue = value.toString().trim();
      int iNum=0 ;
      Border border = defBorder;
      StringTokenizer st = new StringTokenizer(sValue,",");
      sObject = st.nextToken() ;
      iNum = Integer.parseInt(st.nextToken()) ;
      setCurrentObject(sObject+iNum);
      if (st.hasMoreTokens())
      {
        sBorder = st.nextToken() ; 
        if( sBorder.equalsIgnoreCase("RAISED")) border = raisedBorder ;
        if( sBorder.equalsIgnoreCase("LOWERED")) border = loweredBorder ;
        if( sBorder.equalsIgnoreCase("NONE")) border = noneBorder ;
        if( sBorder.equalsIgnoreCase("TITLED") && iCType == TEXTFIELD ) 
          border = tabTitleTF[iNum] ;
        if( sBorder.equalsIgnoreCase("TITLED") && iCType == TEXTAREA ) 
          border = tabTitleTA[iNum] ;          
        output("SetBevel "+sObject+iCNum+" ("+sBorder+")") ;
      }
      else output("SetBevel "+sObject+iCNum+" (Standard)") ;
      if(iCType == BUTTON)    tabButtons[iCNum].setBorder(border);
      if(iCType == TEXTFIELD) tabJtextField[iCNum].setBorder(border);
      if(iCType == TEXTAREA)  tabJtextArea[iCNum].setBorder(border);
      return true;
    }       
    /****************************
     *  Set the text separator  *
     ***************************/
    else if (property == SetSeparator)
    {
      separator = value.toString().trim();
      System.out.println("SetSeparator ["+separator+"]") ;
      return true;
    } 
    /******************************
     *  Remove an existing item   *
     *****************************/
    else if (property == RemoveItem)
    {
      String sText, sObject, sValue = value.toString().trim();
      int iNum = 0 ;
      StringTokenizer st = new StringTokenizer(sValue,",");
      sObject = st.nextToken() ;      
      iNum = Integer.parseInt(st.nextToken()) ;
      setCurrentObject(sObject+iNum);
      output("Remove component "+sObject+iNum) ;
      if(sObject.equalsIgnoreCase("BT"))
      {  
        this.getParent().remove(tabButtons[iNum]);
        tabDebug[BUTTON][iNum] = false ;
      }
      if(sObject.equalsIgnoreCase("TF"))
      {
        this.getParent().remove(tabJtextField[iNum]) ;
        tabDebug[TEXTFIELD][iNum] = false ;
        tabLengthTF[iNum] = -1 ;
      }
      if(sObject.equalsIgnoreCase("TA"))
      {
        this.getParent().remove(tabJtextArea[iNum]) ;
        this.getParent().remove(tabScrollPane[iNum]);
        tabDebug[TEXTAREA][iNum] = false ;
        tabLengthTA[iNum] = -1 ;
      }
      return true;
    } 
    /********************************************
     *  Set the focus on a particular component *
     *******************************************/
    else if (property == SetFocus)
    {
      String sObject, sValue = value.toString().trim();
      int iNum = 0 ;
      StringTokenizer st = new StringTokenizer(sValue,",");
      sObject = st.nextToken() ;      
      iNum = Integer.parseInt(st.nextToken()) ;
      if( sObject.equalsIgnoreCase("BT")) tabButtons[iNum].requestFocus();
      if( sObject.equalsIgnoreCase("TF"))
      {
        tabJtextField[iNum].requestFocus();
        tabJtextField[iNum].setCaretPosition(tabJtextField[iNum].getText().length());
      }
      if( sObject.equalsIgnoreCase("TA"))
      {
        tabJtextArea[iNum].requestFocus();
        tabJtextArea[iNum].setCaretPosition(tabJtextArea[iNum].getText().length());
      }      
      System.out.println("SetFocus on "+sObject+iNum) ;
      return true;
    }    
    /******************************************
     *  Set the debug mode for all components *
     *****************************************/
    else if (property == SetDebug)
    {
      String sValue = value.toString().trim();
      System.out.println("SetDebugAll "+sValue) ;
      if( sValue.equalsIgnoreCase("true") )  bDebug = true ;
      if( sValue.equalsIgnoreCase("false") ) bDebug = false ;
      return true;
    }  
    /*********************************************
     *  Set debug mode for particular component  *
     ********************************************/
    else if (property == SetDebugComp)
    {
      String sObject, sValue = value.toString().trim();
      int iNum = 0 ;
      StringTokenizer st = new StringTokenizer(sValue,",");
      sObject = st.nextToken() ;      
      iNum = Integer.parseInt(st.nextToken()) ;
      if( sObject.equalsIgnoreCase("BT") )  tabDebug[BUTTON][iNum] = true ;
      if( sObject.equalsIgnoreCase("TF") )  tabDebug[TEXTFIELD][iNum] = true ;
      if( sObject.equalsIgnoreCase("TA") )  tabDebug[TEXTAREA][iNum] = true ;
      System.out.println("SetDebugComponent for "+sObject+iNum) ;
      return true;
    }      
    else
    {
     return super.setProperty(property, value);
    }
  }  

  /*************************
    * Get the properties
    ************************/
   public Object getProperty(ID pId) // Get the current pressed button ID
   {
     
     /******************
      * button pressed *
      *****************/
     if (pId == GetBtPressed)
     {
       return "" + sButtonPressed;
     }      
     /*****************
      * selected item *
      ****************/
     else if (pId == GetSelItem)
     {
       String sType = ""  ;
       if( iCType == BUTTON )    sType = "BT" ;
       if( iCType == TEXTFIELD ) sType = "TF" ;
       if( iCType == TEXTAREA )  sType = "TA" ;
       return "" + sType + "," + iCNum ;
     }
     /******************
      *  get the text  *
      *****************/
     else if (pId == GetText)
     {
       String sText = ""  ;
       if( iCType == BUTTON )    sText = tabButtons[iCNum].getText();
       if( iCType == TEXTFIELD ) sText = tabJtextField[iCNum].getText();
       if( iCType == TEXTAREA )  sText = tabJtextArea[iCNum].getText();
       return sText;
     }     
     else
     {
       return super.getProperty(pId);
     }
   } 

   public void focusGained(FocusEvent e)
     {
         System.out.println("focus on "+e.getComponent().getName()) ;             
         setCurrentObject(e.getComponent().getName()) ;
         try
         {
             m_handler.setProperty(FOCUS_EVENT, e);
         }
         catch ( Exception ex )
         {
           ;
         }
         finally
         {
           CustomEvent ce = new CustomEvent(m_handler, FocusChanged);
           dispatchCustomEvent(ce);           
         }
     }
    
   public void focusLost(FocusEvent e)
     {     
     }

   /** Handle the key typed event from the text field. **/
   public void keyTyped(KeyEvent e) {
      // Get the component indice number
      int max, cur ;
      setCurrentObject(e.getComponent().getName());
      if( iCType == TEXTFIELD )
      {
        cur = tabJtextField[iCNum].getText().length() ;
        output("keyTyped Component="+e.getComponent().getName() + iCNum +" curLength="+cur);
        if( iCNum > 0 )
        {
          if(e.getKeyChar() != e.VK_BACK_SPACE)
          {
            max = tabLengthTF[iCNum] ;
            output("keyTyped max="+max+"  cur="+cur ) ;
            if( cur >= max ) e.setKeyChar(java.awt.event.KeyEvent.CHAR_UNDEFINED);
          }
        }
      }
     if( iCType == TEXTAREA )
     {
       cur = tabJtextArea[iCNum].getText().length() ;
       output("keyTyped Component="+e.getComponent().getName() + iCNum +" curLength="+cur);
       if( iCNum > 0 )
       {
         if(e.getKeyChar() != e.VK_BACK_SPACE)
         {
           max = tabLengthTA[iCNum] ;
           output("keyTyped max="+max+"  curLength="+cur ) ;
           if( cur >= max ) e.setKeyChar(java.awt.event.KeyEvent.CHAR_UNDEFINED);
         }
       }
     }
   }
   
  /** Handle the key pressed event from the text field. **/
  public void keyPressed(KeyEvent e) {
      ;
  }

  /** Handle the key released event from the text field. **/
  public void keyReleased(KeyEvent e) {
      ;       
  }
  
  /** debug trace on java console  **/
  private void output( String text ) 
  {
    if( bDebug ) System.out.println( text ) ;
    else
    {
      if( tabDebug[iCType][iCNum] ) System.out.println( text ) ;
    }
  }

  /******************************
   *   set the current object   *
   ******************************/
  private void setCurrentObject( String sObject)           
  {
    String sReturn = "", sType="" ;
    int iNum = 0 ;
    System.out.println("setCurrentObject="+sObject);
    sType = sObject.substring(0,2) ;
    if( sType.equals("BT") || sType.equals("TF") || sType.equals("TA"))
    {
      iNum =  Integer.parseInt(sObject.substring(2)) ;
      if( sType.equalsIgnoreCase("BT")) iCType = BUTTON ;
      if( sType.equalsIgnoreCase("TF")) iCType = TEXTFIELD ;
      if( sType.equalsIgnoreCase("TA")) iCType = TEXTAREA ;
      iCNum = iNum ;
    }

  }

}
