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

io.imunity.vaadin.elements.CustomValuesMultiSelectComboBox Maven / Gradle / Ivy

/*
 * Copyright (c) 2021 Bixbit - Krzysztof Benedyczak. All rights reserved.
 * See LICENCE.txt file for licensing information.
 */

package io.imunity.vaadin.elements;

import com.vaadin.flow.component.combobox.MultiSelectComboBox;
import com.vaadin.flow.component.combobox.dataview.ComboBoxListDataView;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class CustomValuesMultiSelectComboBox extends MultiSelectComboBox
{

	private Set items;

	public CustomValuesMultiSelectComboBox(String label)
	{
		this();
		setLabel(label);
	}

	public CustomValuesMultiSelectComboBox()
	{
		items = new HashSet<>();
		setAllowCustomValue(true);
		addCustomValueSetListener(event ->
		{
			HashSet values = new HashSet<>(getValue());
			values.add(event.getDetail());
			setValue(values);
		});
	}

	@Override
	public ComboBoxListDataView setItems(Collection items)
	{
		ComboBoxListDataView stringComboBoxListDataView = super.setItems(items);
		this.items = stringComboBoxListDataView.getItems().collect(Collectors.toCollection(LinkedHashSet::new));
		return stringComboBoxListDataView;
	}

	@Override
	public ComboBoxListDataView setItems(String... items)
	{
		ComboBoxListDataView stringComboBoxListDataView = super.setItems(items);
		this.items = stringComboBoxListDataView.getItems().collect(Collectors.toSet());
		return stringComboBoxListDataView;
	}

	@Override
	public void setValue(Set values)
	{
		if(values == null)
			return;
		if(!items.containsAll(values))
		{
			items.addAll(values);
			setItems(items);
		}
		super.setValue(values);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy