All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.holonplatform.vaadin.internal.components.builders.AbstractSelectFieldBuilder Maven / Gradle / Ivy

There is a newer version: 5.4.0
Show newest version
/*
 * Copyright 2016-2017 Axioma srl.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.holonplatform.vaadin.internal.components.builders;

import java.util.LinkedList;
import java.util.List;

import com.holonplatform.core.i18n.Localizable;
import com.holonplatform.core.internal.utils.ObjectUtils;
import com.holonplatform.vaadin.components.Field;
import com.holonplatform.vaadin.components.Input;
import com.holonplatform.vaadin.components.ItemSet.ItemCaptionGenerator;
import com.holonplatform.vaadin.components.ItemSet.ItemIconGenerator;
import com.holonplatform.vaadin.components.builders.SelectInputBuilder;
import com.holonplatform.vaadin.data.ItemDataProvider;
import com.holonplatform.vaadin.data.ItemIdentifierProvider;
import com.holonplatform.vaadin.internal.components.AbstractSelectField;
import com.holonplatform.vaadin.internal.data.ItemDataProviderAdapter;
import com.vaadin.data.provider.DataProvider;
import com.vaadin.server.Resource;

/**
 * Base {@link SelectInputBuilder} implementation.
 * 
 * @param  Field type
 * @param  Selection type
 * @param  Selection item type
 * @param  Internal field type
 * @param  Concrete builder type
 * @param  Internal component type
 * 
 * @since 5.0.0
 */
public abstract class AbstractSelectFieldBuilder, S, ITEM, I extends AbstractSelectField, B extends SelectInputBuilder>
		extends AbstractFieldBuilder implements SelectInputBuilder {

	/**
	 * Explicitly added selection items
	 */
	protected final List items = new LinkedList<>();

	/**
	 * Item data provider
	 */
	protected ItemDataProvider itemDataProvider = null;

	/**
	 * Vaadin data provider
	 */
	protected DataProvider dataProvider = null;

	/**
	 * Item identifier
	 */
	protected ItemIdentifierProvider itemIdentifier = null;

	/**
	 * Initial value
	 */
	protected T initialValue;

	/**
	 * Constructor
	 * @param instance Field instance to build
	 */
	public AbstractSelectFieldBuilder(I instance) {
		super(instance);
	}

	/*
	 * (non-Javadoc)
	 * @see com.holonplatform.vaadin.internal.components.builders.AbstractFieldBuilder#withValue(java.lang.Object)
	 */
	@Override
	public B withValue(T value) {
		this.initialValue = value;
		return builder();
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * com.holonplatform.vaadin.components.builders.SelectFieldBuilder#itemCaptionGenerator(com.holonplatform.vaadin.
	 * components.ItemSetComponent.ItemCaptionGenerator)
	 */
	@Override
	public B itemCaptionGenerator(ItemCaptionGenerator itemCaptionGenerator) {
		getInstance().setItemCaptionGenerator(itemCaptionGenerator);
		return builder();
	}

	/*
	 * (non-Javadoc)
	 * @see com.holonplatform.vaadin.components.builders.SelectFieldBuilder#itemIconGenerator(com.holonplatform.vaadin.
	 * components.ItemSetComponent.ItemIconGenerator)
	 */
	@Override
	public B itemIconGenerator(ItemIconGenerator itemIconGenerator) {
		getInstance().setItemIconGenerator(itemIconGenerator);
		return builder();
	}

	/*
	 * (non-Javadoc)
	 * @see com.holonplatform.vaadin.components.builders.SelectFieldBuilder#itemCaption(java.lang.Object,
	 * com.holonplatform.core.i18n.Localizable)
	 */
	@Override
	public B itemCaption(ITEM item, Localizable caption) {
		getInstance().setItemCaption(item, caption);
		return builder();
	}

	/*
	 * (non-Javadoc)
	 * @see com.holonplatform.vaadin.components.builders.SelectFieldBuilder#itemIcon(java.lang.Object,
	 * com.vaadin.server.Resource)
	 */
	@Override
	public B itemIcon(ITEM item, Resource icon) {
		getInstance().setItemIcon(item, icon);
		return builder();
	}

	/*
	 * (non-Javadoc)
	 * @see com.holonplatform.vaadin.components.builders.SelectInputBuilder#items(java.lang.Iterable)
	 */
	@Override
	public B items(Iterable items) {
		this.items.clear();
		if (items != null) {
			items.forEach(i -> this.items.add(i));
		}
		return builder();
	}

	/*
	 * (non-Javadoc)
	 * @see com.holonplatform.vaadin.components.builders.SelectInputBuilder#addItem(java.lang.Object)
	 */
	@Override
	public B addItem(ITEM item) {
		ObjectUtils.argumentNotNull(item, "Item must be not null");
		this.items.add(item);
		return builder();
	}

	/**
	 * Configure items data source
	 * @param instance Building instance
	 */
	protected void configureDataSource(I instance) {
		if (!items.isEmpty()) {
			instance.setItems(items);
		} else if (itemDataProvider != null) {
			instance.setDataProvider(new ItemDataProviderAdapter<>(itemDataProvider, itemIdentifier, null));
		} else if (dataProvider != null) {
			instance.setDataProvider(dataProvider);
		}
	}

	/**
	 * Setup field before actual building
	 * @param instance Building instance
	 */
	protected void preSetup(I instance) {
		if (initialValue != null) {
			instance.setValue(initialValue);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.holonplatform.vaadin.internal.components.builders.AbstractComponentBuilder#build(com.vaadin.ui.
	 * AbstractComponent)
	 */
	@Override
	protected C build(I instance) {
		configureDataSource(instance);
		preSetup(instance);
		return buildSelect(instance);
	}

	/*
	 * (non-Javadoc)
	 * @see com.holonplatform.vaadin.internal.components.builders.AbstractFieldBuilder#buildAsField(com.vaadin.ui.
	 * AbstractField)
	 */
	@Override
	protected Field buildAsField(I instance) {
		configureDataSource(instance);
		preSetup(instance);
		return buildSelectAsField(instance);
	}

	protected abstract C buildSelect(I instance);

	protected abstract Field buildSelectAsField(I instance);

}