package oracle.forms.fd;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import oracle.forms.ui.DropDownEvent;
import oracle.forms.ui.DropDownListener;
import oracle.forms.ui.VPopList;
import oracle.forms.handler.IHandler;
import oracle.forms.properties.ID;

    /**
     * A PopList PJC to correct the BUG No. 3867157
     * 
     * @author   Francois Degrelle
     * @creation May 2008
     * @version  1.1
     * added functionality to search in
     * non sorted lists
     * 
     * 29/05/08 - Tom Cleymans
     * Added DropDownListener - do not consume in case dropdown is not shown
     * 
     */
     
public class PopList extends VPopList{
    
    public final static ID INIT_OTHER_KEYS    = ID.registerProperty("INIT_OTHER_KEYS");   
    public final static ID SET_LOG            = ID.registerProperty("SET_LOG");   
    public final static ID SET_CASE_SENSITIVE = ID.registerProperty("SET_CASE_SENSITIVE");    
    
    public    IHandler    m_handler; 
    private   KeyListener kl ;     
    private   DropDownListener dd;
    private   boolean     isDropped = false ;
    private   int         iCountSel  = 0 ; // elements count
    private   int         iCurSel    = 0 ; // current selected index
    private   char        cChar ;
    private   int         iModifier  = 0 ;
    private   String      sModifier  = "" ;
    private   String      sOtherKeys = "";
    private   boolean     bLog       = false ;
    private   boolean     bSensitive = true ;
    private   VPopList    pl         = this ;
    
    
    public PopList()
    {
      super();
    }
     
    public void init(IHandler handler)
    {
      m_handler = handler;
      super.init(handler);
      // add a new KeyListener to the poplist
      setKeyListener();
      addKeyListener(kl);
      //add a DropDownListener to the poplist
      setDropDownListener();
      addDropDownListener(dd);
    }     
    
    /*-----------------------*
     *  set the properties
     *-----------------------*/
    public boolean setProperty(ID property, Object value)
    {log("setProperty(): "+property);
      //
      // other allowed keys with int value > 105
      //
      if (property == INIT_OTHER_KEYS)
      {
         sOtherKeys = value.toString() ;
         return true;
      }
      
      // set the case sensitive on/off
      else if (property == SET_CASE_SENSITIVE)
       {
          if(value.toString().equalsIgnoreCase("true")) bSensitive = true ; 
          else if(value.toString().equalsIgnoreCase("false")) bSensitive = false ; 
          return true;
       }      
      
      // set the log on/off
      else if (property == SET_LOG)
        {
           if(value.toString().equalsIgnoreCase("true")) bLog = true ; 
           else if(value.toString().equalsIgnoreCase("false")) bLog = false ; 
           return true;
        }      
      
      else
        {
          return super.setProperty(property, value);
        }      
    }
    
    /*-----------------------*
     *  Get the properties
     *-----------------------*/
     public Object getProperty(ID pId) 
     {
       
       log("getProperty(): "+pId+" value:"+super.getProperty(pId));
       //
       // get the index count
       //
       if (pId == ID.COUNT)
       {
         String s = "" + super.getProperty(pId) ;
         iCountSel = Integer.parseInt(s);
         log("count(): "+iCountSel);
       }
       //
       // get the selected index
       //
       else if (pId == ID.SELECTEDINDEX)
       {
         String s = "" + super.getProperty(pId) ;
         iCurSel = Integer.parseInt(s);
         log("selectIndex(): "+iCurSel);
       }

       return super.getProperty(pId);       
     }    

    /*-----------------------------------------*
     *   create a DropDownListener for the PopList
     *-----------------------------------------*/
    void setDropDownListener()
    {
       dd = new DropDownListener()
          {
           public void dropDownHidden (DropDownEvent e)
           {
             isDropped = false;
             log("Not Dropped");
           }
           public void dropDownShown (DropDownEvent e)
           {
             isDropped = true;
             log("Dropped");
           }
       };
    }


    /*-----------------------------------------*
     *   create a KeyListener for the PopList
     *-----------------------------------------*/
    void setKeyListener()
    {
         kl = new KeyListener(){
              public void keyPressed(KeyEvent e) {
             if (isDropped)
             {
             // get the code
             int iKey = e.getKeyCode() ;
             // get the character
             cChar = e.getKeyChar() ;
             // get the modifier
             iModifier = e.getModifiers();
             sModifier = KeyEvent.getKeyModifiersText(iModifier);
             String s = "" ;
             boolean bFound = false ;
             /*
              * Handle the navigation
              * to find matching values
              */
              int i = 0 ;
              log("iKey="+iKey+" char:"+cChar+" modifier:"+iModifier);
              if((iModifier != 2 && iModifier != 8) && (iKey > 40 && iKey <=105) || (sOtherKeys.indexOf(iKey) > -1))
              { 
                // search for current position to the end
                if(iCurSel < (iCountSel-1)) {
                  i =  iCurSel + 1 ;
                    while((i<iCountSel-1) && (!bFound)) {
                        if(!bSensitive) s = pl.getItem(i).toLowerCase() ;
                        else s = pl.getItem(i) ;
                        try{
                        if(s != null && (s.charAt(0) == cChar)) {
                             // set the new selected index
                            Object o = new Integer(i);
                            pl.setProperty( ID.SELECT, o) ;
                            iCurSel = i ;
                            bFound = true ;
                        }
                        }catch(Exception ex) {;}
                        i++;
                    }                  
                }
                // search for begin to current position
                if(! bFound)
                {
                  i = 0 ;
                    while((i<iCurSel) && (!bFound)) {
                        if(!bSensitive) s = pl.getItem(i).toLowerCase() ;
                        else s = pl.getItem(i) ;
                        try{
                        if(s != null && (s.charAt(0) == cChar)) {
                             // set the new selected index
                            Object o = new Integer(i);
                            pl.setProperty( ID.SELECT, o) ;
                            iCurSel = i ;
                            bFound = true ;
                        }
                        }catch(Exception ex) {;}
                        i++;
                    }                  
                }
                e.consume();
                log("consumed");
              }  
              }
            }
            public void keyReleased(KeyEvent e) {
            }
            public void keyTyped(KeyEvent e) {
            }
         };             
    }    
    
    // method to print a message on the console
    public void log( String sMessage )
    {
      if( bLog) System.out.println( "[" + this.getClass().getName() + "] " + sMessage ) ;
    }       
}
