
package uk.co.wingpath.gui;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;

/**
* This class enables tooltips to be displayed for table cells.
* <p>The tooltip may be set using the {@code setToolTipText} method.
* <p>If the text in the table cell is too long to be displayed, the table cell
* text will be used as the tooltip, instead of the normal tooltip.
*/
public class WTableCellRenderer
    extends DefaultTableCellRenderer
{
    private String tooltip = null;

    public Component getTableCellRendererComponent (
        JTable table, Object value, boolean isSelected,
        boolean hasFocus, int row, int column)
    {
        Component component =
             super.getTableCellRendererComponent (table, value,
                isSelected, hasFocus, row, column);
        int width = table.getColumnModel ().getColumn (column).getWidth ();
        super.setToolTipText (
            Gui.selectToolTipText (tooltip, getText (), width));
        return component;
    }

    /**
    * Sets the text to be used as the normal tooltip.
    * <p>If the text in the table cell is too long to be displayed,
    * the table cell text will be used as the tooltip, instead of the
    * normal tooltip.
    * @param tooltip text to be used as the normal tooltip. May be {@code null}
    * if tooltip is not normally required.
    */
    public void setToolTipText (String tooltip)
    {
        this.tooltip = tooltip;
    }
}

