Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.holonplatform.vaadin.internal.components.MultiSelectField Maven / Gradle / Ivy
package com.holonplatform.vaadin.internal.components;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import com.holonplatform.core.internal.utils.ObjectUtils;
import com.holonplatform.core.property.Property;
import com.holonplatform.core.property.PropertyBox;
import com.holonplatform.vaadin.components.Field;
import com.holonplatform.vaadin.components.MultiSelect;
import com.holonplatform.vaadin.components.builders.BaseSelectInputBuilder.RenderingMode;
import com.holonplatform.vaadin.components.builders.BaseSelectModeMultiPropertySelectInputBuilder.OptionsModeMultiPropertySelectInputBuilder;
import com.holonplatform.vaadin.components.builders.BaseSelectModeMultiPropertySelectInputBuilder.SelectModeMultiPropertySelectInputBuilder;
import com.holonplatform.vaadin.components.builders.BaseSelectModeMultiSelectInputBuilder.OptionsModeMultiSelectInputBuilder;
import com.holonplatform.vaadin.components.builders.BaseSelectModeMultiSelectInputBuilder.SelectModeMultiSelectInputBuilder;
import com.holonplatform.vaadin.components.builders.MultiPropertySelectInputBuilder;
import com.holonplatform.vaadin.components.builders.MultiPropertySelectInputBuilder.GenericMultiPropertySelectInputBuilder;
import com.holonplatform.vaadin.components.builders.MultiSelectInputBuilder;
import com.holonplatform.vaadin.components.builders.MultiSelectInputBuilder.GenericMultiSelectInputBuilder;
import com.holonplatform.vaadin.data.ItemDataProvider;
import com.holonplatform.vaadin.internal.components.builders.AbstractSelectFieldBuilder;
import com.holonplatform.vaadin.internal.data.PropertyItemIdentifier;
import com.vaadin.data.Converter;
import com.vaadin.data.HasDataProvider;
import com.vaadin.data.provider.DataProvider;
import com.vaadin.data.provider.Query;
import com.vaadin.event.selection.MultiSelectionEvent;
import com.vaadin.server.SerializableFunction;
import com.vaadin.server.SerializablePredicate;
import com.vaadin.ui.AbstractMultiSelect;
import com.vaadin.ui.CheckBoxGroup;
import com.vaadin.ui.ComboBox.CaptionFilter;
import com.vaadin.ui.ListSelect;
public class MultiSelectField
extends AbstractSelectField , T , ITEM , Set , AbstractMultiSelect > implements MultiSelect {
private static final long serialVersionUID = -7662977233168084151L ;
@SuppressWarnings ("unchecked" )
public MultiSelectField (Class type, RenderingMode renderingMode) {
super ((Class>) (Class) Set.class, renderingMode);
}
@Override
protected AbstractMultiSelect buildInternalField (Class> type) {
RenderingMode mode = getRenderingMode();
if (mode == null ) {
mode = RenderingMode.OPTIONS;
}
if (mode == RenderingMode.OPTIONS) {
final CheckBoxGroup- field =
new CheckBoxGroup<>();
field.setItemCaptionGenerator(i -> generateItemCaption(i));
field.setItemIconGenerator(i -> generateItemIcon(i));
field.addSelectionListener(e -> fireSelectionListeners(buildSelectionEvent(e)));
return field;
}
final ListSelect- field =
new ListSelect<>();
field.setItemCaptionGenerator(i -> generateItemCaption(i));
field.addSelectionListener(e -> fireSelectionListeners(buildSelectionEvent(e)));
return field;
}
protected SelectionEvent buildSelectionEvent (MultiSelectionEvent event) {
return new DefaultSelectionEvent<>(fromInternalValue(event.getAllSelectedItems()), event.isUserOriginated());
}
@Override
protected Set fromInternalValue (Set value) {
if (value != null ) {
final Set set = new HashSet<>(value.size());
value.forEach(v -> set.add(toSelection(v)));
return set;
}
return null ;
}
@Override
protected Set toInternalValue (Set value) {
if (value != null ) {
final Set- set =
new HashSet<>(value.size());
value.forEach(v -> set.add(toItem(v)));
return set;
}
return null ;
}
@Override
public void setItems (Collection items) {
getInternalField().setItems(items);
}
@SuppressWarnings ({ "unchecked" , "rawtypes" })
@Override
public void setDataProvider (DataProvider dataProvider) {
if (getInternalField() instanceof HasDataProvider) {
((HasDataProvider) getInternalField()).setDataProvider(dataProvider);
}
}
@Override
public void setDataProvider (DataProvider dataProvider, SerializableFunction filterConverter) {
setDataProvider(dataProvider);
}
@Override
public void setItems (Collection items, CaptionFilter filter) {
setItems(items);
}
@Override
public Optional> getDataProvider() {
return Optional.ofNullable(getInternalField().getDataProvider());
}
@Override
public Set getSelectedItems () {
final Set value = getValue();
return (value != null ) ? Collections.unmodifiableSet(value) : Collections.emptySet();
}
@Override
public void deselectAll () {
clear();
}
@SuppressWarnings ("unchecked" )
@Override
public void select (Iterable items) {
ObjectUtils.argumentNotNull(items, "Items to select must be not null" );
final Set set = new HashSet<>();
items.forEach(i -> {
ObjectUtils.argumentNotNull(i, "Items to select must be not null" );
set.add(i);
});
Set- toSelect = toInternalValue(set);
getInternalField().select((ITEM[]) toSelect.toArray());
}
@SuppressWarnings ("unchecked" )
@Override
public void deselect (Iterable items) {
ObjectUtils.argumentNotNull(items, "Items to deselect must be not null" );
final Set set = new HashSet<>();
items.forEach(i -> {
if (i != null ) {
set.add(i);
}
});
Set- toDeselect = toInternalValue(set);
getInternalField().deselect((ITEM[]) toDeselect.toArray());
}
@Override
public void selectAll () {
getDataProvider().ifPresent(dp -> {
Stream- allItemsStream = dp.fetch(
new Query<>());
LinkedHashSet- allItems =
new LinkedHashSet<>();
allItemsStream.forEach(allItems::add);
getInternalField().setValue(allItems);
});
}
public void setRows (int rows) {
if (getInternalField() instanceof ListSelect) {
((ListSelect) getInternalField()).setRows(rows);
}
}
public void setHtmlContentAllowed (boolean htmlContentAllowed) {
if (getInternalField() instanceof CheckBoxGroup) {
((CheckBoxGroup) getInternalField()).setHtmlContentAllowed(htmlContentAllowed);
}
}
@SuppressWarnings ("unchecked" )
public void setItemEnabledProvider (SerializablePredicate itemEnabledProvider) {
if (getInternalField() instanceof CheckBoxGroup) {
((CheckBoxGroup) getInternalField()).setItemEnabledProvider(itemEnabledProvider);
}
}
public static abstract class Builder >
extends AbstractSelectFieldBuilder , MultiSelect , T , T , MultiSelectField , B >
implements MultiSelectInputBuilder {
public Builder (Class type, RenderingMode renderingMode) {
super (new MultiSelectField<>(type, renderingMode));
getInstance().setItemConverter(Converter.identity());
}
@Override
public B dataSource (ItemDataProvider dataProvider) {
ObjectUtils.argumentNotNull(dataProvider, "Item data provider must be not null" );
this .itemDataProvider = dataProvider;
return builder();
}
@Override
public B dataSource (DataProvider dataProvider) {
this .dataProvider = dataProvider;
return builder();
}
@Override
public B items (Iterable items) {
ObjectUtils.argumentNotNull(items, "Items must be not null" );
this .items.clear();
items.forEach(i -> this .items.add(i));
return builder();
}
@Override
public B addItem (T item) {
ObjectUtils.argumentNotNull(item, "Item must be not null" );
this .items.add(item);
return builder();
}
@Override
protected MultiSelect buildSelect (MultiSelectField instance) {
return instance;
}
@Override
protected Field> buildSelectAsField(MultiSelectField instance) {
return instance;
}
}
public static class GenericBuilder extends Builder >
implements GenericMultiSelectInputBuilder {
public GenericBuilder (Class type, RenderingMode renderingMode) {
super (type, renderingMode);
}
@Override
protected GenericMultiSelectInputBuilder builder () {
return this ;
}
}
public static class SelectModeBuilder extends Builder >
implements SelectModeMultiSelectInputBuilder {
public SelectModeBuilder (Class type) {
super (type, RenderingMode.SELECT);
}
@Override
public SelectModeMultiSelectInputBuilder rows (int rows) {
getInstance().setRows(rows);
return this ;
}
@Override
protected SelectModeMultiSelectInputBuilder builder () {
return this ;
}
}
public static class OptionsModeBuilder extends Builder >
implements OptionsModeMultiSelectInputBuilder {
public OptionsModeBuilder (Class type) {
super (type, RenderingMode.OPTIONS);
}
@Override
public OptionsModeMultiSelectInputBuilder htmlContentAllowed (boolean htmlContentAllowed) {
getInstance().setHtmlContentAllowed(htmlContentAllowed);
return this ;
}
@Override
public OptionsModeMultiSelectInputBuilder itemEnabledProvider (SerializablePredicate itemEnabledProvider) {
ObjectUtils.argumentNotNull(itemEnabledProvider, "ItemEnabledProvider must be not null" );
getInstance().setItemEnabledProvider(itemEnabledProvider);
return this ;
}
@Override
protected OptionsModeMultiSelectInputBuilder builder () {
return this ;
}
}
public static abstract class PropertyBuilder > extends
AbstractSelectFieldBuilder , MultiSelect , T , PropertyBox , MultiSelectField , B >
implements MultiPropertySelectInputBuilder {
@SuppressWarnings ({ "unchecked" , "rawtypes" })
public PropertyBuilder (Property selectProperty, RenderingMode renderingMode) {
super (new MultiSelectField<>(selectProperty.getType(), renderingMode));
itemIdentifier = new PropertyItemIdentifier(selectProperty);
getInstance().setItemConverter(new DefaultPropertyBoxConverter<>(selectProperty));
}
@Override
public B dataSource (ItemDataProvider dataProvider) {
ObjectUtils.argumentNotNull(dataProvider, "ItemDataProvider must be not null" );
this .itemDataProvider = dataProvider;
return builder();
}
@Override
protected MultiSelect buildSelect (MultiSelectField instance) {
return instance;
}
@Override
protected Field> buildSelectAsField(MultiSelectField instance) {
return instance;
}
}
public static class GenericPropertyBuilder extends PropertyBuilder >
implements GenericMultiPropertySelectInputBuilder {
public GenericPropertyBuilder (Property selectProperty, RenderingMode renderingMode) {
super (selectProperty, renderingMode);
}
@Override
protected GenericMultiPropertySelectInputBuilder builder () {
return this ;
}
}
public static class SelectModePropertyBuilder
extends PropertyBuilder >
implements SelectModeMultiPropertySelectInputBuilder {
public SelectModePropertyBuilder (Property selectProperty) {
super (selectProperty, RenderingMode.SELECT);
}
@Override
public SelectModeMultiPropertySelectInputBuilder rows (int rows) {
getInstance().setRows(rows);
return this ;
}
@Override
protected SelectModeMultiPropertySelectInputBuilder builder () {
return this ;
}
}
public static class OptionsModePropertyBuilder
extends PropertyBuilder >
implements OptionsModeMultiPropertySelectInputBuilder {
public OptionsModePropertyBuilder (Property selectProperty) {
super (selectProperty, RenderingMode.OPTIONS);
}
@Override
public OptionsModeMultiPropertySelectInputBuilder htmlContentAllowed (boolean htmlContentAllowed) {
getInstance().setHtmlContentAllowed(htmlContentAllowed);
return this ;
}
@Override
public OptionsModeMultiPropertySelectInputBuilder itemEnabledProvider (
SerializablePredicate itemEnabledProvider) {
ObjectUtils.argumentNotNull(itemEnabledProvider, "ItemEnabledProvider must be not null" );
getInstance().setItemEnabledProvider(itemEnabledProvider);
return this ;
}
@Override
protected OptionsModeMultiPropertySelectInputBuilder builder () {
return this ;
}
}
}