/*******************************************************************************
 * 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.labo.gui.download;

import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;

import lu.tudor.santec.gecamed.core.gui.IconFetcher;
import lu.tudor.santec.gecamed.core.gui.widgets.GenericRenderer;
import lu.tudor.santec.gecamed.core.gui.widgets.animatedicon.AnimatedIcon;
import lu.tudor.santec.gecamed.labo.ejb.entity.beans.Connection;
import lu.tudor.santec.gecamed.labo.gui.LaboModule;

public class ConnectionRenderer extends GenericRenderer
	{
	private JCheckBox	m_Enabled;
		
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static ImageIcon m_ServerIcon 	   = IconFetcher.getScaledIcon (LaboModule.class,LaboModule.c_i32_Server,16);
	
	private static AnimatedIcon m_DownloadBusy	   = new AnimatedIcon (IconFetcher.getIcon(LaboModule.class, LaboModule.c_i16_DownloadBusy));
	private static ImageIcon 	m_DownloadSuccess  = IconFetcher.getIcon(LaboModule.class, LaboModule.c_i16_DownloadSuccess);
	private static ImageIcon 	m_DownloadFailed   = IconFetcher.getIcon(LaboModule.class, LaboModule.c_i16_DownloadFailed);
	
//---------------------------------------------------------------------------
//***************************************************************************
//* Constants	                                                            *
//***************************************************************************
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//***************************************************************************
//* Constructor	                                                            *
//***************************************************************************
//---------------------------------------------------------------------------

public ConnectionRenderer ()
	{
	m_Enabled = new JCheckBox ();	
	}

//---------------------------------------------------------------------------
//***************************************************************************
//* Primitives	                                                            *
//***************************************************************************
//---------------------------------------------------------------------------

private JLabel formatConnectionStatus (JLabel p_Label, Integer p_Status)
	{
	switch (p_Status)
		{
		case Connection.c_Idle:
			
			p_Label.setIcon (null);
			p_Label.setText (null);
			break;
			
		case Connection.c_Downloading:
			
			p_Label.setIcon (m_DownloadBusy);
			p_Label.setHorizontalAlignment (SwingConstants.CENTER);
			p_Label.setText (null);
			break;
			
		case Connection.c_Done:
			
			p_Label.setIcon (m_DownloadSuccess);
			p_Label.setHorizontalAlignment (SwingConstants.CENTER);
			p_Label.setText (null);
			break;
	
		case Connection.c_Failed:
			
			p_Label.setIcon (m_DownloadFailed);
			p_Label.setHorizontalAlignment (SwingConstants.CENTER);
			p_Label.setText (null);
			break;
	
		}
		
	return p_Label;	
	}

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

public Component getTableCellRendererComponent (JTable p_Table, Object p_Value,
        										 boolean p_IsSelected, 
        										 boolean p_HasFocus, 
        										 int p_Row, int p_Column)
	{
	Component	l_Renderer;
	JLabel		l_Label = null;
	int			l_Column;
	
	l_Renderer = super.getTableCellRendererComponent(p_Table, p_Value, 
												     p_IsSelected, p_HasFocus, 
												     p_Row, p_Column);		
	if (l_Renderer instanceof JLabel)
		{
		l_Label = (JLabel) l_Renderer;
		l_Column = modelColumn (p_Table,p_Column);
		
		switch (l_Column)
			{
			case ConnectionListModel.c_StatusColumn:
			
				l_Label = this.formatConnectionStatus(l_Label, (Integer)p_Value);
				l_Renderer = l_Label;
				break;
				
			case ConnectionListModel.c_EnabledColumn:
				
				m_Enabled.setBackground(l_Label.getBackground());
				m_Enabled.setHorizontalAlignment (SwingConstants.CENTER);
				m_Enabled.setSelected((Boolean)p_Value);
				l_Renderer = m_Enabled;
				break;

			case ConnectionListModel.c_ServerColumn:
				
				l_Label.setIcon(m_ServerIcon);
				l_Label.setText((String)p_Value);
				l_Label.setHorizontalAlignment(SwingConstants.LEFT);
				l_Renderer = l_Label;
				break;
			}	
		}
	
	return l_Renderer;
	}

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