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

import org.eclipse.core.databinding.BindingProperties;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.observable.set.IObservableSet;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.property.value.IValueProperty;
import org.eclipse.core.internal.databinding.property.value.SelfValueProperty;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.databinding.viewers.ObservableSetContentProvider;
import org.eclipse.jface.databinding.viewers.ViewersObservables;
import org.eclipse.jface.viewers.ColumnViewerEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
import org.eclipse.jface.viewers.ComboBoxViewerCellEditor;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.widgets.FormText;

import com.patrikdufresne.jface.databinding.ControlStyleSupport;
import com.patrikdufresne.jface.databinding.ControlStyleUpdater;
import com.patrikdufresne.jface.databinding.collections.CollectionProperties;
import com.patrikdufresne.jface.databinding.forms.FormWidgetProperties;
import com.patrikdufresne.jface.databinding.viewers.ColumnSupport;
import com.patrikdufresne.jface.databinding.viewers.PropertyViewerComparator;
import com.patrikdufresne.jface.viewers.ColumnViewerPreferences;
import com.patrikdufresne.managers.databinding.ManagerUpdateValueStrategy;
import com.patrikdufresne.ui.IPageSite;
import com.patrikdufresne.ui.IViewPartPage;
import com.patrikdufresne.util.Localized;
import com.planimod.core.AbstractCalendarEvent;
import com.planimod.core.GeneratePlanifContext;
import com.planimod.core.PlanimodManagers;
import com.planimod.core.Position;
import com.planimod.core.Product;
import com.planimod.core.ProductionEvent;
import com.planimod.core.Section;
import com.planimod.core.Shift;
import com.planimod.core.Task;
import com.planimod.core.Team;
import com.planimod.core.comparators.EmployeeComparators;
import com.planimod.core.comparators.SectionComparators;
import com.planimod.core.comparators.TaskComparators;
import com.planimod.ui.databinding.Converters;
import com.planimod.ui.databinding.DateColorControlStyleSupport;
import com.planimod.ui.databinding.DateFormatRangeValueProperty;
import com.planimod.ui.preference.PlanimodPreferences;
import com.planimod.ui.theme.Resources;
import com.planimod.ui.viewers.EmployeeViewerUtils;
import com.planimod.ui.views.AbstractViewPart;
import com.planimod.ui.views.ChangeDateAction;

/**
 * This windows is used to change the locked status of the planif.
 * 
 * @author Patrik Dufresne
 * 
 */
public class ProposedTaskViewPartPage extends AbstractViewPart implements
		IViewPartPage {

	/**
	 * Dot (.) separator.
	 */
	private static final String DOT = "."; //$NON-NLS-1$

	/**
	 * The part id.
	 */
	public static final String ID = "com.planimod.ui.views.planif.LockedStyleViewPart"; //$NON-NLS-1$

	/**
	 * Key use to store the columne preferences.
	 */
	private static final String PREFERENCE_COLUMNS_CURRENT_TASK = "LockedStyleViewPart.columns.current"; //$NON-NLS-1$
	/**
	 * Key used to store the columns preferences.
	 */
	private static final String PREFERENCE_COLUMNS_OFFER_TASK = "LockedStyleViewPart.columns.offer"; //$NON-NLS-1$

	/**
	 * Key used to store the sash weight.
	 */
	private static final String PREFERENCE_SASH1_WEIGHTS = "LockedStyleViewPart.sash1.weights"; //$NON-NLS-1$

	/**
	 * Default sash weights.
	 */
	private static final int[] PREFERENCE_SASH1_DEFAULT_WEIGHTS = new int[] {
			30, 70 };

	/**
	 * Key used to store the sash weight.
	 */
	private static final String PREFERENCE_SASH2_WEIGHTS = "LockedStyleViewPart.sash2.weights"; //$NON-NLS-1$

	/**
	 * Default sash weights.
	 */
	private static final int[] PREFERENCE_SASH2_DEFAULT_WEIGHTS = new int[] {
			50, 50 };

	/**
	 * Label to display the date range.
	 */
	private Label dateLabel;

	/**
	 * The employee list.
	 */
	private TableViewer employeeViewer;

	/**
	 * Action to generate planif.
	 */
	private GeneratePlanifAction generatePlanifAction;

	/**
	 * Used for localization.
	 */
	private Localized localized = Localized
			.load(ProposedTaskViewPartPage.class);

	/**
	 * Action to change the current week
	 */
	private ChangeDateAction nextAction;

	/**
	 * Action to change the current week
	 */
	private ChangeDateAction previousAction;
	/**
	 * Viewer used to display the current position.fFs
	 */
	private TableViewer taskAssignedViewer;
	/**
	 * The position to be offered.
	 */
	private TableViewer taskProposedViewer;
	/**
	 * Action to change the current week.
	 */
	private ChangeDateAction todayAction;

	/**
	 * Widget to display the warnings.
	 */
	private FormText warningsFormText;

	/**
	 * Create a new view part.
	 */
	public ProposedTaskViewPartPage() {
		super(ID);
		setTitle(this.localized.get("LockedStyleViewPart.title")); //$NON-NLS-1$
	}

	/**
	 * Binds the planner date selection to a shared state.
	 */
	@Override
	protected void bindValues() {

		GeneratePlanifContext context = ((PlanifViewPart) getSite()
				.getParentPart()).getContext();

		IObservableValue week = BeanProperties.value(
				GeneratePlanifContext.class, GeneratePlanifContext.WEEK)
				.observe(context);

		/*
		 * Bind date sate to date label
		 */
		getDbc().bindValue(
				SWTObservables.observeText(this.dateLabel),
				BeanProperties
						.value(GeneratePlanifContext.class,
								GeneratePlanifContext.START)
						.value(BindingProperties.convertedValue(Converters
								.weekTitleConverter())).observe(context));

		/*
		 * Bind the employee viewer to manager model
		 */
		EmployeeInContextComputedSet employees = new EmployeeInContextComputedSet(
				context);
		IObservableMap employeeLabels = BindingProperties.convertedValue(
				EmployeeViewerUtils.fullnameConverter()).observeDetail(
				employees);
		IObservableSet employeeWithTaskProposed = new EmployeesWithTaskProposedComputedSet(
				context);
		this.employeeViewer
				.setContentProvider(new ObservableSetContentProvider());
		this.employeeViewer.setLabelProvider(new EmployeeLabelWithAnnotation(
				employeeLabels, employeeWithTaskProposed));
		this.employeeViewer.setComparator(new PropertyViewerComparator(
				EmployeeComparators.bySeniority()));
		this.employeeViewer.setInput(employees);
		IObservableValue selectedEmployee = ViewersObservables
				.observeSingleSelection(this.employeeViewer);

		DateColorControlStyleSupport.create(this.employeeViewer.getTable(),
				week);

		/*
		 * Bind offered position viewer input with employee selection.
		 */
		TaskProposedByEmployeeComputedSet taskProposed = new TaskProposedByEmployeeComputedSet(
				context, selectedEmployee);
		this.taskProposedViewer
				.setContentProvider(new ObservableSetContentProvider());
		createTaskProposedColumns(this.taskProposedViewer, taskProposed);
		this.taskProposedViewer.setInput(taskProposed);

		// Bind background color
		DateColorControlStyleSupport.create(this.taskProposedViewer.getTable(),
				week);

		/*
		 * Bind warnings
		 */
		ProposedWarningComputedSet proposedWarning = new ProposedWarningComputedSet(
				context, selectedEmployee);
		IObservableValue warningsText = CollectionProperties
				.set()
				.value(BindingProperties
						.convertedValue(new ProposedWarningsToStringConverter()))
				.observe(proposedWarning);
		getDbc().bindValue(
				FormWidgetProperties.text().observe(this.warningsFormText),
				warningsText);

		ControlStyleSupport.create(this.warningsFormText, warningsText,
				new ControlStyleUpdater() {

					/**
					 * This implementation update the background color of the
					 * widget based on the status.
					 */
					@Override
					public void update(Control control, Object status) {
						Color background = Display.getDefault().getSystemColor(
								SWT.COLOR_LIST_BACKGROUND);
						if (status instanceof Date
								&& ((Date) status).compareTo(new Date()) < 0) {
							background = Display.getDefault().getSystemColor(
									SWT.COLOR_INFO_BACKGROUND);
						}
						control.setBackground(background);
					}

				});

		/*
		 * Bind the current position viewer.
		 */
		TaskByEmployeeComputedSet taskAssigned = new TaskByEmployeeComputedSet(
				context, selectedEmployee);
		this.taskAssignedViewer
				.setContentProvider(new ObservableSetContentProvider());
		createTaskAssignedViewerColumns(this.taskAssignedViewer, taskAssigned);
		this.taskAssignedViewer.setInput(taskAssigned);

		DateColorControlStyleSupport.create(this.taskAssignedViewer.getTable(),
				week);

		// Sets the action state
		this.previousAction.setDate(week);
		this.todayAction.setDate(week);
		this.nextAction.setDate(week);

	}

	/**
	 * Create the controls to display the current assignment for the selected
	 * employees.
	 * 
	 * @param parent
	 * @return
	 */
	protected void createTaskAssignedViewer(Composite parent) {

		GridLayout layout = new GridLayout(1, false);
		layout.marginWidth = 0;
		Composite comp = new Composite(parent, SWT.NONE);
		comp.setLayout(layout);

		/*
		 * Create a widget for the warnings
		 */
		this.warningsFormText = new FormText(comp, SWT.NONE);
		this.warningsFormText.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
				true, false));

		/*
		 * Create label for the list
		 */
		Label label = new Label(comp, SWT.NONE);
		label.setText(this.localized
				.get("LockedStyleViewPart.label.currentPositions")); //$NON-NLS-1$
		label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		this.taskAssignedViewer = new TableViewer(comp);
		this.taskAssignedViewer.getTable().setHeaderVisible(true);
		this.taskAssignedViewer.getControl().setLayoutData(
				new GridData(SWT.FILL, SWT.FILL, true, true));
		ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(
				this.taskAssignedViewer) {
			@Override
			protected boolean isEditorActivationEvent(
					ColumnViewerEditorActivationEvent event) {
				return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
						|| event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
						|| (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR)
						|| event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
			}
		};
		TableViewerEditor.create(this.taskAssignedViewer, actSupport,
				ColumnViewerEditor.TABBING_HORIZONTAL
						| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
						| ColumnViewerEditor.TABBING_VERTICAL
						| ColumnViewerEditor.KEYBOARD_ACTIVATION);
	}

	/**
	 * This implementation creates all the control required to change the locked
	 * status of any planif event.
	 */
	@Override
	public void activate(Composite parent) {

		// Create a label to display the date
		this.dateLabel = new Label(parent, SWT.CENTER);
		this.dateLabel.setFont(Resources.getWeekTitleFont());
		this.dateLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				false));

		SashForm sash1 = createSashForm(parent, SWT.HORIZONTAL,
				PlanimodPreferences.getPreferenceStore(),
				PREFERENCE_SASH1_WEIGHTS, PREFERENCE_SASH1_DEFAULT_WEIGHTS);
		sash1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		/*
		 * Create the left composite
		 */
		createEmployeeViewer(sash1);

		/*
		 * Create a second sash form
		 */
		SashForm sash2 = createSashForm(sash1, SWT.VERTICAL,
				PlanimodPreferences.getPreferenceStore(),
				PREFERENCE_SASH2_WEIGHTS, PREFERENCE_SASH2_DEFAULT_WEIGHTS);

		/*
		 * Create the right composite
		 */
		createTaskProposedViewer(sash2);

		createTaskAssignedViewer(sash2);

		// Populate tool bar.
		fillToolBar();

		// Create binding
		bind();

	}

	/**
	 * Populate tool bar.
	 */
	void fillToolBar() {
		/*
		 * Date navigation
		 */
		this.previousAction = ChangeDateAction.previousAction();
		this.todayAction = ChangeDateAction.todayAction();
		this.nextAction = ChangeDateAction.nextAction();
		getSite().getToolBarManager().add(this.previousAction);
		getSite().getToolBarManager().add(this.todayAction);
		getSite().getToolBarManager().add(this.nextAction);

		/*
		 * Separator
		 */
		getSite().getToolBarManager().add(new Separator());

		/*
		 * Generate planif action
		 */
		this.generatePlanifAction = new GeneratePlanifAction(
				((PlanifViewPart) getSite().getParentPart()).getContext(),
				getSite(), getSite().getMainWindow());
		getSite().getToolBarManager().add(this.generatePlanifAction);
	}

	/**
	 * Create all the control to be displayed in the left side of the view.
	 * Mostly it's create the employee list.
	 * 
	 * @param parent
	 * @return
	 */
	protected Control createEmployeeViewer(Composite parent) {
		GridLayout layout = new GridLayout(1, false);
		layout.marginWidth = 0;
		layout.marginLeft = 5;
		Composite comp = new Composite(parent, SWT.NONE);
		comp.setLayout(layout);

		// Create a label for the list
		Label label = new Label(comp, SWT.NONE);
		label.setText(this.localized.get("LockedStyleViewPart.label.employees")); //$NON-NLS-1$
		label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		// Create the list
		this.employeeViewer = new TableViewer(comp);
		this.employeeViewer.getControl().setLayoutData(
				new GridData(SWT.FILL, SWT.FILL, true, true));

		return comp;
	}

	/**
	 * Create all the control to be displayed in the right side of the view.
	 * 
	 * @param parent
	 * @return
	 */
	protected void createTaskProposedViewer(Composite parent) {
		GridLayout layout = new GridLayout(1, false);
		layout.marginWidth = 0;
		layout.marginRight = 5;
		Composite comp = new Composite(parent, SWT.NONE);
		comp.setLayout(layout);

		/*
		 * Create label for the list
		 */
		Label label = new Label(comp, SWT.NONE);
		label.setText(this.localized
				.get("LockedStyleViewPart.label.offeredPositions")); //$NON-NLS-1$
		label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		this.taskProposedViewer = new TableViewer(comp);
		this.taskProposedViewer.getTable().setHeaderVisible(true);
		this.taskProposedViewer.getControl().setLayoutData(
				new GridData(SWT.FILL, SWT.FILL, true, true));
		ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(
				this.taskProposedViewer) {
			@Override
			protected boolean isEditorActivationEvent(
					ColumnViewerEditorActivationEvent event) {
				return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
						|| event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
						|| (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR)
						|| event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
			}
		};
		TableViewerEditor.create(this.taskProposedViewer, actSupport,
				ColumnViewerEditor.TABBING_HORIZONTAL
						| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
						| ColumnViewerEditor.TABBING_VERTICAL
						| ColumnViewerEditor.KEYBOARD_ACTIVATION);
	}

	protected void createTaskAssignedViewerColumns(TableViewer viewer,
			IObservableSet knownElements) {
		// Product name
		IValueProperty productLabel = BeanProperties.value(Task.class,
				Task.PRODUCTION_EVENT + DOT + ProductionEvent.PRODUCT + DOT
						+ Product.NAME);
		ColumnSupport
				.create(viewer,
						this.localized.get("ListStyleViewPart.column.event"), //$NON-NLS-1$
						knownElements, productLabel).setWidth(200)
				.addPropertySorting();

		// Section name
		IValueProperty sectionLabel = BeanProperties.value(Task.class,
				Task.POSITION + DOT + Position.SECTION + DOT + Section.NAME)
				.value(BindingProperties.convertedValue(Converters
						.removeFrontNumber()));
		ColumnSupport
				.create(viewer,
						this.localized.get("ListStyleViewPart.column.section"), //$NON-NLS-1$
						knownElements, sectionLabel).setWidth(100)
				.addPropertySorting();

		// Position name
		IValueProperty positionLabel = BeanProperties.value(Task.class,
				Task.POSITION + DOT + Position.NAME);
		ColumnSupport
				.create(viewer,
						this.localized.get("ListStyleViewPart.column.position"), //$NON-NLS-1$
						knownElements, positionLabel).setWidth(250)
				.addPropertySorting();

		// Employee
		IValueProperty employeeLabel = BeanProperties.value(Task.class,
				Task.EMPLOYEE).value(
				BindingProperties.convertedValue(EmployeeViewerUtils
						.fullnameConverter()));
		ColumnSupport.create(viewer,
				this.localized.get("ListStyleViewPart.column.employee"), //$NON-NLS-1$
				knownElements, employeeLabel).setWidth(150);

		// Team
		IValueProperty teamLabel = BeanProperties.value(
				Task.class,
				Task.PRODUCTION_EVENT + DOT + ProductionEvent.SHIFT + DOT
						+ Shift.TEAM + DOT + Team.NAME)
				.value(BindingProperties.convertedValue(Converters
						.removeFrontNumber()));
		ColumnSupport
				.create(viewer,
						this.localized.get("ListStyleViewPart.column.team"), //$NON-NLS-1$
						knownElements, teamLabel)
				.setWidth(150)
				.addPropertySorting(new SelfValueProperty(Task.class),
						TaskComparators.byTeam()).activateSorting();

		// Time Range
		IValueProperty timeRangeProperty = DateFormatRangeValueProperty
				.createTimeMedium();
		IValueProperty startDateProperty = BeanProperties.value(Task.class,
				AbstractCalendarEvent.START_DATE);
		ColumnSupport
				.create(viewer,
						this.localized
								.get("ListStyleViewPart.column.timeRange"), //$NON-NLS-1$
						knownElements, timeRangeProperty).setWidth(90)
				.setWidth(150).addPropertySorting(startDateProperty);

		// Locked
		IValueProperty lockedProperty = BeanProperties.value(Task.class,
				Task.LOCKED, Boolean.TYPE);
		IValueProperty lockedLabel = lockedProperty.value(BindingProperties
				.convertedValue(Converters.booleanConverter()));
		ColumnSupport
				.create(viewer,
						this.localized.get("ListStyleViewPart.column.locked"), //$NON-NLS-1$
						knownElements, lockedLabel)
				.addPropertySorting()
				.addCheckboxEditingSupport(
						getDbc(),
						lockedProperty,
						new ManagerUpdateValueStrategy(getSite().getService(
								PlanimodManagers.class),
								UpdateValueStrategy.POLICY_CONVERT), null);

		// Attach a column preference
		ColumnViewerPreferences.create(viewer,
				PlanimodPreferences.getPreferenceStore(),
				PREFERENCE_COLUMNS_CURRENT_TASK);
	}

	/**
	 * Creates the columns to display the proposed position.
	 * 
	 * @param viewer
	 * @param knownElements
	 * @param selection
	 */
	protected void createTaskProposedColumns(TableViewer viewer,
			IObservableSet knownElements) {

		// Create an observable for the current selection.
		final IObservableValue taskSelection = ViewersObservables
				.observeSingleSelection(this.taskProposedViewer);

		// Product name
		IValueProperty productLabel = BeanProperties.value(Task.class,
				Task.PRODUCTION_EVENT + DOT + ProductionEvent.PRODUCT + DOT
						+ Product.NAME);
		ColumnSupport
				.create(viewer,
						this.localized.get("ListStyleViewPart.column.event"), //$NON-NLS-1$
						knownElements, productLabel).setWidth(200)
				.addPropertySorting();

		// Section name
		IValueProperty sectionProperty = BeanProperties.value(Task.class,
				Task.POSITION + DOT + Position.SECTION);
		IValueProperty sectionLabel = BeanProperties.value(Task.class,
				Task.POSITION + DOT + Position.SECTION + DOT + Section.NAME)
				.value(BindingProperties.convertedValue(Converters
						.removeFrontNumber()));
		ColumnSupport
				.create(viewer,
						this.localized.get("ListStyleViewPart.column.section"), //$NON-NLS-1$
						knownElements, sectionLabel)
				.setWidth(100)
				.addPropertySorting(sectionProperty,
						SectionComparators.byName());

		// Position name
		IValueProperty positionLabel = BeanProperties.value(Task.class,
				Task.POSITION + DOT + Position.NAME);
		ColumnSupport
				.create(viewer,
						this.localized.get("ListStyleViewPart.column.position"), //$NON-NLS-1$
						knownElements, positionLabel).setWidth(250)
				.addPropertySorting();

		// Employee -- with list of proposed employee, but not editing.
		IObservableSet proposedEmployees = new EmployeeByTaskProposedComputedSet(
				((PlanifViewPart) getSite().getParentPart()).getContext(),
				taskSelection);
		IObservableMap proposedEmployeeLabels = BindingProperties
				.convertedValue(EmployeeViewerUtils.fullnameConverter())
				.observeDetail(proposedEmployees);
		ComboBoxViewerCellEditor employeeCellEditor = new ComboBoxViewerCellEditor(
				viewer.getTable());
		ComboViewer comboViewer = employeeCellEditor.getViewer();
		comboViewer.setContentProvider(new ObservableSetContentProvider());
		comboViewer.setLabelProvider(new ObservableMapLabelProvider(
				proposedEmployeeLabels));
		comboViewer.setComparator(new PropertyViewerComparator(
				EmployeeComparators.bySeniority()));
		comboViewer.setInput(proposedEmployees);

		IValueProperty employeeProperty = BeanProperties.value(Task.class,
				Task.EMPLOYEE);
		IValueProperty employeeLabel = employeeProperty.value(BindingProperties
				.convertedValue(EmployeeViewerUtils.fullnameConverter()));
		ColumnSupport
				.create(viewer,
						this.localized.get("ListStyleViewPart.column.employee"), //$NON-NLS-1$
						knownElements, employeeLabel)
				.setWidth(150)
				.addPropertySorting()
				.addViewerEditingSupport(
						getDbc(),
						employeeProperty,
						employeeCellEditor,
						new UpdateValueStrategy(
								UpdateValueStrategy.POLICY_NEVER), null);

		// Team
		IValueProperty teamLabel = BeanProperties.value(
				Task.class,
				Task.PRODUCTION_EVENT + DOT + ProductionEvent.SHIFT + DOT
						+ Shift.TEAM + DOT + Team.NAME)
				.value(BindingProperties.convertedValue(Converters
						.removeFrontNumber()));
		ColumnSupport
				.create(viewer,
						this.localized.get("ListStyleViewPart.column.team"), //$NON-NLS-1$
						knownElements, teamLabel)
				.setWidth(150)
				.addPropertySorting(new SelfValueProperty(Task.class),
						TaskComparators.byTeam()).activateSorting();

		// Time Range
		IValueProperty timeRangeProperty = DateFormatRangeValueProperty
				.createTimeMedium();
		IValueProperty startDateProperty = BeanProperties.value(Task.class,
				AbstractCalendarEvent.START_DATE);
		ColumnSupport
				.create(viewer,
						this.localized
								.get("ListStyleViewPart.column.timeRange"), //$NON-NLS-1$
						knownElements, timeRangeProperty).setWidth(90)
				.setWidth(150).addPropertySorting(startDateProperty);

		// Locked
		final IObservableValue selectedEmployee = ViewersObservables
				.observeSingleSelection(this.employeeViewer);

		IValueProperty lockedProperty = new TaskLockedValueProperty(
				selectedEmployee);
		IValueProperty lockedLabel = BeanProperties.value(Task.class,
				Task.LOCKED)
				.value(BindingProperties.convertedValue(Converters
						.booleanConverter()));
		ColumnSupport
				.create(viewer,
						this.localized.get("ListStyleViewPart.column.locked"), //$NON-NLS-1$
						knownElements, lockedLabel)
				.addPropertySorting()
				.addCheckboxEditingSupport(
						getDbc(),
						lockedProperty,
						new ManagerUpdateValueStrategy(getSite().getService(
								PlanimodManagers.class),
								UpdateValueStrategy.POLICY_CONVERT), null);

		// Attach a column preference
		ColumnViewerPreferences.create(viewer,
				PlanimodPreferences.getPreferenceStore(),
				PREFERENCE_COLUMNS_OFFER_TASK);

	}

	/**
	 * This implementation free allocated resources.
	 */
	@Override
	public void deactivate() {
		try {
			if (this.generatePlanifAction != null) {
				this.generatePlanifAction.dispose();
				this.generatePlanifAction = null;
			}
			this.dateLabel = null;
			this.employeeViewer = null;
			this.nextAction = null;
			this.taskProposedViewer = null;
			this.previousAction = null;
			this.todayAction = null;
		} finally {
			super.deactivate();
		}
	}

	@Override
	public IPageSite getSite() {
		return (IPageSite) super.getSite();
	}

}
