/*
 * 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.property.value.IValueProperty;
import org.eclipse.core.internal.databinding.beans.BeanPropertyHelper;

import com.patrikdufresne.util.DateFormatRegistry;
import com.planimod.core.AbstractCalendarEvent;

/**
 * This value property return a formated date time range using the
 * DateFormatRegistry.
 * 
 * @author Patrik Dufresne
 * 
 */
public class DateFormatRangeValueProperty extends BeanComputedValueProperty {

	/**
	 * Create a new instance of this class with the style DATE | MEDIUM.
	 * 
	 * @return new instance of this class
	 */
	public static DateFormatRangeValueProperty createDateMedium() {
		return new DateFormatRangeValueProperty(DateFormatRegistry.DATE
				| DateFormatRegistry.MEDIUM);
	}

	/**
	 * Create a new instance of this class with the style DATE | SHORT.
	 * 
	 * @return
	 */
	public static IValueProperty createDateShort() {
		return new DateFormatRangeValueProperty(DateFormatRegistry.DATE
				| DateFormatRegistry.SHORT);
	}

	/**
	 * Create a new instance of this class with the style TIME | MEDIUM.
	 * 
	 * @return new instance of this class.
	 */
	public static IValueProperty createTimeMedium() {
		return new DateFormatRangeValueProperty(DateFormatRegistry.TIME
				| DateFormatRegistry.MEDIUM);
	}

	/**
	 * 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(
						AbstractCalendarEvent.class,
						AbstractCalendarEvent.START_DATE),
				BeanPropertyHelper.getPropertyDescriptor(
						AbstractCalendarEvent.class,
						AbstractCalendarEvent.END_DATE) };
	}

	private int style;

	/**
	 * Create a new property value.
	 */
	public DateFormatRangeValueProperty(int style) {
		super(properties(), String.class);
		this.style = style;
	}

	@Override
	protected Object doGetValue(Object source) {
		AbstractCalendarEvent event = (AbstractCalendarEvent) source;
		return DateFormatRegistry.formatRange(event.getStartDate(),
				event.getEndDate(), this.style);
	}
}
