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

io.imunity.vaadin.auth.services.tabs.GroupedValuesChipsWithDropdown Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
/*
 * Copyright (c) 2021 Bixbit - Krzysztof Benedyczak. All rights reserved.
 * See LICENCE.txt file for licensing information.
 */

package io.imunity.vaadin.auth.services.tabs;

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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class GroupedValuesChipsWithDropdown extends MultiSelectComboBox
{
	private static final String LABEL_PREFIX = "__label__";

	public GroupedValuesChipsWithDropdown(Map> labels)
	{
		setItemLabelGenerator(
				v -> !v.startsWith(LABEL_PREFIX) ? "\u2003" + v : v.substring(LABEL_PREFIX.length(), v.length()));

		List items = new ArrayList<>();
		for (String key : labels.keySet())
		{
			items.add(LABEL_PREFIX + key);
			items.addAll(labels.get(key)
					.stream()
					.sorted()
					.collect(Collectors.toList()));
		}

		setItems(items);
		addSelectionListener(e ->
		{
			if (!e.isFromClient())
				return;

			e.getAddedSelection()
					.stream()
					.filter(s -> s.startsWith(LABEL_PREFIX))
					.forEach(s ->
					{
						select(labels.get(s.substring(LABEL_PREFIX.length(), s.length())));
						deselect(s);
					});
			e.getRemovedSelection()
					.stream()
					.filter(s -> s.startsWith(LABEL_PREFIX))
					.forEach(s -> deselect(labels.get(s.substring(LABEL_PREFIX.length(), s.length()))));
		});
		setAutoExpand(AutoExpandMode.BOTH);
	}

	public Set getSelectedValue()
	{
		return super.getValue().stream()
				.filter(v -> !v.startsWith(LABEL_PREFIX))
				.collect(Collectors.toSet());
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy