/**
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 *
 * Copyright (C)
 * <a href="http://www.inab.org">Spanish National Institute of Bioinformatics (INB)</a>
 * <a href="http://www.bsc.es">Barcelona Supercomputing Center (BSC)</a>
 * <a href="http://inb.bsc.es">Computational Node 6</a>
 */

package org.inb.swing;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.JScrollPane;

/**
 * This is an extended JScrollPane component that paints a background image
 *
 * @author Dmitry Repchevsky
 */

public class DecoratedJScrollPane extends JScrollPane
{
    private BufferedImage logo;

    public DecoratedJScrollPane() {}

    public DecoratedJScrollPane(Component view)
    {
        super(view);
    }

    public DecoratedJScrollPane(Component view, int vsbPolicy, int hsbPolicy)
    {
        super(view, vsbPolicy, hsbPolicy);
    }

    public DecoratedJScrollPane(int vsbPolicy, int hsbPolicy)
    {
        super(vsbPolicy, hsbPolicy);
    }

    public void setBackgroundImage(BufferedImage logo)
    {
        this.logo = logo;
    }

    @Override
    public void paint(Graphics g)
    {
        viewport.setOpaque(false);

        paintComponent(g);
        paintBorder(g);
        paintChildren(g);
    }

    @Override
    public void paintComponent(Graphics g)
    {
        if (isOpaque())
        {
            // paint view background ourselves (if it is declared transparent)
            Component view = viewport.getView();
            if (view != null && !view.isOpaque())
            {
                Dimension dim = viewport.getExtentSize();

                g.setColor(view.getBackground());
                g.fillRect(0, 0, dim.width, dim.height);
            }
            
            // paint background image
            if (logo != null)
            {
                Graphics2D g2 = (Graphics2D)g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                Dimension dim = viewport.getExtentSize();

                g2.drawImage(logo, dim.width - logo.getWidth(), dim.height - logo.getHeight(), this);
            }
        }
    }
}
