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

import org.eclipse.core.databinding.conversion.Converter;

import com.planimod.core.ProposedTasks.ProposedWarning;

/**
 * Converter to create a string for a collection of warnings.
 * 
 * @author Patrik Dufresne
 * 
 */
public class ProposedWarningsToStringConverter extends Converter {

	private static final Object EMPTY = ""; //$NON-NLS-1$

	private ProposedWarningToStringConverter converter;

	/**
	 * Create a new converter.
	 */
	public ProposedWarningsToStringConverter() {
		super(Collection.class, String.class);
		this.converter = new ProposedWarningToStringConverter();
	}

	/**
	 * This implementation convert the warnings collection into one single
	 * string.
	 */
	@Override
	public Object convert(Object fromObject) {
		if (fromObject instanceof Collection) {
			if (((Collection) fromObject).size() == 0) {
				return EMPTY;
			}
			StringBuilder buf = new StringBuilder();
			for (Object o : (Collection) fromObject) {
				if (o instanceof ProposedWarning) {
					buf.append(this.converter.convert(o).toString());
				}
			}
			return buf.toString();
		}
		return EMPTY;
	}

}
