/**
 * 
 */
package view.tool.jEasyTable;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.Border;


/**
 * @author alexis
 * cell of the JEasyTable, used to set border arround elements without modifying them
 */
public class JEasyTableCell extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 492688149943347590L;
	protected JComponent content;
	private final Border notSelected = BorderFactory.createLineBorder(Color.lightGray);
	private final Border selected = BorderFactory.createLineBorder(Color.darkGray);
	private final Color notselectedBackground = UIManager.getColor ( "Panel.background" );
	private final Color selectedBackground = new Color(173,216,230);
	private boolean isSelected = false;
	
	/**************************************************************************
	 * constructeur
	 *************************************************************************/

	/**
	 * Constructeur
	 * @param content JComponent a mettre
	 */
	protected JEasyTableCell(JComponent content) {
		super(new BorderLayout());
		this.content = content;
		this.add(content,BorderLayout.CENTER);
		this.setBorder(notSelected);
		this.setBackground(notselectedBackground);
	}
	
	/**
	 * @param content
	 * @return JEasyTableCell with no listener
	 */
	public static JEasyTableCell contentWithNoListener(JComponent content){
		JEasyTableCell cell = new JEasyTableCell(content);

		return cell;
	}
	
	/**
	 * @param content
	 * @param _obs
	 * @return JEasyTableCell with click listener
	 */
	public static JEasyTableCell contentWithClickListener(JComponent content,Observer _obs){
		JEasyTableCell cell = new JEasyTableCellClick(content,_obs);
		
		return cell;
	}
	
	/**
	 * @param content
	 * @param _obs
	 * @return JEasyTableCell with double click listener
	 */
	public static JEasyTableCell contentWithDoubleClickListener(JComponent content,Observer _obs){
		if (_obs==null)
			throw new IllegalArgumentException();
		JEasyTableCell cell = new JEasyTableCellDoubleClick(content,_obs);
		
		return cell;
	}
	
	/**
	 * @param content
	 * @param _obs
	 * @return JEasyTableCell with selection
	 */
	public static JEasyTableCell contentWithSelection(JComponent content,Observer _obs, int row){
		if (_obs==null)
			throw new IllegalArgumentException();
		JEasyTableCell cell = new JEasyTableCellSelectable(content,_obs,row);
		
		return cell;
	}
	
	/**************************************************************************
	 * getter
	 *************************************************************************/

	/**
	 * @return the content
	 */
	public JComponent getContent() {
		return content;
	}
	
	/**************************************************************************
	 * setter
	 *************************************************************************/

	/**
	 * @param content the content to set
	 */
	public void setContent(JComponent content) {
		this.content = content;
	}

	public void setSelected(Boolean isSelected){
		this.isSelected = isSelected;
		if(isSelected)
		{
			this.setBorder(selected);
			this.setBackground(selectedBackground);
		}
		else
		{
			this.setBorder(notSelected);
			this.setBackground(notselectedBackground);
		}
	}
	
	public boolean isSelected(){
		return isSelected;
	}
}
