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

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Collection;

import com.patrikdufresne.managers.ManagerException;
import com.patrikdufresne.managers.databinding.ManagedObjectComputedSet;
import com.planimod.core.Employee;
import com.planimod.core.GeneratePlanifContext;

/**
 * This observable set provide collection of employees with offer. This function
 * is wrapping the {@link GeneratePlanifContext#listEmployeeWithTaskProposed()}.
 * 
 * @author Patrik Dufresne
 * 
 */
public class EmployeesWithTaskProposedComputedSet extends
		ManagedObjectComputedSet {
	/**
	 * The managers.
	 */
	private GeneratePlanifContext context;
	private PropertyChangeListener listener = new PropertyChangeListener() {
		@Override
		public void propertyChange(PropertyChangeEvent evt) {
			getRealm().exec(new Runnable() {
				@Override
				public void run() {
					makeDirty();
				}
			});
		}
	};
	
	/**
	 * Create a new observable set of employee.
	 * 
	 * @param realm
	 *            the realm
	 * @param site
	 *            The view site to get the generate planif context
	 * @param lockedElements
	 *            the observable locked elements
	 */
	public EmployeesWithTaskProposedComputedSet(GeneratePlanifContext context) {
		super(context.getManagers(), Employee.class, null, null);
		this.context = context;
	}

	@Override
	public synchronized void dispose() {
		super.dispose();
		this.context = null;
	}

	/**
	 * This implementation query the database to get the employees qualify for
	 * all null planif event.
	 */
	@Override
	protected Collection doList() throws ManagerException {
		return this.context.getProposedTasks().employees();
	}

	@Override
	protected void makeDirty() {
		super.makeDirty();
	}

	@Override
	protected void startListening() {
		super.startListening();
		this.context.addPropertyChangeListener(
				GeneratePlanifContext.PROPOSED_TASKS,
				this.listener);
	}

	@Override
	protected void stopListening() {
		super.stopListening();
		this.context.removePropertyChangeListener(
				GeneratePlanifContext.PROPOSED_TASKS,
				this.listener);
	}

}