/*
 * Copyright (c) 2011, Patrik Dufresne. All rights reserved.
 * Patrik Dufresne PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.planimod.ui.databinding;

import java.beans.PropertyDescriptor;

import org.eclipse.core.databinding.conversion.IConverter;
import org.eclipse.core.internal.databinding.beans.BeanPropertyHelper;

import com.planimod.core.EmployeePreference;

public class EmployeePreferredPositionValueProperty extends
		BeanComputedValueProperty {

	private String noneString;

	/**
	 * Create a new property value.
	 * 
	 * @param noneString
	 *            define the string value when the employee doesn't have a
	 *            preferred position.
	 */
	public EmployeePreferredPositionValueProperty(String noneString) {
		super(properties(), String.class);
		this.noneString = noneString;
	}

	/**
	 * Create a new property value.
	 */
	public EmployeePreferredPositionValueProperty() {
		this(""); //$NON-NLS-1$
	}

	/**
	 * Static function used to return the list of property descriptors for this
	 * computed value property.
	 * 
	 * @return
	 */
	private static PropertyDescriptor[] properties() {
		return new PropertyDescriptor[] {
				BeanPropertyHelper.getPropertyDescriptor(
						EmployeePreference.class,
						EmployeePreference.PREFERRED_POSITION),
				BeanPropertyHelper.getPropertyDescriptor(
						EmployeePreference.class,
						EmployeePreference.PREFERRED_POSITION_TEAM) };
	}

	private IConverter converter = Converters.removeFrontNumber();

	@Override
	protected Object doGetValue(Object source) {
		EmployeePreference pref = (EmployeePreference) source;
		if (pref.getPreferredPosition() == null
				|| pref.getPreferredPositionTeam() == null) {
			return this.noneString;
		}
		return this.converter
				.convert(pref.getPreferredPositionTeam().getName()) + " - " //$NON-NLS-1$
				+ pref.getPreferredPosition().getName();
	}
}
