package oracle.forms.demos;


/**
 * <H3><FONT COLOR="#336699">Simple Progress Bar Pluggable Java Component for Oracle Forms</FONT></H3>
 * This class is part of a Pluggable Java Component for Oracle Forms, that implements
 * a lightweight progressbar type widget as a JavaBean
 * The class is implemented using the Oracle ewt LWComponent class at it's base so
 * will work with both 1.3 based JREs such as Jinitiator, and older JREs such as
 * The Microsoft JVM
 * For a more comprehensive ProgressBar see the Swing based oracle.forms.demos.extendedUI.ProgressBar
 * class which is swing based and restricted to a 1.3+ JRE
 *
 * The class implements the PJC warpper code which links the underlying Bean to the Forms Bean Area Handler
 * using the IView interface that produces
 * @see ProgressBar
 *
 * The class implements the following Custom properties for the programmer to
 * use:
 * <ul><li>PROGRESS_PERCENT_VALUE</li>
 *     <li>PROGRESS_BACKGROUND_COLOR</li>
 *     <li>PROGRESS_FILL_COLOR</li>
 * </ul>
 * The values for PROGRESS_BACKGROUND_COLOR and PROGRESS_FILL_COLOR are only settable - not gettable
 * <br>
 * For more PJC Demos and White Papers see <a href="http://otn.oracle.com/products/forms" target="_blank">Forms on OTN</a>
 *
 * @version 9.0 19/Oct/2001<br>
 *
 * @author Steve Button
 * @author Duncan Mills
 */

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;

import oracle.forms.properties.ID;
import oracle.forms.handler.IHandler;
import oracle.forms.ui.CustomEvent;
import oracle.forms.ui.VBean;

/**
   declare the public class and extend the oracle.forms.ui.VBean class.

   The oracle.forms.ui.VBean class provides implements the
   oracle.forms.ui.IView interface and extends the EWT lightweight
   container class.  All Pluggable Java Components must implement
   the IView interface either directly or via subclassing from
   another class that implements the IView interface.

   By extending from the VBean interface we need only override the methods
   we desire and are not forced to provide an implementation for each of the
   methods defined in the IView interface since the VBean class does this
   for us
 */

public class ProgressBarPJC
       extends VBean
{

  // the private objects we use throughout
  private Component mComp;
  private IHandler mHandler;

  // register the Java Bean methods as form properties
  private static final ID PROGRESS_PERCENT_VALUE     = ID.registerProperty("PROGRESS_PERCENT_VALUE");
  private static final ID PROGRESS_BACKGROUND_COLOR  = ID.registerProperty("PROGRESS_BACKGROUND_COLOR");
  private static final ID PROGRESS_FILL_COLOR        = ID.registerProperty("PROGRESS_FILL_COLOR");

  // the public constructor for the class
  public ProgressBarPJC()
  {
    super();
    try{
      // create an instance of the bean we wish to use
      ClassLoader cl = getClass().getClassLoader();
      Object obj = Beans.instantiate(cl,"oracle.forms.demos.ProgressBar");

      // make a component representation of the instantiated bean
      mComp = (Component)obj;
      mComp.setVisible(true);
      mComp.setEnabled(true);
      add("Center",mComp);
    }
    catch(Exception e){
      e.printStackTrace();
    }
  }

  /**
    the initialization method called by the Forms applet immediately after
    the object is constructed.  A handler reference is passed to the object
    */
  public void init(IHandler handler)
  {
    super.init(handler);
    mHandler = handler;
  }

  /**
    the Forms applet calls this method to elimate the object.  The object
    can release any resources it is using prior to it's destruction
    */
  public void destroy()
  {
  }

  /**
    this method sets the value of the desired property to the value represented
    by the Object value. The properties are identified from the results of the
    registration process performed at an earlier stage
   */
  public boolean setProperty(ID pid, Object value)
  {
    if(pid == PROGRESS_PERCENT_VALUE)
    {
      int percent = ((Integer)value).intValue();
      ((oracle.forms.demos.ProgressBar)mComp).setPercent(percent);
      return true;
    }
    else if(pid == PROGRESS_BACKGROUND_COLOR)
    {
      Color color = getColor((String)value);
      if(color!=null)
        ((oracle.forms.demos.ProgressBar)mComp).setBackground(color);
      return true;
    }
    else if(pid == PROGRESS_FILL_COLOR)
    {
      Color color = getColor((String)value);
      if(color!=null)
        ((oracle.forms.demos.ProgressBar)mComp).setFillColor(color);
      return true;
    }
    else
      return super.setProperty(pid,value);
  }

  /**
    this method returns the value of requested property.
  */
  public Object getProperty(ID pid)
  {
    if(pid == PROGRESS_PERCENT_VALUE)
    {
      return " " +((oracle.forms.demos.ProgressBar)mComp).getPercent();
    }
    else
    {
      return super.getProperty(pid);
    }
  }

  /**
    given a String definition of a color, return
    the appropriate Color object.  Returns null on unknown color String
  */
  public Color getColor(String sColor)
  {
    Color color = null;
    if(sColor.equalsIgnoreCase("blue"))
      color = Color.blue;
    else if (sColor.equalsIgnoreCase("red"))
      color = Color.red;
    else if (sColor.equalsIgnoreCase("green"))
      color = Color.green;
    else if (sColor.equalsIgnoreCase("white"))
      color = Color.white;
    else if (sColor.equalsIgnoreCase("yellow"))
      color = Color.yellow;
    else if (sColor.equalsIgnoreCase("orange"))
      color = Color.orange;
    return color;
  }
}














