/*******************************************************************************
 * This file is part of GECAMed.
 * 
 * GECAMed is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License (L-GPL) as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GECAMed 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License (L-GPL)
 * along with GECAMed.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * GECAMed is Copyrighted by the Centre de Recherche Public Henri Tudor (http://www.tudor.lu)
 * (c) CRP Henri Tudor, Luxembourg, 2008
 *******************************************************************************/
package lu.tudor.santec.gecamed.core.gui.utils;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.TableCellRenderer;

import lu.tudor.santec.gecamed.core.gui.GECAMedColors;

/**
  * The TextWrappingCellRenderer is a TableCellRenderer intented to render
  * plain text, making sure that text is automatically wrapped inside bounding
  * box of Renderer.
  * @author nico.mack@tudor.lu
 */

public class TextWrappingCellRenderer extends JTextArea implements TableCellRenderer 
	{

//***************************************************************************
//* Class Constants                                                         *
//***************************************************************************
	
	private static final long serialVersionUID = 1L;
	    
    private static final int   c_Gap   = 2;
		
//---------------------------------------------------------------------------
//***************************************************************************
//* Constructor                                                             *
//***************************************************************************     
//---------------------------------------------------------------------------

public TextWrappingCellRenderer() 
	{
    setLineWrap(true);
    setWrapStyleWord(true);
	this.setBorder(BorderFactory.createEmptyBorder (c_Gap,c_Gap,c_Gap,c_Gap));
	}

//---------------------------------------------------------------------------
//***************************************************************************
//* Class Body                                                              *
//***************************************************************************     
//---------------------------------------------------------------------------

public Component getTableCellRendererComponent(JTable p_Table, Object p_Value,
          										boolean p_IsSelected, boolean hasFocus, 
          										int p_Row, int p_Column) 	
	{
	Color	l_Background;
	
	l_Background = ( p_Row % 2 == 0) ? GECAMedColors.c_OddLineBackground : 
									   GECAMedColors.c_EvenLineBackground;
	if (p_IsSelected) 
		 this.setBackground(p_Table.getSelectionBackground());
	else this.setBackground(l_Background);

	this.setFont (new Font (getFont().getName(), Font.PLAIN, 10));	

	
    setText((String)p_Value);//or something in value, like value.getNote()...
    setSize(p_Table.getColumnModel().getColumn(p_Column).getWidth(),
              getPreferredSize().height);
    
    if (p_Table.getRowHeight(p_Row) < getPreferredSize().height) 
    	{
    	p_Table.setRowHeight(p_Row, getPreferredSize().height);
    	}
    return this;
	}

//---------------------------------------------------------------------------
//***************************************************************************
//* End of Class                                                            *
//***************************************************************************     
//---------------------------------------------------------------------------

} 
