/*
 * 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.util.Date;

import org.eclipse.core.databinding.observable.value.ComputedValue;
import org.eclipse.core.databinding.observable.value.IObservableValue;

import com.patrikdufresne.managers.ManagerException;
import com.planimod.core.PlanimodManagers;
import com.planimod.core.TimeRanges;

public class ObservableWeekStart extends ComputedValue {

	private IObservableValue value;

	private PlanimodManagers managers;

	public ObservableWeekStart(PlanimodManagers managers, IObservableValue value) {
		this.managers = managers;
		this.value = value;
	}

	@Override
	protected Object calculate() {
		if (!(this.value.getValue() instanceof Date)) {
			return null;
		}
		try {
			return TimeRanges.getWeekStart((Date) this.value.getValue(),
					this.managers.getApplicationSettingManager()
							.getFirstDayOfWeek());
		} catch (ManagerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
