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

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

There is a newer version: 4.0.1
Show newest version
package io.github.factoryfx.javafx.util;

import java.util.function.Function;

import io.github.factoryfx.factory.FactoryBase;
import javafx.beans.InvalidationListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.cell.TextFieldTreeCell;

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

    private ObservableValue displayText;
    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){
                displayText= new ObservableFactoryDisplayText(dataSupplier.apply(item));
                InvalidationListener invalidationListener = observable -> setText(displayText.getValue());
                displayText.addListener(invalidationListener);
                invalidationListener.invalidated(null);
            } else {
                setText(alternativeDisplayText.apply(item));
            }

        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy