Need NameBuilderListener without DataSource

I need NameBuilderListener functionality for a couple of screens but I cannot use existing one from security.user package for it’s using deprecated DataSource and FieldGroup. Is there a replacement I can use?

Hi,
There is no replacement of this class for new GUI API.
Also this class is an internal class, it’s not intended for public use. You can use it if you want since it’s an open source, but it may be eventually removed in the new version of the platform (in Jmix).

If someone else has similar requirement, I made my custom class based on NamedBuilderListener using InstanceContainer.

Updated Class Code:

import com.haulmont.chile.core.model.MetaProperty;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.sys.AppContext;
import com.haulmont.cuba.gui.model.InstanceContainer;
import com.haulmont.cuba.security.global.UserUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;

public class NameBuilder<T extends Entity> {

    public static final String DEFAULT_NAME_PATTERN = "{FF| }{LL}";

    protected InstanceContainer<T> instanceContainer;

    protected String pattern;

    public NameBuilder(InstanceContainer<T> instanceContainer, String pattern) {
        this(instanceContainer);
        this.pattern = pattern;
    }

    public NameBuilder(InstanceContainer<T> instanceContainer) {
        if (instanceContainer == null)
            throw new IllegalArgumentException("window is null");

        this.instanceContainer = instanceContainer;
    }

    protected boolean isGeneratingDisplayName(
            String pattern, String firstName, String lastName, String middleName, String property, Object prevValue) {
        String name = getFieldValue("name");
        if (StringUtils.isNotEmpty(name)) {

            switch (property) {
                case "firstName": firstName = (String) prevValue;
                    break;
                case "lastName": lastName = (String) prevValue;
                    break;
                case "middleName": middleName = (String) prevValue;
                    break;
                default:
                    // no action
                    break;
            }

            String displayName;
            try {
                displayName = UserUtils.formatName(pattern, firstName, lastName, middleName);
            } catch (ParseException e) {
                return false;
            }

            if (!name.equals(displayName)) {
                return true;
            }
        }

        return false;
    }

    protected void setFullName(String displayedName) {
        instanceContainer.getItem().setValue("name", displayedName);
    }

    protected String getFieldValue(String name) {
        return instanceContainer.getItem().getValue(name);
    }

    public void itemPropertyChanged(InstanceContainer.ItemPropertyChangeEvent e) {

        if (!"firstName".equals(e.getProperty())
                && !"lastName".equals(e.getProperty())
                && !"middleName".equals(e.getProperty())) {
            return;
        }

        String firstName = getFieldValue("firstName");
        String lastName = getFieldValue("lastName");
        String middleName = getFieldValue("middleName");

        String displayedName;
        try {
            if (this.pattern == null) {
                // todo rework with Config interface
                pattern = AppContext.getProperty("cuba.user.fullNamePattern");
                if (StringUtils.isBlank(pattern))
                    pattern = DEFAULT_NAME_PATTERN;
            }

            if (isGeneratingDisplayName(pattern, firstName, lastName, middleName, e.getProperty(), e.getPrevValue())) {
                return;
            }

            displayedName = UserUtils.formatName(pattern, firstName, lastName, middleName);
        } catch (ParseException pe) {
            displayedName = "";
        }

        MetaProperty nameProperty = e.getItem().getMetaClass().getProperty("name");
        if (nameProperty != null && nameProperty.getAnnotations().containsKey("length")) {
            int length = (int) nameProperty.getAnnotations().get("length");
            if (displayedName.length() > length) {
                displayedName = "";
            }
        }

        setFullName(displayedName);
    }
}

Usage:

@Inject
private InstanceContainer<Student> studentDc;
@Subscribe
public void onInit(InitEvent event) {
    final NameBuilder<Student> studentNameBuilder = new NameBuilder<>(studentDc);
    studentDc.addItemPropertyChangeListener(e -> {
        studentNameBuilder.itemPropertyChanged(e);
    });
}