/*
 *  Copyright (C) 2010 Lari Natri <lari.natri@gmail.com>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package compid.gui;

import net.aavalla.utils.general.StrUtils;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Lari Natri <lari.natri@gmail.com>
 */
public class CompidTable extends JTable {
    
    private boolean cellContentAsTooltip = true;
    private boolean tooltipBreakLines = true;
    private String tooltipBreakSymbol = "; "; //NOI18N
    private boolean includeBreakSymbol = true;
    private int tooltipMaxLineLength = 80;

    public CompidTable() {
        super();
    }

    public CompidTable(int numRows, int numColumns) {
        super(numRows, numColumns);
    }

    public CompidTable(Object[][] rowData, Object[] columnNames) {
        super(rowData, columnNames);
    }

    public CompidTable(TableModel dm) {
        super(dm);
    }

    public CompidTable(TableModel dm, TableColumnModel cm) {
        super(dm, cm);
    }

    public CompidTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm) {
        super(dm, cm, sm);
    }

    public void setCellContentAsTooltip(final boolean cellContentAsTooltip) {
        invokeInEDTifNeeded(new Runnable() {
            @Override
            public void run() {
                CompidTable.this.cellContentAsTooltip = cellContentAsTooltip;
            }
        });
    }

    public void setIncludeBreakSymbol(final boolean includeBreakSymbol) {
        invokeInEDTifNeeded(new Runnable() {
            @Override
            public void run() {
                CompidTable.this.includeBreakSymbol = includeBreakSymbol;
            }
        });
    }

    public void setTooltipBreakLines(final boolean tooltipBreakLines) {

        invokeInEDTifNeeded(new Runnable() {
            @Override
            public void run() {
                CompidTable.this.tooltipBreakLines = tooltipBreakLines;
            }
        });
    }

    public void setTooltipBreakSymbol(final String tooltipBreakSymbol) {
        invokeInEDTifNeeded(new Runnable() {
            @Override
            public void run() {
                CompidTable.this.tooltipBreakSymbol = tooltipBreakSymbol;
            }
        });
    }




    @Override
    public String getToolTipText(MouseEvent e) {
        if (!this.cellContentAsTooltip) {
            return super.getToolTipText(e);
        }

        java.awt.Point p = e.getPoint();
        int c = convertColumnIndexToModel(this.columnAtPoint(p));
        int r = convertRowIndexToModel(this.rowAtPoint(p));

        TableModel model = this.getModel();
        String tip = model.getValueAt(r,c).toString();
        
        if (this.tooltipBreakLines) {
            String[] tips_sym = null;
            if (this.tooltipBreakSymbol==null) { // do not brake by symbol, if null
                tips_sym = new String[1];
                tips_sym[0] = tip;
            } else {
                tips_sym = tip.split(this.tooltipBreakSymbol);
            }
            StringBuilder sb = new StringBuilder("<html>"); //NOI18N
            for (int i = 0; i < tips_sym.length; ++i) {
                if (this.tooltipMaxLineLength > 0) {
                    String[] tips_sym_len = StrUtils.slice(tips_sym[i], this.tooltipMaxLineLength);
                    for (int j = 0; j < tips_sym_len.length; ++j) {
                        if (j>0) {
                            sb.append("&nbsp;&nbsp;&nbsp;&nbsp;"); //NOI18N add emptiness in front of length-splitted lines
                        }
                        sb.append(tips_sym_len[j]);
                        if (j < tips_sym_len.length-1) {
                            sb.append("<br/>"); //NOI18N
                        }
                    }
                } else {
                    sb.append(tips_sym[i]);
                }
                if (i < tips_sym.length-1) {
                    if (this.includeBreakSymbol) {
                        sb.append(this.tooltipBreakSymbol);
                    }
                    sb.append("<br/>"); //NOI18N
                }
            }
            sb.append("</html>"); //NOI18N
            tip = sb.toString();
        }

        return tip;
    }


    public static void invokeInEDTifNeeded(Runnable runnable) {
        if (EventQueue.isDispatchThread()) {
            runnable.run();
        } else {
            SwingUtilities.invokeLater(runnable);
        }
    }

}
