/**
 * 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.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;


/**
 * Table cell renderer for <code>TableStringCell</code>.
 */
class TableStringCellRenderer
        extends JLabel
        implements TableCellRenderer
{
    /** foreground color */
    private final Color fg;

    /** background color */
    private final Color bg;

    /** foreground color for selected cells */
    private final Color fgSelected;

    /** background color for selected cells */
    private final Color bgSelected;

    /** 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
    */
    TableStringCellRenderer(
            final Color fg,
            final Color bg,
            final Color fgSelected,
            final Color bgSelected)
    {
        this.fg = fg;
        this.bg = bg;
        this.fgSelected = fgSelected;
        this.bgSelected = bgSelected;
    }


    /**
    * 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 TableStringCell c = (TableStringCell) val;
        download = c.download;

        setText(c.text);
        setHorizontalAlignment(c.align);

        if (isSelected)
        {
            setForeground(fgSelected);
            setBackground(bgSelected);
            setOpaque(true);
        }
        else
        {
            setForeground(fg);
            setBackground(bg);
            setOpaque(false);
        }

        return this;
    }
}