package oracle.forms.fd;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import oracle.forms.handler.IHandler;
import oracle.forms.properties.ID;
import oracle.forms.ui.VTextField;

  /**
   * A multi-record Forms table's PJC
   * 
   * @author Francois Degrelle
   * @version 1.0
   */
   
  public class MultiTextField extends VTextField
  {
    public final static ID SETFGCOLOR     = ID.registerProperty("SET_FGCOLOR");    
    public final static ID SETBGCOLOR     = ID.registerProperty("SET_BGCOLOR");        
    public final static ID SETNEWREC      = ID.registerProperty("SET_NEW_REC");            
    public final static ID SETINDICE      = ID.registerProperty("SET_INDICE");                
    public final static ID PAINTRECORD    = ID.registerProperty("PAINT_RECORD");                
    public final static ID SETINTVALUE    = ID.registerProperty("SET_INT_VALUE");        
    public final static ID SETFLOATVALUE  = ID.registerProperty("SET_FLOAT_VALUE");        
    public final static ID SETCHARVALUE   = ID.registerProperty("SET_CHAR_VALUE");            
    public final static ID SETTOOLTIP     = ID.registerProperty("SET_TOOLTIP");    
    public final static ID SETHINT        = ID.registerProperty("SET_HINT");     
    public final static ID GETTOOLTIP     = ID.registerProperty("GET_TOOLTIP");    
    public final static ID GETHINT        = ID.registerProperty("GET_HINT");     
    public final static ID GETINTVALUE    = ID.registerProperty("GET_INT_VALUE");        
    public final static ID GETFLOATVALUE  = ID.registerProperty("GET_FLOAT_VALUE");        
    public final static ID GETCHARVALUE   = ID.registerProperty("GET_CHAR_VALUE");            
    public final static ID SETLOG         = ID.registerProperty("SET_LOG"); 
    public final static ID POPUPHELP_ID   = ID.registerProperty("POPUPHELP_ID"); 
    
    private String sSep = "|" ;
    private IHandler  m_handler;  
    private static  List liste = new ArrayList(10) ; 
    private   int  iCurRec = 1 ;
    private static int iIndice = 0 ;
    private boolean    bLog = false ;
    
    public MultiTextField ()
    {
       super();
    }

      
    public void init(IHandler handler)
    {
      m_handler = handler;
      super.init(handler);
    }      
    
    
    /*
     * set some properties
     */
    public boolean setProperty(ID property, Object value)
    {
      //
      // set the item indice
      //
      if (property == SETINDICE)
      {
        int i = Integer.parseInt(value.toString());
        iIndice=i;
        log("** SetIndice="+iIndice);
        return true;
      }          
      // set the foreground color
      else if (property == SETFGCOLOR)  
      {
        String s = value.toString() ;
        int i=0, iPos = s.indexOf(",") ;
        if(iPos>-1)
        {
          i = Integer.parseInt(s.substring(0,iPos)) ;
          String sColor = s.substring(iPos+1) ;
          Color c = getColor(sColor) ;
          log("** SetBGColor="+c);
          try{
            MultiProps mps = (MultiProps) liste.get(i-1) ;
            mps.setFGColor(c);
            liste.set(i-1, mps) ;
            setFGColor(i);
          }
          catch (Exception e) { log(" ** SetFGColor : error index") ; }          
        }
        return true;
      }   
      // set the background color
      else if (property == SETBGCOLOR)  
      {
        String s = value.toString() ;
        int i=0, iPos = s.indexOf(",") ;
        if(iPos>-1)
        {
          i = Integer.parseInt(s.substring(0,iPos)) ;
          String sColor = s.substring(iPos+1) ;
          Color c = getColor(sColor) ;
          log("** SetBGColor="+c);
          try{
            MultiProps mps = (MultiProps) liste.get(i-1) ;
            mps.setBGColor(c);
            liste.set(i-1, mps) ;
            setBGColor(i);
          }
          catch (Exception e) { log(" ** SetTooltip : error index") ; }          
        }
        return true;
      }         
      //
      // set a int value
      //
      else if (property == SETINTVALUE)
      {
        String s = value.toString() ;
        int i=0, iPos=0;
        StringTokenizer st = new StringTokenizer(s,",");
        if (st.hasMoreTokens())
        {
          i = Integer.parseInt(st.nextToken());
          int iVal = Integer.parseInt(st.nextToken()) ;
          try{
            MultiProps mps = (MultiProps) liste.get(i-1) ;
            mps.setValue(iVal);
            liste.set(i-1, mps) ;
            log("** SetIntValue["+i+"]="+iVal);
          }
          catch (Exception e) { log(" ** SetTooltip : error index") ; }          
        }
        return true;
      }  
      //
      // set a float value
      //
      else if (property == SETFLOATVALUE)
      {
        String s = value.toString() ;
        int i=0, iPos=0;
        StringTokenizer st = new StringTokenizer(s,",");
        if (st.hasMoreTokens())
        {
          i = Integer.parseInt(st.nextToken());
          float fVal = Float.parseFloat(st.nextToken()) ;
          try{
            MultiProps mps = (MultiProps) liste.get(i-1) ;
            mps.setValue(fVal);
            liste.set(i-1, mps) ;
            log("** SetFloatValue["+i+"]="+fVal);
          }
          catch (Exception e) { log(" ** SetTooltip : error index") ; }          
        }
        return true;
      }
      //
      // set a string value
      //
      else if (property == SETCHARVALUE)
      {
        String s = value.toString() ;
        int i=0, iPos=0;
        StringTokenizer st = new StringTokenizer(s,",");
        if (st.hasMoreTokens())
        {
          i = Integer.parseInt(st.nextToken());
          String sVal = st.nextToken() ;
          try{
            MultiProps mps = (MultiProps) liste.get(i-1) ;
            mps.setValue(sVal);
            liste.set(i-1, mps) ;
            log("** SetCharValue["+i+"]="+sVal);
          }
          catch (Exception e) { log(" ** SetTooltip : error index") ; }          
        }
        return true;
      }      
      // add a new item
      else if (property == SETNEWREC) 
      {
        int i = Integer.parseInt(value.toString());
        log("** SetNewRec="+i);
        MultiProps mps = new MultiProps(i) ;
        liste.add(mps); 
        return true;
      }         
      //
      // paint the item
      //
      else if (property == PAINTRECORD) 
      {
        int i = Integer.parseInt(value.toString());
        iCurRec = i ;
        try
        {
          MultiProps mps = (MultiProps) liste.get(iCurRec-1) ;
          setBGColor(i);
          setFGColor(i);
          oracle.forms.ui.FormsToolTip tp = mps.getTP() ;
          String sTooltip = mps.getTooltip();
          if(tp != null)
          {
              tp.setToolTipProperty(ID.INDEX_POPUPHELP_STRING,(Object)(sTooltip));
              setProperty(ID.POPUPHELP_ID,(Object)(tp));
              log("tooltip="+sTooltip);
          }
        }
        catch (Exception e) { log(" ** SetRecPos : error index") ; }
        return true;
      } 
      // tooltip
      else if (property == SETTOOLTIP) 
      {
        String s = value.toString();
        String sTooltip = "" ;
        StringTokenizer st = new StringTokenizer(s,",");
        int i = Integer.parseInt(st.nextToken());
        iCurRec = i ;
        if (st.hasMoreTokens())
        {
          sTooltip       = st.nextToken() ;
          try{
            MultiProps mps = (MultiProps) liste.get(i-1) ;
            mps.setTooltip(sTooltip);
            liste.set(i-1, mps) ;
            log("**** SetTooltip==>"+sTooltip);
            oracle.forms.ui.FormsToolTip tp = new oracle.forms.ui.FormsToolTip() ;
            if(tp != null)
            {
              tp.setToolTipProperty(ID.INDEX_POPUPHELP_STRING,(Object)(sTooltip));
              setProperty(ID.POPUPHELP_ID,(Object)(tp));
            }
          }
          catch (Exception e) { log(" ** SetTooltip : error index") ; e.printStackTrace();}
        }
        return true;
      }
      // hint
      else if (property == SETHINT) 
      {
        String s = value.toString();
        String sHint = "" ;
        StringTokenizer st = new StringTokenizer(s,",");
        int i = Integer.parseInt(st.nextToken());
        if (st.hasMoreTokens())
        {
          sHint       = st.nextToken() ;
          try{
            MultiProps mps = (MultiProps) liste.get(i-1) ;
            mps.setHint(sHint);
            liste.set(i-1, mps) ;
            log("** SetHint="+sHint);        
          }
          catch (Exception e) { log(" ** SetHint : error index") ; }
        }
        return true;
      }      
      // set the log flag
      else if (property == SETLOG) 
      {
        String s = value.toString();
        if(s.equalsIgnoreCase("true")) bLog = true ;
        else bLog = false ;
        return true;
      }         
       // tooltip
       else if(property == ID.POPUPHELP_ID)
       {
         oracle.forms.ui.FormsToolTip tp = (oracle.forms.ui.FormsToolTip)value ;
         String stool = tp.getToolTipProperty(ID.INDEX_POPUPHELP_STRING).toString() ;
         if(stool != null && ! stool.equals(""))
         {
           String sTooltip = "" ;
           StringTokenizer st = new StringTokenizer(stool,sSep);
           if(! st.hasMoreTokens())  sTooltip = stool; 
           else
           {
             while(st.hasMoreTokens())
             {
               sTooltip += st.nextToken() + "\n" ;
             }
           }
           tp.setToolTipProperty(ID.INDEX_POPUPHELP_STRING,(Object)(sTooltip));
           try{
             MultiProps mps = (MultiProps) liste.get(iCurRec-1) ;
             mps.setTP(tp);
             liste.set(iCurRec-1, mps) ;
             log("** SetTP ** for item="+iCurRec);        
           }           
           catch (Exception e) { log(" ** SetTP : error index") ; }
           return super.setProperty(property, value);
         }
         else return true ;
       }
      else
      {
       return super.setProperty(property, value);
      }
    }

   //
   // set the foreground color
   //
   protected void setFGColor(int i)
   {
          try{
            MultiProps mps = (MultiProps) liste.get(i-1) ;
            Color c = mps.getFGColor() ;
            if(c != null) this.setForeground(c);
          }
          catch (Exception e) { log(" ** SetFGColor : error index") ; }     
   }
   
   //
   // set the background color
   //
   protected void setBGColor(int i)
   {
          try{
            MultiProps mps = (MultiProps) liste.get(i-1) ;
            Color c = mps.getBGColor() ;
            if(c != null) this.setBackground(c);
          }
          catch (Exception e) { log(" ** SetBGColor : error index") ; }     
   }
   
   //
   //  Get the properties 
   //
   public Object getProperty(ID pId)
   {
     // tooltip
     if(pId == GETTOOLTIP)
     {
        try
        {
          MultiProps mps = (MultiProps) liste.get(iIndice-1) ;
          return "" + mps.getTooltip() ;   
        }
        catch (Exception e) { log(" ** getTooltip : error index") ; }
        return "" ;
     }
     // hint
     else if(pId == GETHINT)
     {
        try
        {
          MultiProps mps = (MultiProps) liste.get(iIndice-1) ;
          return "" + mps.getHint() ;   
        }
        catch (Exception e) { log(" ** getHint : error index") ; }
        return "" ;
     }   
     // int value
     else if(pId == GETINTVALUE)
     {
        try
        {
          log(" ** getIntValue indice="+(iIndice-1));
          MultiProps mps = (MultiProps) liste.get(iIndice-1) ;
          return "" + mps.getIValue() ;   
        }
        catch (Exception e) { log(" ** getIntValue : error index") ; }
        return "" ;
     }      
     // float value
     else if(pId == GETFLOATVALUE)
     {
        try
        {
          MultiProps mps = (MultiProps) liste.get(iIndice-1) ;
          return "" + mps.getFValue() ;   
        }
        catch (Exception e) { log(" ** getFloatValue : error index") ; }
        return "" ;
     }      
     // char value
     else if(pId == GETCHARVALUE)
     {
        try
        {
          MultiProps mps = (MultiProps) liste.get(iIndice-1) ;
          return "" + mps.getSValue() ;   
        }
        catch (Exception e) { log(" ** getCharValue : error index") ; }
        return "" ;
     }      
     else return super.getProperty(pId);
   } 
     
  // expects r,g,b values separated by commas  
  private Color getColor(String colourValue) 
  {    
    try{      
      int r,g,b;      
      int rPos, gPos;      
      rPos = colourValue.indexOf(",");      
      gPos = colourValue.indexOf(",", rPos + 1);      
      if (rPos < 1 || gPos < 1 || gPos + 1 == colourValue.length()) {        
          throw new Exception("Invalid colour");
      }      
      r = Integer.parseInt(colourValue.substring(0, rPos));      
      g = Integer.parseInt(colourValue.substring(rPos + 1, gPos));      
      b = Integer.parseInt(colourValue.substring(gPos + 1));        
      if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {        
         throw new Exception("Invalid colour");
      } 
  
      return new Color(r,g,b);
     } 
    catch(Exception e) {      
      return new Color(0,0,0);
    }  
  } 
  
  void log( String s )
  {
    if(bLog) System.out.println(s);
  }
  
  
}
