/* $Id$
 *
 * Copyright (c) 1999 Xenonsoft Limited. All Rights Reserved.
 *
 * This software is protected by copyright. You are hereby notified from
 * now by reading this message. This software is also the confidential 
 * and proprietary information of Xenonsoft Limited.  ("Confidential
 * Information"). 
 *
 * This software is distributed under the Xenonsoft Public end user 
 * License ("XPeL"), where the machine-readable source code is provided 
 * under the "Open Source" model.
 * For more information, please read the file "LICENSE-XPL.txt" 
 */

// Filename:`ProgressBar.java'
//
//
// Description:
//	  
//	An custom AWT progress bar with a text label message.
//
//	  
// Author:
//	Peter Pilgrim
//	Fri Jun 25 17:04:02 BST 1999
//	  
// RCS HEADER
// 
// $Author$
// $Date$
// $Source$
// $Revision$    $State$    $Locker$
//
// ================================================================================
// History
//	  $Log$
// 
//
package xenon.gamekit;

import java.applet.*;				// for Applet, AppletContext
import java.beans.*;				// for PropertyChangeListener
import java.awt.*;				// for Component, Graphics
import java.awt.event.*;			// for ActionEvent, ActionListener


/**
 * A custom AWT ProgressBar component
 * with an inbuilt text label message.
 *
 * @author Peter Pilgrim Mon Aug 16 00:59:58 BST 1999
 * @version $Id$
 */
public class ProgressBar extends Panel
{

    protected int		minimum;
    protected int		value;
    protected int		maximum;

    protected String		msg="****";
    protected Font		textFont;
    protected Color		textColor;
    protected int		gapSize;
    protected int		text_width;
    protected int		text_height;
    protected int		ytext;
    protected int		xbar, ybar;
    protected Dimension		preferredSize;
    
    public ProgressBar( int min, int val, int max )
    {
	this.minimum = min;
	this.value   = val;
	this.maximum = max;

	gapSize=2;
	textFont = new Font( "Dialog", Font.PLAIN, 12 );
	textColor = Color.black;
	preferredSize = new Dimension();
	
	recomputeSize();
    }
    
    /** sets the progress bar value */
    public synchronized void setValue( int v )
    {
	int oldvalue = value;
	value = v;
	if ( value != oldvalue )
	    repaint();
    }

    /** gets the progress bar value */
    public synchronized int getValue( )
    {
	return (value);
    }

    /** sets the progress bar minimum value */
    public synchronized void setMinimum( int m )
    {
	int oldvalue = minimum;
	minimum = m;
	if ( minimum != oldvalue )
	    repaint();
    }

    /** gets the progress bar minimum value */
    public synchronized int getMinimum( )
    {
	return (minimum);
    }

    /** sets the progress bar maximum value */
    public synchronized void setMaximum( int m )
    {
	int oldvalue = maximum;
	maximum = m;
	if ( maximum != oldvalue )
	    repaint();
    }

    /** gets the progress bar maximum value */
    public synchronized int getMaximum( )
    {
	return (maximum);
    }

    /** sets the gap size*/
    public void setGapSize( int g )
    {
	int oldvalue = gapSize;
	gapSize = g;
	if ( gapSize != oldvalue )
	    recomputeSize();
    }

    /** gets the gap size */
    public int getGapSize( )
    {
	return (maximum);
    }

    /** sets the text font */
    public void setTextFont( Font f )
    {
	textFont = f;
	repaint();
    }
    
    /** gets the text font */
    public Font getTextFont()
    {
	return (textFont);
    }
    
    /** sets the text color */
    public void setTextColor( Color c )
    {
	textColor = c;
	repaint();
    }
    
    /** gets the text color */
    public Color getTextColor()
    {
	return (textColor);
    }
    
    /** sets the message */
    public synchronized void setMessage( String m )
    {
	msg = m;
	repaint();
    }

    /** gets the message */
    public synchronized String getMessage()
    {
	return (msg);

    }

    /** get the preferred size */
    public Dimension getPreferredSize()
    {
	recomputeSize();
	return new Dimension(preferredSize);
    }
    

   /** recompute size of the component */
    protected void recomputeSize()
    {
 	int pref_width, pref_height;
	Toolkit toolkit = getToolkit();
	FontMetrics fm = toolkit.getFontMetrics( textFont );
	text_width = fm.stringWidth( msg );
	text_height = fm.getMaxAscent() + fm.getMaxDescent() + 24;
	pref_width = Math.max( text_width, 100 ) + 2*gapSize;
	pref_height = text_height + 2*gapSize; 

	ytext = gapSize + fm.getMaxAscent();

// 	   System.out.println("  text_width:"+text_width+
// 			      "  text_height:"+text_height+
// 			      "  ytext:"+ytext );
	
	xbar = gapSize;
	ybar = pref_height;
	pref_height += 32 + gapSize ;

	preferredSize.width = pref_width;
	preferredSize.height = pref_height;

// 	   System.out.println("  xbar:"+xbar+
// 			      "  ybar:"+ybar+
// 			      "  pref_width:"+pref_width+
// 			      "  pref_height:"+pref_height);
    }
    

    /** paint graphics */
    public void paint( Graphics g ) 
    {
	Dimension currSize = getSize();

	g.setColor( getBackground());
	g.fillRect( 0, 0, currSize.width, currSize.height );
	g.setFont( textFont );
	g.setColor( textColor );
	g.drawString( getMessage(), gapSize+24, ytext  );

	paintProgressBar( g, xbar, ybar, currSize.width-2*gapSize, 
			  currSize.height - 28 );
    }
    
    /** paint graphics progress bar */
    protected void paintProgressBar( Graphics g, int xp, int yp, int width, int height ) 
    {
	g.setColor( Color.magenta );
	g.fillRect( xp, yp, width, height );
	int xwidth;
	synchronized (this) {
	    int val = value;
	    if ( val < minimum ) 
		val = minimum;
	    else if ( val > maximum ) 
		val = maximum;
	    xwidth = (int)((double)(width-4.0)*((double)val-minimum )/
			   (maximum-minimum));
	}
	if ( value > 0 ) {
	    g.setColor( Color.yellow );
	    g.fillRect( xp+2, yp+2, xwidth, height-4 );
	}
    }

    public static void main( String [] args )
    {
	Frame frame = new Frame( "Progress Bar Test" );
	ProgressBar pbar = new ProgressBar( 0, 0, 100 );
	pbar.setMessage( "Load Internet Applet" );
	frame.add( pbar );
	frame.pack();
	frame.setVisible(true);
	for ( int n=0; n<10; ++n ) {
	    for ( int v=0; v<=100; v+=10 ) {
		pbar.setValue( v );
		try { Thread.sleep(333); }
		catch ( InterruptedException ie ) { }
	    }
	}

	System.exit(0);
    }
    
}

// fini

