/**
 * 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.ontology;

import java.awt.Dimension;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JScrollPane;
import org.inb.biomoby.client.gui.AbstractMobyComponent;
import org.inb.biomoby.shared.datatypes.MobyString;
import org.inb.biomoby.shared.ontology.Rules;
import org.inb.biomoby.client.gui.swing.RoundedMultilineLabel;

/**
 * @author Dmitry Repchevsky
 */

public class RulesComponent<T extends Rules> extends AbstractMobyComponent<T>
{
    private RoundedMultilineLabel label;

    public RulesComponent(T rules)
    {
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

        List<? extends MobyString> list = rules.getRules();

        if (list != null)
        {
            StringBuilder sb = new StringBuilder();

            for (MobyString mobyString : list)
            {
                String rule = mobyString.getString();

                if (rule != null && rule.length() > 0)
                {
                    sb.append(rule).append('\n');
                }
            }

            String str = sb.toString();

            if (str.length() > 0)
            {
                label = new RoundedMultilineLabel(sb.toString());
                add(new JScrollPane(label));
            }
        }
    }

    @Override
    public Dimension getMaximumSize()
    {
        if (label != null)
        {
            Dimension prf = label.getPreferredSize();
            Dimension max = label.getMaximumSize();

            return new Dimension(max.width, prf.height);
        }
        else
        {
            return new Dimension(0,0);
        }
    }

}
