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

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

import java.util.Collection;

import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;

import com.opcoach.training.rental.RentalAgency;
import com.opcoach.training.rental.ui.RentalUIActivator;
import com.opcoach.training.rental.ui.RentalUIConstants;

/**
 * ElectriqueMonte
 * 
 * @author olivier
 */
public class AgencyContentProvider implements ITreeContentProvider, RentalUIConstants
{

	private static final Object[] EMPTY_RESULT = new Object[0];

	public AgencyContentProvider()
	{

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
	 */
	@Override
	public void dispose()
	{
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface
	 * .viewers.Viewer, java.lang.Object, java.lang.Object)
	 */
	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
	{
		/*
		 * if (newInput instanceof RentalAgency) agency = (RentalAgency)
		 * newInput; else agency = null;
		 */

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.
	 * Object)
	 */
	@Override
	public Object[] getChildren(Object parentElement)
	{
		Object[] result = null;

		if (parentElement instanceof TNode)
		{
			return ((TNode) parentElement).getChildren();

		} else if (parentElement instanceof RentalAgency)
		{
			RentalAgency a = (RentalAgency) parentElement;
			return new TNode[] { new TNode(CUSTOMERS_NODE, a), new TNode(RENTALS_NODE, a), new TNode(OBJECTS_NODE, a) };
		}

		return (result == null) ? EMPTY_RESULT : result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object
	 * )
	 */
	@Override
	public Object getParent(Object element)
	{
		Object result = null;

		if (element instanceof TNode)
		{
			result = ((TNode) element).agency;
		}
		return result;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.
	 * Object)
	 */
	@Override
	public boolean hasChildren(Object element)
	{
		return ((element instanceof RentalAgency) || (element instanceof TNode));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java
	 * .lang.Object)
	 */
	@Override
	public Object[] getElements(Object inputElement)
	{
		if (inputElement instanceof Collection<?>)
		{
			return ((Collection<?>) inputElement).toArray();
		}

		return EMPTY_RESULT;
	}

	/** A private class to manage the logical nodes in tree */
	class TNode
	{
		public String name;

		public RentalAgency agency;

		public TNode(String n, RentalAgency a)
		{
			name = n;
			agency = a;
		}

		public String getName()
		{
			return name;
		}

		public Image getImage()
		{
			ImageRegistry reg = RentalUIActivator.getDefault().getImageRegistry();

			if (name == RENTALS_NODE)
			{
				return reg.get(RENTAL_KEY);
			} else if (name == CUSTOMERS_NODE)
			{
				return reg.get(CUSTOMER_KEY);
			} else if (name == OBJECTS_NODE)
			{
				return reg.get(RENTAL_OBJECT_KEY);
			}
			return null;
		}

		public Object[] getChildren()
		{

			if (CUSTOMERS_NODE == name)
			{
				return agency.getCustomers().toArray();
			} else if (RENTALS_NODE == name)
			{
				return agency.getRentals().toArray();
			} else if (OBJECTS_NODE == name)
			{
				return agency.getObjectsToRent().toArray();
			}
			return EMPTY_RESULT;

		}

		public String getText(boolean displayCount)
		{
			if (CUSTOMERS_NODE == name)
			{
				return CUSTOMERS_NODE + (displayCount ? "(" + agency.getCustomers().size() + ")" : "");
			} else if (RENTALS_NODE == name)
			{
				return RENTALS_NODE + (displayCount ? "(" + agency.getRentals().size() + ")" : "");
			} else if (OBJECTS_NODE == name)
			{
				return OBJECTS_NODE + (displayCount ? "(" + agency.getObjectsToRent().size() + ")" : "");
			}
			return "No Text for TNode";

		}

		// To refresh the expanded nodes, this class must implement equals and
		// hascode
		@Override
		public int hashCode()
		{
			return name.hashCode();
		}

		@Override
		public boolean equals(Object arg0)
		{
			if (arg0 instanceof TNode)
			{
				TNode other = (TNode) arg0;
				return (other.name.equals(name)) && (other.agency == agency);
			}
			return false;
		}

	}
}

@@ -3874,431 +3874,76 @@
 %0A%09%09%09
-if (CUSTOMERS_NODE == name)%0A%09%09%09%7B%0A%09%09%09%09return CUSTOMERS_NODE + (displayCount ? %22(%22 + agency.getCustomers().size() + %22)%22 : %22%22);%0A%09%09%09%7D else if (RENTALS_NODE == name)%0A%09%09%09%7B%0A%09%09%09%09return RENTALS_NODE + (displayCount ? %22(%22 + agency.getRentals().size() + %22)%22 : %22%22);%0A%09%09%09%7D else if (OBJECTS_NODE == name)%0A%09%09%09%7B%0A%09%09%09%09return OBJECTS_NODE + (displayCount ? %22(%22 + agency.getObjectsToRent().size() + %22)%22 : %22%22);%0A%09%09%09%7D%0A%09%09%09return %22No Text for TNode%22
+return name + (displayCount ? %22(%22 + getChildren().length + %22)%22 : %22%22)
 ;%0A%0A%09

Simplify the getText method in node