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 underlying "bean" that produces the ProgressBar
 * @see ProgressBarPJC
 * <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 26/Oct/2001<br>
 *
 * @author Steve Button
 * @author Duncan Mills
 */

import java.awt.Dimension;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Component;
import java.awt.FontMetrics;

import oracle.ewt.lwAWT.*;

public class ProgressBar extends LWComponent{

  protected int maxValue;
  protected int minValue;
  protected int value;
  protected int percent;
  private double scale;
  protected Color background;
  protected Color foreground;
  protected Color borderColor;
  protected Color fillColor;

  public ProgressBar(){
    this(0,100);
  }

  public ProgressBar(int min, int max){
    this(0,100,0);
  }

  public ProgressBar(int min, int max, int percent){
    super();
    this.maxValue = max;
    this.minValue = min;
    this.percent = percent;
    if(maxValue!=minValue){
      this.scale = 100.00/(maxValue-minValue);
    }
    this.value = this.minValue + (int)(this.percent*1.00/scale);
    this.foreground = Color.black;
    this.background = Color.white;
    this.borderColor = Color.black;
    this.fillColor = Color.blue;
    this.doLayout();
    this.setPercent(percent);
  }

  public void paint(Graphics g){
    g.setColor(this.borderColor);
    g.drawRect(0,0,this.getSize().width-1,this.getSize().height-1);
    g.setColor(this.background);
    g.fillRect(1,1,this.getSize().width-2,this.getSize().height-2);
    g.setColor(this.fillColor);
    g.fillRect(1,1,(int)Math.floor(percent*this.getSize().width/100.00),this.getSize().height-2);

    FontMetrics fm = g.getFontMetrics();
    g.setColor(this.foreground);
    g.drawString(percent+"%",(this.getSize().width/2)-(fm.stringWidth("100%")/2),this.getSize().height/2+3);

    this.doLayout();
  }

  public void update(Graphics g){
    super.paint(g);
  }

  public void setBackground(Color col){
    this.background=col;
    repaint();
  }

  public Color getBackground(){
    return this.background;
  }

  public void setFillColor(Color col){
    this.fillColor=col;
    repaint();
  }

  public Color getFillColor(){
    return this.fillColor;
  }

  public int getPercent(){
    return this.percent;
  }

  public void setPercent(int val){
    int old = this.percent;
    this.percent = val;
    this.value = this.minValue+(int)Math.round(percent*(maxValue-minValue)/100.00);
    repaint();
  }

  public int getValue(){
    return this.minValue + (int)(Math.round(this.percent*(maxValue-minValue)/100.00));
  }

  public void setMaxValue(int mv){
    if(mv>minValue){
      this.maxValue = mv;
      scale = 100.00/(maxValue-minValue);
      this.setPercent(percent);
      repaint();
    }
    else return;
  }

  public int getMaxValue(){
    return this.maxValue;
  }

  public void setMinValue(int mv){
    if(mv <maxValue){
      this.minValue = mv;
      scale = 100.00/(maxValue-minValue);
      this.setPercent(percent);
      repaint();
    }else return;
  }

  public int getMinValue(){
    return this.minValue;
  }

  public Dimension getMinimumSize(){
    return new Dimension(100,25);
  }

  public Dimension getPreferredSize(){
    return new Dimension(100,25);
  }
}













