package oracle.forms.fd;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.border.AbstractBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

public class CButton extends JButton
{
  protected boolean bGradient = false ;
  // gradient positions
  public static int LeftToRight = 1 ;
  public static int RightToLeft = 2 ;
  public static int UpToDown = 3 ;
  public static int DownToUp = 4 ;
  public static int UpLeftToDownRight = 5 ;  
  // text position
  public static int Left   = 1 ;
  public static int Center = 2 ;
  public static int Right  = 3 ;
  // colors
  protected Color   cStart   = Color.white ;
  protected Color   cEnd     = Color.blue ;
  protected Color   cFG      = Color.black ;
  protected Color   cBG      = null ;
  protected Color   cBack    = null ;
  protected Color   cBorder  = Color.gray ;  
  protected int     igrDir   = LeftToRight ;
  protected int     iTextPos = Center ;
  protected int     iImgPos  = Left ;
  protected Font    font = null ;
  // image
  protected Image[]   m_images = { null, null };
  protected Image     image = null ;
  protected ImageIcon ic    = null ;
  protected ImageIcon[] tic    = {null,null,null} ;
  protected int       iImg = 0 ;
  protected String    sImgPos = "CM" ;
  protected int       iX=0, iY=0, iW=0,iH=0 ;
  protected int       iImgW=0, iImgH=0 ;
  protected int[]     tiImgW={0,0,0},tiImgH={0,0,0} ;  
  // rounded parameters
  protected int       iArcWidth  = 40 ;
  protected int       iArcHeight = 40 ;
  protected int       iDec = 5 ;
  
  public CButton(String sLabel)
  {
    Border br1 = new RoundBorder ( ) ;
    Border br2 = BorderFactory.createBevelBorder ( BevelBorder.RAISED );
    Border br3 = BorderFactory.createCompoundBorder ( br2, br1 ) ;
    font = this.getFont() ;
    this.setBorder (br1) ;
    setNumImage(0);
  }
  protected void setFGcolor( Color c ) { cFG = c ; this.setForeground(c);}
  protected void setBGcolor( Color c ) { cBG = c ; this.setBackground(c);}
  protected void setShadowcolor( Color c ) { cBack = c ; }  
  protected void setBordercolor( Color c ) { cBorder = c ; }    
  protected void setGradientcolors( Color s, Color e ) { cStart = s ;  cEnd = e; bGradient = true ;}
  protected void setDirection(int iDirection) { igrDir = iDirection ; }
  protected void setTextPosition(int iPos) { iTextPos = iPos ; }
  protected void setImagePosition(String sPos) { sImgPos = sPos ; }  
  protected void setFonte(Font f) { font = f ; }
  protected void setRounded( int iW, int iH ) 
  { 
    iArcWidth = iW ;  
    iArcHeight = iH;
    if     (iArcWidth < 40 || iArcHeight < 40) iDec = 5 ;
    else if(iArcWidth < 60 || iArcHeight < 60) iDec = 8 ;
    else if(iArcWidth < 80 || iArcHeight < 80) iDec = 12 ;
  }  
  protected void setBTImage(Image i) 
  { 
    tic[0]  = null;
   
    if(i != null)
    {
      tic[0]   = new ImageIcon(i);
      tiImgW[0] = tic[0].getIconWidth() ;
      tiImgH[0] = tic[0].getIconHeight() ;
    }
    setNumImage(0);
  }
  protected void setImageON(Image i) 
  { 
    tic[1]  = null;
   
    if(i != null)
    {
      tic[1]  = new ImageIcon(i);
      tiImgW[1] = tic[1].getIconWidth() ;
      tiImgH[1] = tic[1].getIconHeight() ;
    }
  }
  protected void setImageOFF(Image i) 
  { 
    tic[2] = null;
   
    if(i != null)
    {
      tic[2] = new ImageIcon(i);
      tiImgW[2] = tic[2].getIconWidth() ;
      tiImgH[2] = tic[2].getIconHeight() ;
    }
  }
  protected void mouseON()
  {
     if(tic[1] != null)
      {
        setNumImage(1);       
        repaint();
      }    
  }
  protected void mouseOFF()
  {
     if(tic[0] != null)
      {
        setNumImage(0);       
        repaint();
      }    
  }
  protected void setNumImage(int iNum)
  {
    ic    = tic[iNum];
    iImgW = tiImgW[iNum] ;
    iImgH = tiImgH[iNum] ;
  }
  
  /*
   * Draw the button
   */
  public void paintComponent (Graphics g)
  {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g ;
      int iTx,iTy ;
      iX = (int)this.getBounds().getX() ;
      iY = (int)this.getBounds().getY() ;
      iW = (int)this.getBounds().getWidth() ;
      iH = (int)this.getBounds().getHeight() ;
      g2.setColor(cBG);
      g2.fill(new RoundRectangle2D.Double(4,4,iW-4,iH-4,iArcWidth,iArcHeight));
      if(bGradient)
      {
        if(igrDir == LeftToRight)
        {
          GradientPaint redtowhite = new GradientPaint(0,0,cStart,iW,iH,cEnd);
          g2.setPaint(redtowhite);
          g2.fill(new RoundRectangle2D.Double(3,3,iW-4,iH-5,iArcWidth,iArcHeight)) ;
        }
        else if(igrDir == RightToLeft)
        {
          GradientPaint redtowhite = new GradientPaint(iW,iH,cStart,0,0,cEnd);
          g2.setPaint(redtowhite);
          g2.fill(new RoundRectangle2D.Double(3,3,iW-4,iH-5,iArcWidth,iArcHeight)) ;
        }
        else if(igrDir == UpToDown)
        {
          GradientPaint redtowhite = new GradientPaint(0,0,cStart,0,iH,cEnd);
          g2.setPaint(redtowhite);
          g2.fill(new RoundRectangle2D.Double(3,3,iW-4,iH-5,iArcWidth,iArcHeight)) ;
        }        
        else if(igrDir == DownToUp)
        {
          GradientPaint redtowhite = new GradientPaint(0,iH,cStart,0,0,cEnd);
          g2.setPaint(redtowhite);
          g2.fill(new RoundRectangle2D.Double(3,3,iW-4,iH-5,iArcWidth,iArcHeight)) ;
        }            
      }
      // image to draw ?
      if (ic != null)
      {
         // paint the image
         Point p = SetImgCoordinate();
         ic.paintIcon(this,g2,p.x,p.y);
      }

      if(this.getText() == null) return ;
      
      // label
      FontMetrics fm = g2.getFontMetrics(font);  // metrics for this object
      Rectangle2D rect = fm.getStringBounds(this.getText(), g2); // size of string
      int textHeight  = (int)(rect.getHeight());
      int textWidth   = (int)(rect.getWidth());
      
      //... Center text horizontally and vertically
      if(iTextPos == Left)
      {
        iTx = 8;
        iTy = (this.getHeight() - textHeight) / 2  + fm.getAscent();
      }
      else if(iTextPos == Center)
      {
        iTx = (this.getWidth()  - textWidth)  / 2;
        iTy = (this.getHeight() - textHeight) / 2  + fm.getAscent();
      }      
      else
      {
        iTx = (this.getWidth()  - textWidth) - 10 ;
        iTy = (this.getHeight() - textHeight) / 2  + fm.getAscent();
      }                              

      g2.setFont(font);
      // draw shadow
      if(cBack != null)
      {
        g2.setColor(Color.white);
        g2.drawString(this.getText(), iTx-1, iTy-1);
      }
      g2.setColor(cFG);
      g2.drawString(this.getText(), iTx, iTy);
    
  }  

  /*
   * calculate the image x,y coordinate
   */
  private Point SetImgCoordinate()
  {
    int x=5, y=5 ;

    // image on top position
    if(sImgPos.equalsIgnoreCase("LT"))      { x = iDec; y = 5; }
    else if(sImgPos.equalsIgnoreCase("CT")) { x = (iW/2)-(iImgW/2); y = 5; }
    else if(sImgPos.equalsIgnoreCase("RT")) { x = (iW)-(iImgW)-iDec; y = 5; }
    // image on middle position
    else if(sImgPos.equalsIgnoreCase("LM")) { x = iDec; y = (iImgH/2)-iDec; }
    else if(sImgPos.equalsIgnoreCase("CM")) { x = (iW/2)-(iImgW/2); y = (iImgH/2)-iDec; }
    else if(sImgPos.equalsIgnoreCase("RM")) { x = (iW)-(iImgW)-iDec; y = (iImgH/2)-iDec; }
    // image on bottom position
    else if(sImgPos.equalsIgnoreCase("LB")) { x = iDec; y = iH-(iImgH); }
    else if(sImgPos.equalsIgnoreCase("CB")) { x = (iW/2)-(iImgW/2); y = iH-(iImgH)-iDec; }
    else if(sImgPos.equalsIgnoreCase("RB")) { x = (iW)-(iImgW)-iDec; y = iH-(iImgH)-iDec; }
    
    Point pt = new Point(x,y);
    return pt ;
  }
  
  class RoundBorder extends AbstractBorder 
  {
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) 
    {
      Graphics2D g2 = (Graphics2D) g ;
      g2.setColor (cBorder) ; 
      g2.setStroke(new BasicStroke(3.0f)); 
      g.drawRoundRect ( x , y , width , height , iArcWidth, iArcHeight) ;
    }
    public Insets getBorderInsets ( ) 
    {
      return new Insets (2,2,2,2) ;
    }
  }

}