package oracle.forms.fd;

import java.awt.geom.RoundRectangle2D;
import java.util.StringTokenizer;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import oracle.forms.ui.VBean;
import oracle.forms.handler.IHandler;
import oracle.forms.properties.ID;

  /**
   * A javabean that paint the current canevas
   * with a gradient content
   *
   * @author Francois Degrelle
   * @version 1.0
   */

public class PaintCanevas extends VBean {
    public final static ID setGradient   = ID.registerProperty("SET_GRADIENT_COLORS");  
    public final static ID setDirection  = ID.registerProperty("SET_GRADIENT_DIRECTION");      
    public final static ID setHCycle     = ID.registerProperty("SET_H_CYCLE");          
    public final static ID setVCycle     = ID.registerProperty("SET_V_CYCLE");
    // gradient positions
    protected static int LeftToRight = 1 ;
    protected static int UpToDown = 2 ;
    protected static int LeftUpToRightDown = 3 ;
    protected static int LeftDownToRightUp = 4 ;
    protected int        iDirection = LeftToRight ;
    // other variables
    protected  boolean   bFirst = true ;
    protected  Color     c1=null, c2=null ;
    protected  int       iX=0, iY=0, iW=0, iH=0 ;
    protected  int       iHcycle=0, iVcycle=0 ;
    protected  JPanel    jp = null;

    public PaintCanevas() 
    {             
    }

  public void paint(Graphics g)
  {
    if(bFirst)
    {
        bFirst = false ;
        jp = new JPanel()
        {
          /*
           * paint the canevas
           */
          public void paint(Graphics g)
         {
           Graphics2D g2 = (Graphics2D) g ;
           iX = (int)this.getBounds().getX() ;
           iY = (int)this.getBounds().getY() ;
           iW = (int)this.getBounds().getWidth() ;
           iH = (int)this.getBounds().getHeight() ;
           int x=0,y=0,w=0,h=0;
           if(c1 != null && c2 != null)
           {
             if(iDirection == LeftToRight)
             {
               x = 0; y = 0; w = iW ; h = 0 ;
             }
             else if(iDirection == UpToDown)
             {
               x = 0; y = 0; w = 0 ; h = iH ;
             }             
             else if(iDirection == LeftUpToRightDown)
             {
               x = 0; y = 0; w = iW ; h = iH ;
             }                          
             else if(iDirection == LeftDownToRightUp)
             {
               x = 0; y = iH; w = iW ; h = 0 ;
             }                          
             // must have some transparancy to not completly
             // hidden the Forms prompts
             g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
             // create the gradient
             GradientPaint gp = null ;
             if(iHcycle+iVcycle == 0)
             {
               gp = new GradientPaint(x,y,c1,w,h,c2);
             }
             else
             {
               gp = new GradientPaint(0,0,c1,iHcycle,iVcycle,c2,true);
             }
             g2.setPaint(gp);
             // fill the canevas
             g2.fill(new RoundRectangle2D.Double(0,0,iW,iH,5,5)) ;
           }    
         }
        };
        // give the JPanel the same size as the parent's canevas
        jp.setBounds(getParent().getBounds());
        jp.setVisible(true);
        getParent().add(jp);
    }
  }


  public boolean setProperty(ID property, Object value)
  {
    /***************************
     * set the gradient colors *
     **************************/
    if (property == setGradient) 
    {
            String sColor = value.toString().trim();
            int r = -1;
            int g = -1;
            int b = -1;
            StringTokenizer st = new StringTokenizer(sColor, ",");
            try
            {
                r = Integer.parseInt(st.nextToken());
                g = Integer.parseInt(st.nextToken());
                b = Integer.parseInt(st.nextToken());
                c1 = new Color(r, g, b);
                r = -1;
                g = -1;
                b = -1;
                r = Integer.parseInt(st.nextToken());
                g = Integer.parseInt(st.nextToken());
                b = Integer.parseInt(st.nextToken());
                c2 = new Color(r, g, b);
                System.out.println("SET_GRADIENT");
            }
            catch(Exception e)
            {
                e.printStackTrace();
                return false;
            }
      if(jp != null) jp.invalidate();
      return true;
    }
    // set the gradient direction
    else if (property == setDirection) 
    {
      String s = value.toString().trim();
      if(s.equalsIgnoreCase("LeftToRight")) iDirection = 1 ;
      else if(s.equalsIgnoreCase("UpToDown")) iDirection = 2 ;
      else if(s.equalsIgnoreCase("LeftUpToRightDown")) iDirection = 3 ;
      else if(s.equalsIgnoreCase("LeftDownToRightUp")) iDirection = 4 ;      
      if(jp != null) jp.invalidate();
      return true ;
    }
    // set the horizontal cycle
    else if (property == setHCycle) 
    {
      String s = value.toString().trim();
      int i = 0 ;
      if(s.startsWith("/"))
      {
        i = Integer.parseInt(s.substring(1));
        iHcycle = iW / i ;
      }
      else
      {
        i = Integer.parseInt(s);
        iHcycle = i ;
      }
      if(jp != null) jp.invalidate();
      return true ;
    }
    // set the vertical cycle
    else if (property == setVCycle) 
    {
      String s = value.toString().trim();
      int i = 0 ;
      if(s.startsWith("/"))
      {
        i = Integer.parseInt(s.substring(1));
        iVcycle = iH / i ;
      }
      else
      {
        i = Integer.parseInt(s);
        iVcycle = i ;
      }
      if(jp != null) jp.invalidate();
      return true ;
    }    
    else
    {
     return super.setProperty(property, value);
    }
  }
  

}
