/*
 * 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.util.HashSet;
import java.util.Set;

import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.observable.set.IObservableSet;
import org.eclipse.core.databinding.observable.set.ISetChangeListener;
import org.eclipse.core.databinding.observable.set.SetChangeEvent;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.viewers.LabelProviderChangedEvent;

/**
 * This label provide display a label using an IObserbale map and add an
 * annotation if the element is part of an IObservable set.
 * 
 * @author Patrik Dufresne
 * 
 */
public class EmployeeLabelWithAnnotation extends ObservableMapLabelProvider {

	private static final String ANNOTATION = " *"; //$NON-NLS-1$

	private ISetChangeListener setChangeListener = new ISetChangeListener() {
		@Override
		public void handleSetChange(SetChangeEvent event) {
			Set affectedElements = new HashSet();
			affectedElements.addAll(event.diff.getAdditions());
			affectedElements.addAll(event.diff.getRemovals());
			LabelProviderChangedEvent newEvent = new LabelProviderChangedEvent(
					EmployeeLabelWithAnnotation.this,
					affectedElements.toArray());
			fireLabelProviderChanged(newEvent);
		}
	};

	private IObservableSet withAnnotationSet;

	/**
	 * Create a new label provider.
	 * 
	 * @param attributeMap
	 * @param withAnnotationSet
	 */
	public EmployeeLabelWithAnnotation(IObservableMap attributeMap,
			IObservableSet withAnnotationSet) {
		super(attributeMap);
		this.withAnnotationSet = withAnnotationSet;
		this.withAnnotationSet.addSetChangeListener(this.setChangeListener);
	}

	@Override
	public void dispose() {
		this.withAnnotationSet.removeSetChangeListener(this.setChangeListener);
		super.dispose();
	}

	public String getColumnText(Object element, int columnIndex) {
		if (columnIndex == 0 && this.withAnnotationSet.contains(element)) {
			return super.getColumnText(element, columnIndex) + ANNOTATION;
		}
		return super.getColumnText(element, columnIndex);
	}

}