package ru.rsdn.patterns.mvc.samples;

import java.util.Collection;
import java.util.Collections;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;

import ru.rsdn.patterns.mvc.model.ListModel;
import ru.rsdn.patterns.mvc.model.Model;

/**
 * Поставщик содержимого модели списка
 * @param
 * <P>
 * свойство модели
 */
public class ListContentProvider<P> implements IStructuredContentProvider {
	protected Model<P>[] elements;

	public Object[] getElements(Object inputElement) {
		return elements;
	}

	@SuppressWarnings("unchecked")
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		if (newInput != null) {
			final ListModel<P> listModel = (ListModel<P>) newInput;
			final Collection<Model<P>> models = listModel.getProperty();
			elements = models.toArray(new Model[models.size()]);
		} else
			elements = Collections.emptySet().toArray(new Model[0]);
	}

	public void dispose() {
	}
}
