package slides;

import java.awt.*;
import javax.swing.*;

/**
 * A panel containing slide titles
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class TitlePanel extends JPanel
{
    private Color              highlight;
    private String             text;


    /**
     * Default constructor
     */
    public TitlePanel()
    {
	this(null);
    }


    /**
     * Explicit value constructor
     *
     * @param text   The text in the title
     */
    public TitlePanel(String text)
    {
	this.text = text;
	setBackground(null);
	setForeground(null);
	setHighlight(Color.white);
    }




    /**
     * Get the highlight color
     *
     * @return   The highlight color
     */
    public Color getHighlight()
    {
	return highlight;
    }





    /**
     * Get the minimum size of this component
     *
     * @return  The minimum size
     */
    public Dimension getMinimumSize()
    {
	return getPreferredSize();
    }




    /**
     * Get the preferred size of this component
     *
     * @return  The preferred size
     */
    public Dimension getPreferredSize()
    {
	FontMetrics        fm;
	int                h, w;

	fm = getFontMetrics(getFont());
	h = fm.getHeight()+fm.getMaxDescent()+20;
	w = fm.stringWidth(text) * 2;

	return new Dimension(w, h);
    }


    /**
     * Paint this component
     *
     * @param g   The Graphics context to use
     */
    public void paint(Graphics g)
    {
	Dimension           d;
	FontMetrics         fm;
	int                 bars, barHeight, barSpace;
	int                 barTop, barThickness, barWidth;
	int                 descent, h, i, middle, w, x;


	super.paint(g);
	barSpace = 6;
	barThickness = 3;
	bars = 5;

	if (text != null) 
        {
	    g.setFont(getFont());
	    fm = g.getFontMetrics();
	    
	    d      = getSize();
	    h      = fm.getHeight();
	    middle = d.height/2;

	    descent   = fm.getMaxDescent();
	    barWidth  = d.width*7/10;
	    barHeight = descent+h;
	    barTop    = middle-h/2-descent/2;
	    
	    
	    g.setColor(getHighlight());
	    g.fillRect(0, barTop, barWidth, barHeight);
	    
	    x = barWidth;
	    for (i=0; i < bars; i++) 
            {
		x += barSpace;
		g.fillRect(x, barTop, barThickness, barHeight);
		x += barThickness;
	    }

	    x = 5;
	    g.setColor(getForeground());
	    g.drawString(text, x, middle+h/2-descent/2);
	}
    }


    /**
     * Set the highlight color
     *
     * @param highlight   The highlight color
     */
    public void setHighlight(Color highlight)
    {
	this.highlight = highlight;
    }


    /**
     * Set the title text
     */
    public void setText(String text)
    {
	this.text = text;
	repaint();
    }
}
