/**
 * 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.biomoby.client.gui.auxiliary;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import org.inb.biomoby.client.gui.swing.RoundedMultilineLabel;

/**
 * @author Dmitry Repchevsky
 */

public abstract class AbstractMultilineDescriptionMobyComponent extends AbstractDescriptionMobyComponent
{
    private Dimension dim = new Dimension(120, 16);
    
    private int width;
    private int height;
    
    private int style;
    private Font font;

    public AbstractMultilineDescriptionMobyComponent()
    {
        style = Font.PLAIN;
        
        addMouseListener(this);
        addMouseMotionListener(this);
    }
    
    protected abstract String getDescription();
    
    public void setFontStyle(int style)
    {
        this.style = style;
        
        width = 0;
        repaint();
    }
    
    @Override
    protected RoundedMultilineLabel getPopup()
    {
        String description = getDescription();

        if (description == null || description.length() == 0)
        {
            return null;
        }

        return new RoundedMultilineLabel(description);
    }
    
    @Override
    public Dimension getPreferredSize()
    {
        String description = getDescription();
        
        if (description == null || description.length() == 0)
        {
            return new Dimension();
        }
        
        return dim;
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        String description = getDescription();
        
        if (description != null)
        {
            Graphics2D g2d = (Graphics2D)g;

            final int w = getWidth();
            final int h = getHeight();
            
            if (w != width || h != height)
            {
                FontRenderContext context = new FontRenderContext(null, true, false);

                Rectangle2D bounds;
                int fSize = 36;

                do
                {
                    font = new Font(Font.SANS_SERIF, style, fSize--);
                    bounds = font.getStringBounds(description, context);
                }
                while (bounds.getHeight() > h);

                width = w;
                height = h;
            }

            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

            g.setFont(font);
            g.setColor(Color.DARK_GRAY);

            g.drawString(description, 0, height - g.getFontMetrics().getDescent());
        }
    }
}
