package oracle.forms.fd;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import oracle.forms.handler.IHandler;
import oracle.forms.ui.VBean;
import oracle.forms.properties.ID;
import oracle.forms.ui.CustomEvent;

  /**
   * A Bean to add buttons at runtime
   *
   * @author Francois Degrelle
   * @version 1.0
   */
   
public class AddButton extends VBean implements ActionListener, FocusListener
{
  // variables
  public final static ID AddButton     = ID.registerProperty("ADD_BUTTON");    
  public final static ID SetFont       = ID.registerProperty("SET_FONT");  
  public final static ID SetBGColor    = ID.registerProperty("SET_BG_COLOR");  
  public final static ID SetFGColor    = ID.registerProperty("SET_FG_COLOR");    
  public final static ID GetBtPressed  = ID.registerProperty("GET_BT_PRESSED");      
  public final static ID ButtonPressed = ID.registerProperty("BUTTONPRESSED");    
  private IHandler    m_handler;  
  private JButton[]   tabButtons       = new JButton[100];
  private String       sButtonPressed  = "" ;
  
  public void actionPerformed(ActionEvent e) {
        // set the pressed button's ID
        sButtonPressed = e.getActionCommand() ;
        // send message to Forms that a button was pressed
        CustomEvent ce = new CustomEvent(m_handler, ButtonPressed);
        dispatchCustomEvent(ce);
    }
    
  public void init(IHandler handler)
  {
    m_handler = handler;
    super.init(handler);
    try
    {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      SwingUtilities.updateComponentTreeUI(this);
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }     
  }  
  
  public boolean setProperty(ID property, Object value)
  {
    /**
     * Add a button on the current canvas
     */
    if (property == AddButton)
    {
      Font fFont = new Font("Arial", Font.PLAIN, 8) ;
      int iNum = 0, x = 0, y = 0, w = 0, h = 0 ;
      String sLabel, sAction, string ;
      string = value.toString();
      StringTokenizer st = new StringTokenizer(string,",");
      iNum = Integer.parseInt(st.nextToken()) ; // button number in the table
      sLabel = st.nextToken() ;                 // label
      sAction = st.nextToken() ;                // action name
      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

      // add the new button to the table
      tabButtons[iNum] = new JButton(sLabel) ;
      tabButtons[iNum].setActionCommand(sAction);
      tabButtons[iNum].addActionListener(this);
      tabButtons[iNum].setBounds(x,y,w,h);
      tabButtons[iNum].setFont(fFont);
      // add the button on the parent's container (current Forms canvas)
      this.getParent().add(tabButtons[iNum]) ;

      return true;
    }
    /**
     *  Set the Font 
     */
    else if (property == SetFont)
    {
      String string, sFont ;
      int    iFontSize, iNum = 0 ;
      string = value.toString();
      StringTokenizer st = new StringTokenizer(string,",");
      iNum = Integer.parseInt(st.nextToken()) ;
      sFont = st.nextToken() ;
      iFontSize = Integer.parseInt(st.nextToken()) ;
      Font fFont = new Font(sFont, Font.PLAIN, iFontSize) ;
      tabButtons[iNum].setFont(fFont);
      return true ;
    }
    /**
     *  Set the background color
     */
    else if (property == SetBGColor)
    {
      String sColor = value.toString().trim();
      int r=-1, g=-1, b=-1, iNum = 0 ;
      StringTokenizer st = new StringTokenizer(sColor,",");
      iNum = Integer.parseInt(st.nextToken()) ;
      r = Integer.parseInt(st.nextToken()) ;
      g = Integer.parseInt(st.nextToken()) ;
      b = Integer.parseInt(st.nextToken()) ;    
      tabButtons[iNum].setBackground( new Color(r, g, b)) ;
      return true;
    }
    /**
     *  Set the foreground color
     */
    else if (property == SetFGColor)
    {
      String sColor = value.toString().trim();
      int r=-1, g=-1, b=-1, iNum = 0 ;
      StringTokenizer st = new StringTokenizer(sColor,",");
      iNum = Integer.parseInt(st.nextToken()) ;
      r = Integer.parseInt(st.nextToken()) ;
      g = Integer.parseInt(st.nextToken()) ;
      b = Integer.parseInt(st.nextToken()) ;    
      tabButtons[iNum].setForeground( new Color(r, g, b)) ;
      return true;
    }    
    else
    {
     return super.setProperty(property, value);
    }
  }  

  /**
    * Get the properties
    **/
   public Object getProperty(ID pId) // Get the current pressed button ID
   {
     
     if (pId == GetBtPressed)
     {
       return "" + sButtonPressed;
     }      
     else
     {
       return super.getProperty(pId);
     }
   } 

  public void focusGained(FocusEvent e)
     {
         if (e.getComponent() == this)
         {
             // put the focus on the component
             e.getComponent().requestFocus();
         }
    
         try
         {
             m_handler.setProperty(FOCUS_EVENT, e);
         }
         catch ( Exception ex )
         {
           ;
         }
     }
    
  public void focusLost(FocusEvent e)
     {     
     }
  
}
