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

io.imunity.console.views.signup_and_enquiry.FormAttributeGroupComboBox 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.console.views.signup_and_enquiry;

import com.vaadin.flow.data.renderer.ComponentRenderer;
import io.imunity.vaadin.elements.NotEmptyComboBox;
import pl.edu.icm.unity.base.registration.AttributeRegistrationParam;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;


class FormAttributeGroupComboBox extends NotEmptyComboBox
{
	record GroupModel(
			String name,
			String path
	){}


	private final List groups;
	private List dynamicGroups;

	public FormAttributeGroupComboBox(String caption, Collection groups, List dynamicGroups)
	{
		super(caption);
		this.groups = new ArrayList<>(groups);
		this.dynamicGroups = new ArrayList<>(dynamicGroups);
		setItemLabelGenerator(group -> group.name);
		setRenderer(new ComponentRenderer<>(group -> new GroupItemLabel(group.name, group.path)));
		setInput(null);
	}
	
	void updateDynamicGroups(List dynamicGroups)
	{
		this.dynamicGroups = new ArrayList<>(dynamicGroups);
		setInput(getValue());
	}

	public void setValue(String group)
	{
		GroupModel groupModel = groups.stream()
				.filter(model -> model.path.equals(group))
				.findAny()
				.orElse(null);
		setValue(groupModel);
	}
	
	private void setInput(GroupModel selectedValue)
	{
		groups.sort(Comparator.comparing(model -> model.name));
		List processedGroups = new ArrayList<>(groups);
		for (String dynamicGroup: dynamicGroups)
		{
			String name = AttributeRegistrationParam.DYN_GROUP_PFX + dynamicGroup;
			processedGroups.add(new GroupModel(name, name));
		}
		setItems(processedGroups);
		if (!processedGroups.isEmpty())
		{
			if (selectedValue != null && processedGroups.contains(selectedValue))
				setValue(selectedValue);
			else
				setValue(processedGroups.get(0));
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy