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

io.github.factoryfx.javafx.util.DataTextFieldTreeCell Maven / Gradle / Ivy

package io.github.factoryfx.javafx.util;

import java.util.function.Function;

import io.github.factoryfx.factory.FactoryBase;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.WeakChangeListener;
import javafx.scene.control.cell.TextFieldTreeCell;

/** treeview cell for data with auto updatable displaytext*/
public class DataTextFieldTreeCell extends TextFieldTreeCell {

    private WeakChangeListener changeListener;
    private ReadOnlyStringProperty displayText;
    private ChangeListener changeListenerGarbageCollectionSave;
    private final Function> dataSupplier;
    private final Function alternativeDisplayText;

    public DataTextFieldTreeCell(Function> dataSupplier, Function alternativeDisplayText){
        this.dataSupplier = dataSupplier;
        this.alternativeDisplayText = alternativeDisplayText;
    }

    @Override
    public void updateItem(T item, boolean empty) {
        super.updateItem(item, empty);

        if (item != null) {
            if (dataSupplier.apply(item)!=null){
                if (displayText!=null){
                    displayText.removeListener(changeListener);
                }

                displayText= new DataObservableDisplayText(dataSupplier.apply(item)).get();
                changeListenerGarbageCollectionSave = (observable, oldValue, newValue) -> {
                    setText(dataSupplier.apply(item).internal().getDisplayText());
                };
                changeListener = new WeakChangeListener<>(changeListenerGarbageCollectionSave);
                displayText.addListener(changeListener);
                changeListener.changed(displayText,null,displayText.get());
            } else {
                setText(alternativeDisplayText.apply(item));
            }

        }
        //CellUtils.updateItem(this, getConverter(), hbox, getTreeItemGraphic(), textField);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy