package oracle.forms.fd;
import java.awt.Color;
import java.awt.Font;
import java.util.StringTokenizer;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import oracle.forms.handler.IHandler;
import oracle.forms.ui.*;
import oracle.forms.properties.ID;

  /**
   * A bean to handle frames at runtime
   *
   * @author Francois Degrelle
   * @version 1.1
   */

public class frame extends VBean
{
  protected static final ID pInitFrame          = ID.registerProperty("INIT_FRAME");
  protected static final ID pSetText            = ID.registerProperty("SET_TEXT");  
  protected static final ID pSetFont            = ID.registerProperty("SET_FONT");  
  protected static final ID pSetBackground      = ID.registerProperty("SET_BACKGROUND");  
  protected static final ID pSetBounds          = ID.registerProperty("SET_BOUNDS");  
  protected static final ID pSetFrameTitleColor = ID.registerProperty("SET_FRAME_TITLE_COLOR");  
  protected static final ID pShow               = ID.registerProperty("SHOW");
  protected static final ID pHide               = ID.registerProperty("HIDE");  
  private   IHandler      m_handler;  
  private Border border, loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
  private TitledBorder   tabTitle = new TitledBorder("") ;
  private JComponent panel ;
  private Color canvascolor ;
    
  public void init(IHandler handler)
   {
     super.init(handler);   
     m_handler = handler;
     try
     {
       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
       SwingUtilities.updateComponentTreeUI(this);
     }
     catch (Exception ex)
     {
       ex.printStackTrace();
     }          
  }
  
  /**
   * Constructor 
   */
  public frame()
  {
    super();         
    panel = new myPanel(); 
    panel.setVisible(true);     
  } 


  public boolean setProperty(ID pId, Object pValue)
  {
    /********************************
     * Set the frame type and title *
     *******************************/
    if (pId == pInitFrame)
    {
      //if( canvascolor == null )  canvascolor = super.getParent().getBackground() ;
      //panel.setBackground(canvascolor);      

      int iNum = 0;
      String sType="", sTitle="", sFont="", sColor="", sJustif="", string, sWeight = "" , sWidth="";
      int iFontSize = 10, iWeight=Font.PLAIN, iBorderWidth, iWidth=2 ;
      string = pValue.toString();
      StringTokenizer st = new StringTokenizer(string,",");
      sType     = st.nextToken() ;     // frame Type
      iWidth = Integer.parseInt(st.nextToken()) ;
      if( sType.equalsIgnoreCase("LINE"))
      {
        border = BorderFactory.createLineBorder(new Color(125,80,30), iWidth) ;        
      }
      else
      {
        border = BorderFactory.createTitledBorder(loweredetched) ;                
      }
      tabTitle =  BorderFactory.createTitledBorder(border);
      panel.setBorder(tabTitle);      
      if (st.hasMoreTokens())
        sTitle     = st.nextToken() ;  // frame Title
      if (st.hasMoreTokens())
        sFont      = st.nextToken() ;  // frame Font
      if (st.hasMoreTokens())
        iFontSize  = Integer.parseInt(st.nextToken()) ;  // frame Font size      
      if (st.hasMoreTokens())
        {
          sWeight = st.nextToken() ;
          if( sWeight.equalsIgnoreCase("N")) iWeight = Font.PLAIN ;
          if( sWeight.equalsIgnoreCase("B")) iWeight = Font.BOLD ;
          if( sWeight.equalsIgnoreCase("I")) iWeight = Font.ITALIC ;
        }
      if (st.hasMoreTokens())
        sJustif    = st.nextToken() ; // frame Justification

      if( sFont != null ) 
      { 
         Font fFont = new Font(sFont, iWeight, iFontSize) ; 
         tabTitle.setTitleFont(fFont);
      }
      if ( sJustif != null )
      {
        if( sJustif.equalsIgnoreCase("left") ) tabTitle.setTitleJustification(TitledBorder.LEFT);
        if( sJustif.equalsIgnoreCase("right") ) tabTitle.setTitleJustification(TitledBorder.RIGHT);
        if( sJustif.equalsIgnoreCase("center") ) tabTitle.setTitleJustification(TitledBorder.CENTER);
      }
      tabTitle.setTitle(sTitle);
      this.getParent().add(panel) ;            
      return true;
    } 
    
    /********************************
     * Set the frame type and title *
     *******************************/
    else if (pId == pSetFont)
    {
      int iNum = 0;
      String string, sFont="", sWeight = "" , sWidth="";
      int iFontSize = 10, iWeight=Font.PLAIN ;
      string = pValue.toString();
      StringTokenizer st = new StringTokenizer(string,",");
      sFont      = st.nextToken() ;  // frame Font
      if (st.hasMoreTokens())
        iFontSize  = Integer.parseInt(st.nextToken()) ;  // frame Font size      
      if (st.hasMoreTokens())
        {
          sWeight = st.nextToken() ;
          if( sWeight.equalsIgnoreCase("N")) iWeight = Font.PLAIN ;
          if( sWeight.equalsIgnoreCase("B")) iWeight = Font.BOLD ;
          if( sWeight.equalsIgnoreCase("I")) iWeight = Font.ITALIC ;
          if( sWeight.equalsIgnoreCase("BI")) iWeight = Font.BOLD | Font.ITALIC ;
        }
      Font fFont = new Font(sFont, iWeight, iFontSize) ; 
      tabTitle.setTitleFont(fFont);
      panel.setVisible(false);
      panel.setVisible(true);      
      return true;
    }     
    
    /*******************************
     *  set the frame title color  *
     ******************************/
    else if (pId == pSetFrameTitleColor)
    {
      String sColor = pValue.toString().trim();
      int r=-1, g=-1, b=-1, iNum = 0 ;
      StringTokenizer st = new StringTokenizer(sColor,",");
      r = Integer.parseInt(st.nextToken()) ;
      g = Integer.parseInt(st.nextToken()) ;
      b = Integer.parseInt(st.nextToken()) ;    
      tabTitle.setTitleColor(new Color(r, g, b));
      panel.setVisible(false);
      panel.setVisible(true);      
      return true;
    }
    /************************************
     *  set the frame background color  *
     ***********************************/
    else if (pId == pSetBackground)
    {
      String sColor = pValue.toString().trim();
      int r=-1, g=-1, b=-1, iNum = 0 ;
      StringTokenizer st = new StringTokenizer(sColor,",");
      r = Integer.parseInt(st.nextToken()) ;
      g = Integer.parseInt(st.nextToken()) ;
      b = Integer.parseInt(st.nextToken()) ;    
      panel.setBackground(new Color(r, g, b));
      panel.setVisible(false);
      panel.setVisible(true);      
      return true;
    }         
    /********************
     *  set the bounds  *
     *******************/
    else if (pId == pSetBounds)
    {
      int x = 0, y = 0, w = 0, h = 0;
      String sLabel, string, sName ;
      string = pValue.toString();
      StringTokenizer st = new StringTokenizer(string,",");
      x = Integer.parseInt(st.nextToken()) ;    // X pos
      y = Integer.parseInt(st.nextToken()) ;    // Y pos
      w = Integer.parseInt(st.nextToken()) ;    // Width
      h = Integer.parseInt(st.nextToken()) ;    // Height
      panel.setBounds(x,y,w,h) ;
      panel.setVisible(false);
      panel.setVisible(true);
      return true;
    } 
    /************************
     *  set the title text  *
     ***********************/
    else if (pId == pSetText) 
    {
      String title="", sJustif="", string = pValue.toString() ;
      System.out.println("SET_TEXT="+string);      
      StringTokenizer st = new StringTokenizer(string,",");
      title  = st.nextToken() ;
      if (st.hasMoreTokens())
      {
        sJustif    = st.nextToken() ;   // title Justification      
        System.out.println("Justif=["+sJustif+"]");      
        if( sJustif.equalsIgnoreCase("left") ) tabTitle.setTitleJustification(TitledBorder.LEFT);
        if( sJustif.equalsIgnoreCase("right") ) tabTitle.setTitleJustification(TitledBorder.RIGHT);
        if( sJustif.equalsIgnoreCase("center") ) tabTitle.setTitleJustification(TitledBorder.CENTER);        
      }
      tabTitle.setTitle(title);
      panel.setVisible(false);
      panel.setVisible(true);
      return true;
    }    
    /********************
     *  Show the frame  *
     *******************/
    else if( pId == pShow)
    {
      panel.setVisible(true);
      return true;
    }   
    /********************
     *  Hide the frame  *
     *******************/
    else if( pId == pHide)
    {
      panel.setVisible(false);
      return true;
    }

    else
    {
      return super.setProperty(pId, pValue);
    }
  } 

} // frame

