/**
 * MediaSniper 3.0 (2008-08-02)
 * Copyright 2007 - 2008 Zach Scrivena
 * zachscrivena@gmail.com
 * http://mediasniper.sourceforge.net/
 *
 * Simple program for downloading media files from popular websites.
 *
 * TERMS AND CONDITIONS:
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.freeshell.zs.mediasniper;

import java.awt.Component;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;


/**
 * Table cell renderer for <code>TableProgressCell</code>.
 */
class TableProgressCellRenderer
        extends JProgressBar
        implements TableCellRenderer
{
    /** Download object corresponding to the currently rendered cell */
    volatile private Download download = null;


    /**
    * Constructor.
    *
    * @param fg
    *      foreground color
    * @param bg
    *      background color
    * @param fgSelected
    *      foreground color for selected cells
    * @param bgSelected
    *      background color for selected cells
    */
    TableProgressCellRenderer()
    {
        setStringPainted(true);
    }


    /**
    * Return the tooltip text for this cell.
    *
    * @return
    *      tooltip text
    */
    @Override
    public String getToolTipText()
    {
        final Download d = download;

        if (d != null)
        {
            return d.getToolTipText();
        }
        else
        {
            return null;
        }
    }


    /**
    * Return the JProgressBar component used for drawing the cell.
    */
    @Override
    public Component getTableCellRendererComponent(
            final JTable table,
            final Object val,
            final boolean isSelected,
            final boolean hasFocus,
            final int row,
            final int col)
    {
        final TableProgressCell c = (TableProgressCell) val;
        download = c.download;

        if ((c.percent >= 0) && (c.percent <= 100))
        {
            /* determinate mode */
            setValue(c.percent);
            setIndeterminate(false);

            if (c.percent < 100)
            {
                setString(c.text + " (" + c.percent + "%)");
            }
            else
            {
                setString(c.text);
            }
        }
        else
        {
            /* indeterminate mode */
            setString(c.text);
            setIndeterminate(true);
        }

        return this;
    }
}