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

import org.eclipse.core.databinding.conversion.Converter;
import org.eclipse.core.databinding.conversion.IConverter;

import com.planimod.core.Employee;

public class EmployeeViewerUtils {
	/**
	 * This function may be used to display the employee name (combined).
	 * 
	 * @param e
	 * @return
	 */
	public static String getName(Employee employee) {
		String name = "";//$NON-NLS-1$
		if (employee.getFirstname() != null) {
			name += employee.getFirstname();
		}
		if (employee.getLastname() != null) {
			if (employee.getFirstname() != null) {
				name += " "; //$NON-NLS-1$
			}
			name += employee.getLastname();
		}
		return name.trim();
	}

	/**
	 * Create a new converter to convert an employee object into a string. This
	 * implementation return the employee's fullname.
	 * 
	 * @return the converter
	 */
	public static IConverter fullnameConverter() {
		return new Converter(Employee.class, String.class) {
			@Override
			public Object convert(Object fromObject) {
				if (fromObject == null) {
					return ""; //$NON-NLS-1$
				}
				return getName((Employee) fromObject);
			}
		};
	}

}
