package oracle.forms.fd;

import java.awt.AWTEventMulticaster;
import java.awt.Color;
import java.awt.Cursor;
import oracle.forms.handler.IHandler;
import oracle.forms.properties.ID;
import oracle.forms.ui.VTextField;

  /*
   * A PJC to have a textfield blinking
   * 
   * @author Francois Degrelle
   * @version 1.0 
   */

  public class BlinkTextField extends VTextField  implements Runnable
  {
    public final static ID START        = ID.registerProperty("START");  
    public final static ID STOP         = ID.registerProperty("STOP");  
    public final static ID SETBLINKRATE = ID.registerProperty("SETBLINKRATE");    
    public final static ID SETFGCOLOR   = ID.registerProperty("SETFGCOLOR");    
    public final static ID SETBGCOLOR   = ID.registerProperty("SETBGCOLOR");        
    static Thread runner ;
    protected int seconds   = 400 ;  // milliseconds
    private Color  cdefFG   = null ; // default background color
    private Color  cdefBG   = null ; // default foreground color
    private Color  cFore    = null ; // blinking foreground color
    private Color  cBack    = null ; // blinking background color
    private boolean bSwitch = true ;
    private   IHandler  m_handler;  
    
    public BlinkTextField ()
    {
       super();
    }

      
    public void init(IHandler handler)
    {
      m_handler = handler;
      super.init(handler);
      cdefFG = this.getForeground() ;
      cdefBG = this.getBackground() ;
    }      
    
    
    /*
     * switch color to simulate the blinking process
     */
    public void run()
    {
      Thread theThread = Thread.currentThread();
      while (runner == theThread)
      {
        try{
        Thread.sleep(seconds);
        } catch (InterruptedException e) { }
        if(bSwitch)
        {
          if(cBack != null) this.setBackground(cBack);
          if(cFore != null) this.setForeground(cFore);
        }
        else
        {
          this.setBackground(cdefBG);        
          this.setForeground(cdefFG);
        }
        bSwitch = ! bSwitch ;
      }
    }  

    private void startTimer()
    {
      if (runner == null )
      {
        runner = new Thread(this);
        runner.start();
      }
    }
    
    private static void stopTimer()
    {
      if (runner != null )
      {
        runner = null;
      }
    } 
    
    public boolean setProperty(ID property, Object value)
    {
      if (property == START)  // start the blinking
      {
        startTimer() ;
        System.out.println("** Start **");
        return true;
      }
      if (property == STOP)  // stop the blinking
      {
        System.out.println("** Stop **");
        stopTimer() ;
        return true;
      }      
      else if (property == SETBLINKRATE)  // set the cursor blink rate
      {
        int i = Integer.parseInt(value.toString());
        seconds=i;
        System.out.println("** SetRate="+seconds+ " miliseconds");
        return true;
      }   
      else if (property == SETFGCOLOR)  // set the foreground color
      {
        cFore = getColor(value.toString()) ;
        System.out.println("** SetFGColor="+cFore);
        return true;
      }   
      else if (property == SETBGCOLOR)  // set the background color
      {
        cBack = getColor(value.toString()) ;
        System.out.println("** SetBGColor="+cBack);
        return true;
      }         
      else
      {
       return super.setProperty(property, value);
      }
    }

// expects r,g,b values separated by commas  
public 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);
  }  
} 


  }
