// ------------------------------------------------
// OPCoach Training Projects
//  OPCoach 2009 http://www.opcoach.com
// ------------------------------------------------

package com.opcoach.training.rental.ui.views;

import java.text.SimpleDateFormat;

import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;

import com.opcoach.training.rental.Customer;
import com.opcoach.training.rental.Rental;
import com.opcoach.training.rental.RentalAgency;
import com.opcoach.training.rental.RentalObject;
import com.opcoach.training.rental.ui.Palette;
import com.opcoach.training.rental.ui.RentalUIActivator;
import com.opcoach.training.rental.ui.RentalUIConstants;
import com.opcoach.training.rental.ui.views.AgencyContentProvider.TNode;

/**
 * @author olivier
 */
public class AgencyLabelProvider extends LabelProvider implements IColorProvider, RentalUIConstants
{
		
	/** The choosen palette among the additional (may be null) */
	private Palette currentPalette;


	public AgencyLabelProvider()
	{
		initPalette();
	}

	private SimpleDateFormat df = new SimpleDateFormat("dd/MM");

	@Override
	public String getText(Object element)
	{
		String result = null;
		boolean displayCount = RentalUIActivator.getDefault().getPreferenceStore().getBoolean(DISPLAY_COUNT_PREF);

		if (element instanceof RentalAgency)
		{
			result = ((RentalAgency) element).getName();
		} else if (element instanceof TNode)
		{
			return ((TNode) element).getText(displayCount);
		}

		else if (element instanceof Customer)
		{
			result = ((Customer) element).getDisplayName();
		} else if (element instanceof RentalObject)
		{
			result = ((RentalObject) element).getName();
		} else if (element instanceof Rental)
		{
			result = element.toString();
		}

		return result;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
	 */
	@Override
	public Color getBackground(Object element)
	{
		return (currentPalette == null) ? null : currentPalette.getBackground(element);

	}

	@Override
	public Image getImage(Object element)
	{
		// TODO Auto-generated method stub
		Image result = null;
		ImageRegistry reg = RentalUIActivator.getDefault().getImageRegistry();

		if (element instanceof RentalAgency)
		{
			result = reg.get(AGENCY_KEY);
		} else if (element == RENTALS_NODE)
		{
			result = reg.get(RENTAL_KEY);
		} else if (element == CUSTOMERS_NODE)
		{
			result = reg.get(CUSTOMER_KEY);
		} else if (element == OBJECTS_NODE)
		{
			result = reg.get(RENTAL_OBJECT_KEY);
		}

		return result;

	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
	 */
	@Override
	public Color getForeground(Object element)
	{
		return  (currentPalette != null) ? currentPalette.getForeground(element) : null;
		
	}

	
	
	public void initPalette()
	{
		// Recupere la palette selectionne dans les preferences
		// (appel par le listener de preference store de l'agency view et par le constructeur du label provider)
		String val = RentalUIActivator.getDefault().getPreferenceStore().getString(COLOR_PROVIDER);
		currentPalette = (val == null) ? null : RentalUIActivator.getDefault().getPaletteManager().get(val);

	}

}

@@ -328,117 +328,374 @@
 ace.
-viewers.IColorProvider;%0Aimport org.eclipse.jface.viewers.LabelProvider;%0Aimport org.eclipse.swt.graphics.Color
+resource.JFaceResources;%0Aimport org.eclipse.jface.resource.StringConverter;%0Aimport org.eclipse.jface.viewers.IColorProvider;%0Aimport org.eclipse.jface.viewers.IFontProvider;%0Aimport org.eclipse.jface.viewers.LabelProvider;%0Aimport org.eclipse.swt.SWT;%0Aimport org.eclipse.swt.graphics.Color;%0Aimport org.eclipse.swt.graphics.Font;%0Aimport org.eclipse.swt.graphics.FontData
 ;%0Aim
@@ -1279,16 +1279,31 @@
 onstants
+, IFontProvider
 %0A%7B%0A%09%09%0A%09/
@@ -3633,10 +3633,301 @@
 );%0A%0A%09%7D%0A%0A
+%09@Override%0A%09public Font getFont(Object element)%0A%09%7B%0A%09%09if (element instanceof Customer)%0A%09%09%7B%0A%09%09%09return JFaceResources.getFontRegistry().get(CUSTOMER_KEY);%0A%09%09%7D else if (element instanceof RentalObject)%0A%09%09%09return JFaceResources.getFontRegistry().getBold(RENTAL_OBJECT_KEY);%0A%09%09%0A%09%09return null;%0A%09%7D%0A%0A
 %7D%0A

Deal with fonts for objects