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

io.imunity.furms.ui.components.DefaultNameField Maven / Gradle / Ivy

There is a newer version: 4.3.1
Show newest version
/*
 * Copyright (c) 2020 Bixbit s.c. All rights reserved.
 * See LICENSE file for licensing information.
 */

package io.imunity.furms.ui.components;

import com.vaadin.flow.component.checkbox.Checkbox;
import com.vaadin.flow.component.customfield.CustomField;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.shared.Registration;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;

import static com.vaadin.flow.data.value.ValueChangeMode.EAGER;

public class DefaultNameField extends CustomField {
	private static final DateTimeFormatter DD_MM_YY_FORMAT = DateTimeFormatter.ofPattern("yyMMdd");
	private static final int MAX_NAME_SIZE = 20;
	public final TextField textField;
	public final Checkbox checkbox;
	private Registration registration;
	private String resourceName;
	private Optional currentName;
	private Supplier> occupiedNamesGetter;

	public DefaultNameField() {
		textField = new TextField();
		textField.setValueChangeMode(EAGER);
		textField.setMaxLength(20);
		textField.getStyle().set("margin", "0");
		textField.setReadOnly(true);

		checkbox = new Checkbox(getTranslation("component.default-name-field.checkbox.text"));
		checkbox.addValueChangeListener(event -> textField.setReadOnly(event.getValue()));
		checkbox.getStyle().set("margin", "0");
		checkbox.setValue(true);


		Div div = new Div(textField, checkbox);
		div.getStyle().set("display", "flex");
		div.getStyle().set("flex-direction", "column");
		add(div);
	}

	@Override
	protected String generateModelValue() {
		return textField.getValue();
	}

	@Override
	protected void setPresentationValue(String s) {
		textField.setValue(s);
	}

	@Override
	public boolean isReadOnly() {
		return textField.isReadOnly();
	}

	@Override
	public void setReadOnly(boolean readOnly) {
		textField.setReadOnly(readOnly);
		checkbox.setValue(readOnly);
	}
	public void reloadName(String resourceName, Supplier> occupiedNamesGetter, boolean initialValue, String currentName) {
		reloadName(resourceName, occupiedNamesGetter, currentName);
		textField.setReadOnly(initialValue);
		checkbox.setValue(initialValue);
	}

	public void reloadName(String resourceName, Supplier> occupiedNamesGetter, String currentName) {
		this.currentName = Optional.ofNullable(currentName);
		this.resourceName = Optional.ofNullable(resourceName).orElse("");
		this.occupiedNamesGetter = occupiedNamesGetter;
		if(registration != null)
			registration.remove();
		registration = checkbox.addValueChangeListener(event -> {
			if(event.getValue())
				generateName();
		});
		if(checkbox.getValue())
			generateName();
	}

	public void generateName(String resourceName) {
		this.resourceName = resourceName;
		if(isReadOnly())
			generateName();
	}

	public void generateName() {
		if(resourceName.isEmpty())
			return;
		Set names = occupiedNamesGetter.get();
		currentName.ifPresent(names::remove);
		String name;
		int i = 1;
		do {
			String timeAndIteration = ZonedDateTime.now().format(DD_MM_YY_FORMAT) + "-" + i;
			name = resourceName + "-" + timeAndIteration;
			if(name.length() > MAX_NAME_SIZE)
				name = resourceName.substring(0, MAX_NAME_SIZE - timeAndIteration.length() - 1) + "-" + timeAndIteration;
			i++;
		} while(names.contains(name));
		setValue(name);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy