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

fi.evolver.ai.vaadin.component.form.KeyValueMapForm Maven / Gradle / Ivy

There is a newer version: 1.5.5
Show newest version
package fi.evolver.ai.vaadin.component.form;

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

import com.vaadin.flow.component.checkbox.Checkbox;
import com.vaadin.flow.component.contextmenu.MenuItem;
import com.vaadin.flow.component.contextmenu.SubMenu;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.menubar.MenuBar;
import com.vaadin.flow.component.menubar.MenuBarVariant;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.IntegerField;
import com.vaadin.flow.component.textfield.NumberField;
import com.vaadin.flow.component.textfield.TextField;

import fi.evolver.ai.vaadin.entity.HasValueGetterSetter;
import fi.evolver.utils.string.StringUtils;

public class KeyValueMapForm extends VerticalLayout implements HasValueGetterSetter> {
	private final List> parameterInputs = new ArrayList<>();
	private Div content = new Div();
	public KeyValueMapForm() {
		setWidthFull();
		content.setWidthFull();
		add(content);
		addButtons();
	}

	public KeyValueMapForm(Map keyValueMap) {
		this();
		setValue(keyValueMap);
	}

	private void addButtons() {
		MenuBar menuBar = new MenuBar();
		menuBar.addThemeVariants(MenuBarVariant.LUMO_ICON);
		MenuItem addStringBtn = menuBar.addItem(getTranslation("component.form.keyValueForm.addString"));
		MenuItem dropdownBtn = menuBar.addItem(new Icon(VaadinIcon.CHEVRON_DOWN));
		SubMenu dropdown = dropdownBtn.getSubMenu();
		MenuItem addIntBtn = dropdown.addItem(getTranslation("component.form.keyValueForm.addInt"));
		MenuItem addNumberBtn = dropdown.addItem(getTranslation("component.form.keyValueForm.addNumeric"));
		MenuItem addBoolBtn = dropdown.addItem(getTranslation("component.form.keyValueForm.addBool"));
		add(menuBar);

		addStringBtn.addClickListener(e -> addParameterInput(new KeyValueInput<>(new TextField())));
		addIntBtn.addClickListener(e -> {
			IntegerField field = new IntegerField();
			field.setStepButtonsVisible(true);
			addParameterInput(new KeyValueInput<>(field));
		});
		addNumberBtn.addClickListener(e -> addParameterInput(new KeyValueInput<>(new NumberField())));
		addBoolBtn.addClickListener(e -> addParameterInput(new KeyValueInput<>(new Checkbox())));
	}

	private void addParameterInput(KeyValueInput parameterInput) {
		parameterInputs.add(parameterInput);
		content.add(parameterInput);
		parameterInput.addRemoveHandler(() -> {
			parameterInputs.remove(parameterInput);
			content.remove(parameterInput);
		});
	}

	@Override
	public void setValue(Map keyValueMap) {
		parameterInputs.forEach(content::remove);
		parameterInputs.clear();
		if (keyValueMap != null)
			keyValueMap.entrySet().stream().map(KeyValueInput::of).forEach(this::addParameterInput);
	}

	@Override
	public Map getValue() {
		return parameterInputs.stream()
				.filter(field -> StringUtils.hasText(field.getKey()))
				.map(KeyValueInput::getValue)
				.collect(Collectors.toMap(x -> (String)x.getKey(), Map.Entry::getValue));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy