com.holonplatform.vaadin7.internal.components.SingleSelectField Maven / Gradle / Ivy
/*
* 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.vaadin7.internal.components;
import java.util.Optional;
import com.holonplatform.core.internal.utils.ObjectUtils;
import com.holonplatform.core.property.Property;
import com.holonplatform.core.property.PropertyBox;
import com.holonplatform.vaadin7.components.SingleSelect;
import com.holonplatform.vaadin7.components.builders.SelectInputBuilder;
import com.holonplatform.vaadin7.components.builders.SinglePropertySelectInputBuilder;
import com.holonplatform.vaadin7.components.builders.SingleSelectInputBuilder;
import com.holonplatform.vaadin7.components.builders.BaseSelectInputBuilder.RenderingMode;
import com.holonplatform.vaadin7.data.ItemDataProvider;
import com.holonplatform.vaadin7.data.ItemIdentifierProvider;
import com.holonplatform.vaadin7.data.container.ItemAdapter;
import com.holonplatform.vaadin7.data.container.PropertyBoxItem;
import com.holonplatform.vaadin7.internal.components.builders.AbstractSelectFieldBuilder;
import com.holonplatform.vaadin7.internal.data.PropertyItemIdentifier;
import com.holonplatform.vaadin7.internal.data.container.PropertyBoxItemAdapter;
import com.vaadin.shared.ui.combobox.FilteringMode;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Field;
/**
* Default single select {@link Field} implementation.
*
* @param Field type
*
* @since 5.0.0
*/
public class SingleSelectField extends AbstractSelectField implements SingleSelect {
private static final long serialVersionUID = -52116276228997170L;
/**
* Constructor
* @param type Selection value type
* @param renderingMode Rendering mode
*/
public SingleSelectField(Class extends T> type, RenderingMode renderingMode) {
super(type, renderingMode);
}
/*
* (non-Javadoc)
* @see com.holonplatform.vaadin.components.Selectable#select(java.lang.Object)
*/
@Override
public void select(T item) {
setValue(item);
}
/*
* (non-Javadoc)
* @see com.holonplatform.vaadin.components.Selectable#deselect(java.lang.Object)
*/
@Override
public void deselect(T item) {
clear();
}
/*
* (non-Javadoc)
* @see com.holonplatform.vaadin.components.SingleSelect#getSelectedItem()
*/
@Override
public Optional getSelectedItem() {
return Optional.of(getValue());
}
/**
* Set whether is possible to input text into the field. If the concrete select component does not support user
* input, this method has no effect.
* @param textInputAllowed true to allow entering text, false to just show the current selection
*/
public void setTextInputAllowed(boolean textInputAllowed) {
if (getInternalField() instanceof ComboBox) {
((ComboBox) getInternalField()).setTextInputAllowed(textInputAllowed);
}
}
/**
* Sets whether to scroll the selected item visible (directly open the page on which it is) when opening the
* suggestions popup or not. This requires finding the index of the item, which can be expensive in many large lazy
* loading containers.
*
* Only applies to select field with backing components supporting a suggestion popup.
*
* @param scrollToSelectedItem true to find the page with the selected item when opening the selection popup
*/
public void setScrollToSelectedItem(boolean scrollToSelectedItem) {
if (getInternalField() instanceof ComboBox) {
((ComboBox) getInternalField()).setScrollToSelectedItem(scrollToSelectedItem);
}
}
/**
* Sets the suggestion pop-up's width as a CSS string. By using relative units (e.g. "50%") it's possible to set the
* popup's width relative to the selection component itself.
*
* Only applies to select field with backing components supporting a suggestion popup.
*
* @param width the suggestion pop-up width
*/
public void setSuggestionPopupWidth(String width) {
if (getInternalField() instanceof ComboBox) {
((ComboBox) getInternalField()).setPopupWidth(width);
}
}
/**
* Sets the option filtering mode.
*
* Only applies to select field with backing components supporting a suggestion popup.
*
* @param filteringMode the filtering mode to use
*/
public void setFilteringMode(FilteringMode filteringMode) {
if (getInternalField() instanceof ComboBox) {
((ComboBox) getInternalField()).setFilteringMode(filteringMode);
}
}
/**
* Gets the current filtering mode.
*
* Only applies to select field with backing components supporting a suggestion popup.
*
* @return the filtering mode in use, or null
if the backing component does not supports a suggestion
* popup
*/
public FilteringMode getFilteringMode() {
if (getInternalField() instanceof ComboBox) {
return ((ComboBox) getInternalField()).getFilteringMode();
}
return null;
}
// Builders
/**
* Base {@link SingleSelect} builder.
* @param Selection type
*/
static abstract class AbstractSingleSelectFieldBuilder>
extends AbstractSelectFieldBuilder, T, ITEM, SingleSelectField, B>
implements SelectInputBuilder.SingleSelectConfigurator {
/**
* Constructor
* @param type Selection value type
* @param renderingMode Rendering mode
*/
public AbstractSingleSelectFieldBuilder(Class extends T> type, RenderingMode renderingMode) {
super(new SingleSelectField<>(type, renderingMode));
}
/*
* (non-Javadoc)
* @see com.holonplatform.vaadin.components.builders.SingleSelectFieldBuilder#disableTextInput()
*/
@Override
public B disableTextInput() {
getInstance().setTextInputAllowed(false);
return builder();
}
/*
* (non-Javadoc)
* @see com.holonplatform.vaadin.components.builders.SingleSelectFieldBuilder#scrollToSelectedItem(boolean)
*/
@Override
public B scrollToSelectedItem(boolean scrollToSelectedItem) {
getInstance().setScrollToSelectedItem(scrollToSelectedItem);
return builder();
}
/*
* (non-Javadoc)
* @see
* com.holonplatform.vaadin.components.builders.SingleSelectFieldBuilder#suggestionPopupWidth(java.lang.String)
*/
@Override
public B suggestionPopupWidth(String width) {
getInstance().setSuggestionPopupWidth(width);
return builder();
}
/*
* (non-Javadoc)
* @see
* com.holonplatform.vaadin.components.builders.SingleSelectFieldBuilder#filteringMode(com.vaadin.shared.ui.
* combobox.FilteringMode)
*/
@Override
public B filteringMode(FilteringMode filteringMode) {
getInstance().setFilteringMode(filteringMode);
return builder();
}
}
/**
* Default {@link SingleSelectInputBuilder} implementation.
* @param Selection type
*/
public static class Builder extends AbstractSingleSelectFieldBuilder>
implements SingleSelectInputBuilder {
/**
* Constructor
* @param type Selection value type
* @param renderingMode Rendering mode
*/
public Builder(Class extends T> type, RenderingMode renderingMode) {
super(type, renderingMode);
}
/*
* (non-Javadoc)
* @see com.holonplatform.vaadin.internal.components.builders.AbstractComponentBuilder#builder()
*/
@Override
protected Builder builder() {
return this;
}
/*
* (non-Javadoc)
* @see com.holonplatform.vaadin.components.builders.SelectFieldBuilder#items(java.lang.Iterable)
*/
@Override
public SingleSelectInputBuilder items(Iterable items) {
ObjectUtils.argumentNotNull(items, "Items must be not null");
this.items.clear();
items.forEach(i -> this.items.add(i));
return builder();
}
/*
* (non-Javadoc)
* @see com.holonplatform.vaadin.components.builders.SelectFieldBuilder#addItem(java.lang.Object)
*/
@Override
public SingleSelectInputBuilder addItem(T item) {
ObjectUtils.argumentNotNull(item, "Item must be not null");
this.items.add(item);
return builder();
}
/*
* (non-Javadoc)
* @see
* com.holonplatform.vaadin.components.builders.SelectItemDataSourceBuilder#dataSource(com.holonplatform.vaadin.
* data.ItemDataProvider)
*/
@SuppressWarnings("unchecked")
@Override
public SingleSelectInputBuilder dataSource(ItemDataProvider dataProvider) {
ObjectUtils.argumentNotNull(dataProvider, "Item data provider must be not null");
dataSourceBuilder.dataSource(dataProvider);
dataSourceBuilder.itemIdentifier(ItemIdentifierProvider.identity());
dataProviderConfigured = true;
return builder();
}
/*
* (non-Javadoc)
* @see
* com.holonplatform.vaadin.components.builders.SelectItemDataSourceBuilder#itemAdapter(com.holonplatform.vaadin
* .data.container.ItemAdapter)
*/
@SuppressWarnings("unchecked")
@Override
public SingleSelectInputBuilder itemAdapter(ItemAdapter itemAdapter) {
dataSourceBuilder.itemAdapter(itemAdapter);
return builder();
}
/*
* (non-Javadoc)
* @see
* com.holonplatform.vaadin.internal.components.builders.AbstractSelectFieldBuilder#configureDataSource(com.
* holonplatform.vaadin.internal.components.AbstractSelectField)
*/
@SuppressWarnings("unchecked")
@Override
protected void configureDataSource(SingleSelectField instance) {
super.configureDataSource(instance);
if (!instance.getItemDataExtractor().isPresent()) {
// default extractor
instance.setItemDataExtractor((c, id, i) -> (T) id);
}
}
/*
* (non-Javadoc)
* @see com.holonplatform.vaadin.internal.components.builders.AbstractSelectFieldBuilder#buildSelect(com.
* holonplatform.vaadin.internal.components.AbstractSelectField)
*/
@Override
protected SingleSelect buildSelect(SingleSelectField instance) {
return instance;
}
/*
* (non-Javadoc)
* @see com.holonplatform.vaadin.internal.components.builders.AbstractSelectFieldBuilder#buildSelectAsField(com.
* holonplatform.vaadin.internal.components.AbstractSelectField)
*/
@Override
protected Field buildSelectAsField(SingleSelectField instance) {
return instance;
}
}
/**
* Default {@link SinglePropertySelectInputBuilder} implementation.
* @param Selection type
*/
public static class PropertyBuilder
extends AbstractSingleSelectFieldBuilder>
implements SinglePropertySelectInputBuilder {
/**
* Constructor
* @param selectProperty Selection (and identifier) property
* @param renderingMode Rendering mode
*/
@SuppressWarnings("unchecked")
public PropertyBuilder(Property selectProperty, RenderingMode renderingMode) {
super(selectProperty.getType(), renderingMode);
dataSourceBuilder.withProperty(selectProperty, selectProperty.getType());
dataSourceBuilder.itemIdentifier(new PropertyItemIdentifier<>(selectProperty));
itemAdapter(new PropertyBoxItemAdapter());
}
/*
* (non-Javadoc)
* @see
* com.holonplatform.vaadin.components.builders.PropertySelectInputBuilder#dataSource(com.holonplatform.vaadin.
* data.ItemDataProvider, java.lang.Iterable)
*/
@SuppressWarnings("unchecked")
@Override
public > SinglePropertySelectInputBuilder dataSource(
ItemDataProvider dataProvider, Iterable properties) {
ObjectUtils.argumentNotNull(dataProvider, "ItemDataProvider must be not null");
ObjectUtils.argumentNotNull(properties, "Item property set must be not null");
properties.forEach(p -> dataSourceBuilder.withProperty(p, p.getType()));
dataSourceBuilder.dataSource(dataProvider);
dataProviderConfigured = true;
return builder();
}
/*
* (non-Javadoc)
* @see com.holonplatform.vaadin.components.builders.PropertySelectFieldBuilder#itemAdapter(com.holonplatform.
* vaadin.data.container.ItemAdapter)
*/
@SuppressWarnings("unchecked")
@Override
public SinglePropertySelectInputBuilder itemAdapter(final ItemAdapter itemAdapter) {
ObjectUtils.argumentNotNull(itemAdapter, "ItemAdapter must be not null");
dataSourceBuilder.itemAdapter(itemAdapter);
// set extractor
getInstance().setItemDataExtractor((c, id, i) -> itemAdapter.restore(c, i));
return builder();
}
/*
* (non-Javadoc)
* @see
* com.holonplatform.vaadin.internal.components.builders.AbstractSelectFieldBuilder#configureDataSource(com.
* holonplatform.vaadin.internal.components.AbstractSelectField)
*/
@Override
protected void configureDataSource(SingleSelectField instance) {
super.configureDataSource(instance);
if (!instance.getItemDataExtractor().isPresent()) {
// default extractor
instance.setItemDataExtractor((c, id, i) -> ((PropertyBoxItem) i).getPropertyBox());
}
}
/*
* (non-Javadoc)
* @see com.holonplatform.vaadin.internal.components.builders.AbstractSelectFieldBuilder#buildSelect(com.
* holonframework.vaadin.internal.components.AbstractSelectField)
*/
@Override
protected SingleSelect buildSelect(SingleSelectField instance) {
return instance;
}
/*
* (non-Javadoc)
* @see com.holonplatform.vaadin.internal.components.builders.AbstractSelectFieldBuilder#buildSelectAsField(com.
* holonplatform.vaadin.internal.components.AbstractSelectField)
*/
@Override
protected Field buildSelectAsField(SingleSelectField instance) {
return instance;
}
/*
* (non-Javadoc)
* @see com.holonplatform.vaadin.internal.components.builders.AbstractComponentConfigurator#builder()
*/
@Override
protected SinglePropertySelectInputBuilder builder() {
return this;
}
}
}