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

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

The newest version!
package fi.evolver.ai.vaadin.component.form;

import java.io.Serial;
import java.util.Map;

import org.vaadin.lineawesome.LineAwesomeIcon;

import com.vaadin.flow.component.AbstractField;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.checkbox.Checkbox;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.shared.InputField;
import com.vaadin.flow.component.textfield.IntegerField;
import com.vaadin.flow.component.textfield.NumberField;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.theme.lumo.LumoUtility;

import fi.evolver.ai.vaadin.entity.HasValueGetterSetter;

public class KeyValueInput, TType>, TType> extends HorizontalLayout implements HasValueGetterSetter> {
	@Serial
	private static final long serialVersionUID = 1L;
	private final TextField keyInput = new TextField();
	private final TField valueInput;

	private Runnable onRemove;

	public KeyValueInput(TField valueInput) {
		this.valueInput = valueInput;
		setWidthFull();
		addClassName(LumoUtility.AlignItems.END);

		keyInput.setLabel(getTranslation("component.form.keyValueInput.key"));
		keyInput.setWidth("40%");
		add(keyInput);

		valueInput.setLabel(getTranslation("component.form.keyValueInput.value"));
		valueInput.setWidth("40%");
		add(valueInput);

		Div div = new Div();
		div.setWidth("20%");
		div.addClassNames(LumoUtility.Display.FLEX, LumoUtility.JustifyContent.END);
		add(div);

		Button removeBtn = new Button(LineAwesomeIcon.TRASH_ALT.create());
		removeBtn.addClickListener(e -> {
			if (onRemove != null)
				onRemove.run();
		});
		div.add(removeBtn);
	}

	public KeyValueInput(TField valueInput, Map.Entry entry) {
		this(valueInput);
		setValue(entry);
	}

	public KeyValueInput(TField valueInput, String key, TType value) {
		this(valueInput, Map.entry(key, value));
	}

	public void addRemoveHandler(Runnable onRemove) {
		this.onRemove = onRemove;
	}

	public String getKey() {
		return keyInput.getValue();
	}

	@Override
	public Map.Entry getValue() {
		return Map.entry(keyInput.getValue(), valueInput.getValue());
	}

	@Override
	public void setValue(Map.Entry value) {
		keyInput.setValue(value.getKey());
		valueInput.setValue(value.getValue());
	}

	public static KeyValueInput of(Map.Entry entry) {
		return KeyValueInput.of(entry.getKey(), entry.getValue());
	}

	public static KeyValueInput of(String key, Object value) {
		if (value instanceof String s)
			return new KeyValueInput<>(new TextField(), key, s);
		if (value instanceof Boolean b)
			return new KeyValueInput<>(new Checkbox(), key, b);
		if (value instanceof Double d)
			return new KeyValueInput<>(new NumberField(), key, d);
		if (value instanceof Integer i){
			IntegerField field = new IntegerField();
			field.setStepButtonsVisible(true);
			return new KeyValueInput<>(field, key, i);
		}
		TextField field = new TextField();
		field.setEnabled(false);
		return new KeyValueInput<>(field, key, value.toString());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy