import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.LineBorder;
import javax.swing.JLabel;
/**
 * A specific implementation of a JTextField which can be used in a JTable, and the JTextField's 
 * background color will change depending on whether or not its text is valid.  This
 * class is used exclusively by the MultiFileDeleteDialog.
 * @author Michael Pell, Solutions Plus, Inc.
 */

public class JTextFieldTableCellRenderer extends JTextField implements TableCellRenderer 
{ 
	public JTextFieldTableCellRenderer() 
	{ 
		super(); 
		setOpaque(true); /* itīs essential */ 
		this.setBackground(Color.white);
		
	}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
	String text = (String) value;
	setText(text);
	MultiFileDeleteTableModel tableModel = null;
	EditFileNamesTableModel editTableModel = null;

	//Since this class is used by 2 different GUI classes, need to make a slight distinction.
	//I could solve this by creating an abstract super class or interface, but this works so I'll let it go for now
	try 
	{
		tableModel = (MultiFileDeleteTableModel) table.getModel();
		this.setBorder(new LineBorder(Color.black, 0));
		if (!tableModel.isTextValid(row, column))
		{
			setBackground(Color.red);
			this.setToolTipText("Invalid file name, see Help for details.");
		}
		else
		{
			setBackground(Color.white);
			this.setToolTipText(null);
		}
		return this;		
	}
	catch (ClassCastException e)
	{
		try
		{
			editTableModel = (EditFileNamesTableModel) table.getModel(); 
			this.setBorder(new LineBorder(Color.black, 0));
			if (!editTableModel.isTextValid(row, column))
			{
				setBackground(Color.red);
				this.setToolTipText("Invalid file name, see Help for details.");
			}
			else
			{
				setBackground(Color.white);
				this.setToolTipText(null);
			}
			return this;			
		}
		catch(ClassCastException e2)
		{
			return this;
		}
	}
	

}
}
