/**
 * 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.Dimension;
import java.awt.Insets;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;

/**
 * @author Dmitry Repchevsky
 */

public class ScrollableTextPane extends JScrollPane
{
    private JTextArea textArea;

    public ScrollableTextPane(String text)
    {
        textArea = new JTextArea(text);
        textArea.setEditable(false);
        textArea.setRows(Math.min(textArea.getLineCount(), 6));
        textArea.setCaretPosition(0);

        setViewportView(textArea);
    }

    @Override
    public Dimension getMinimumSize()
    {
        Dimension dim = textArea.getPreferredSize();

        Border border = getBorder();

        int hInsets;

        if (border == null)
        {
            hInsets = 0;
        }
        else
        {
            Insets insets = border.getBorderInsets(this);
            hInsets = insets.top + insets.bottom;
        }

        dim.setSize(0, dim.height + hInsets);

        return dim;
    }

    @Override
    public Dimension getPreferredSize()
    {
        return getMinimumSize();
    }
}
