package forms.fd.utilities;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.util.StringTokenizer;
import oracle.forms.handler.IHandler;
import oracle.forms.ui.CustomEvent;
import oracle.forms.properties.ID;
import oracle.forms.ui.VBean;

  /**
   * utilities.FJFont
   *
   * @author Francois Degrelle
   * @version 1.1
   */
   
public class FJFont extends VBean

{
    // properties you can set
    protected static final ID SET_FONT      = ID.registerProperty("SET_FONT");
    protected static final ID SET_STRING    = ID.registerProperty("SET_STRING"); 
    protected static final ID DRAW_FONT     = ID.registerProperty("DRAW_FONT");     
    // properties you can be get
    protected static final ID GET_WIDTH     = ID.registerProperty("GET_WIDTH");
    protected static final ID GET_HEIGHT    = ID.registerProperty("GET_HEIGHT");    
    protected static final ID GET_SIZE      = ID.registerProperty("GET_SIZE");        
    protected static final ID GET_FONTLIST  = ID.registerProperty("GET_FONTLIST");     
    protected static final ID GET_FIRSTFONT = ID.registerProperty("GET_FIRSTFONT");         
    protected static final ID GET_NEXTFONT  = ID.registerProperty("GET_NEXTFONT");         
    // events you can raise
    protected static final ID EVT_01   = ID.registerProperty("EVT_01");    
    // variables
    static IHandler mHandler;   
    private Font font = null;
    private String sText = "" ;
    private String sFontList="" ;
    private String sTabfonts[] = null ;
    private int iWidth=0, iHeight=0 ;
    private int iCurFont = 0;
    private boolean bLog = true ;
    
    // default constructor
    public FJFont()
    {
      super();
    }

    public void init(IHandler handler)
    {
      super.init(handler);
      mHandler = handler;
      getFontList();
    }      
 
    // get the width
    public static int getWidth (Font f, String s) 
    { 
       Rectangle2D rect = f.getStringBounds(s,new FontRenderContext(new AffineTransform(),true,true)); 
       return (int)rect.getWidth(); 
    }   
    
    // get the height
    public static int getHeight (Font f, String s) 
    { 
       Rectangle2D rect = f.getStringBounds(s,new FontRenderContext(new AffineTransform(),true,true)); 
       return (int)rect.getHeight(); 
    }    
    
    // get the font list
    public void getFontList()
    {
      sTabfonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
      sFontList = "" ;
      for (int i=0; i<sTabfonts.length; i++)
      {
        sFontList += sTabfonts[i] + "\n" ;
      }      
    }
   
 public void paint(Graphics g) {
      g.clearRect((int)this.getX(),(int)this.getY(),(int)this.getWidth(),(int)this.getHeight());      
      Font font = new Font( sTabfonts[iCurFont], Font.PLAIN, 14 ) ;
      g.setFont(font);
      g.setColor(Color.black);
      String change = "ABCDEFGHIJabcdefghij0123456789";
      g.drawString( change, 2, 20 );
 }    
 /**
   * Set the properties to the bean
   **/    
  public boolean setProperty(ID property, Object value)
  {
    if(property == SET_FONT) // set the font
    {
       String string = (String)value, sFontName="", sFontType="";
       int iSize=0, iType = Font.PLAIN;
       log("Set_font=" + string) ;       
       StringTokenizer st = new StringTokenizer(string,",");
       if (st.hasMoreTokens())
       {
         sFontName = st.nextToken() ; log("Fontname="+sFontName);
         if (st.hasMoreTokens())
         {
           sFontType = st.nextToken() ;log("FontType="+sFontType);
           if(sFontType.equalsIgnoreCase("B")) iType = Font.BOLD ;
           else if(sFontType.equalsIgnoreCase("N")) iType = Font.PLAIN ;           
           else if(sFontType.equalsIgnoreCase("I")) iType = Font.ITALIC ;
           else if(sFontType.equalsIgnoreCase("BI")) iType = Font.BOLD+Font.ITALIC ;
           else if(sFontType.equalsIgnoreCase("IB")) iType = Font.BOLD+Font.ITALIC ;
           if (st.hasMoreTokens())
           {
             iSize = Integer.parseInt(st.nextToken()) ;
             log("size="+iSize);
             font = new Font(sFontName,iType,iSize) ;
             iWidth = iHeight = 0 ;
           }           
         }
       }
       return true;       
      }
    else if(property == SET_STRING) // set the string to calculate bounding box
    {
       log("Set_string=" + (String)value) ;       
       sText   = (String)value ;
       if(font != null)
       {
         iWidth  = getWidth(font,sText);
         iHeight = getHeight(font,sText);
         log("size="+iWidth+","+iHeight);
       }
       return true;       
      }     
    else if(property == DRAW_FONT) // draw the selected font
    {
       String sValue = value.toString().trim() ;
       int iNum = Integer.parseInt(sValue) ;
       log("Draw_font=" + iNum) ;              
       if(iNum > 0 && iNum < sTabfonts.length )
       {
          iCurFont = iNum ;
          repaint();
       }
       return true;       
      }        
    else // default behaviour
       {
          return super.setProperty(property, value);
       }      
  }

 /**
   * Get the properties of the bean
   **/
  public Object getProperty(ID property)
  {
    if (property == GET_WIDTH)
    {
      // return the current width
      return "" + iWidth ;
    }
    if (property == GET_HEIGHT)
    {
      // return the current height
      return "" + iHeight ;
    }
    else if (property == GET_SIZE)
    {
      // return both width and height
      return "" + iWidth + "," + iHeight;
    }    
    else if (property == GET_FONTLIST)
    {
      // return font list
      return "" + sFontList;
    }     
    else if (property == GET_FIRSTFONT)
    {
      // return font list
      iCurFont = 0 ;      
      if(sTabfonts.length > 0) 
         return "" + sTabfonts[iCurFont++];
      else
         return "" ;
    }
    else if (property == GET_NEXTFONT)
    {
      // return font list
      if(++iCurFont < sTabfonts.length)
        return "" + sTabfonts[iCurFont];
      else
        return "" ;
    }    
    else // default behaviour
    {
      return super.getProperty(property);
    }
  }

 /**
   * Send a message to the Forms module
   **/
  public void dispatch_event( ID id )
  {
      CustomEvent ce = new CustomEvent(mHandler, id);
      dispatchCustomEvent(ce);
  }

 /*
  *  Log
  */
  private void log(String s)
  {
    if( bLog) System.out.println(s);
  }
}

