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

de.factoryfx.javafx.util.DataTextFieldTreeCell Maven / Gradle / Ivy

package de.factoryfx.javafx.util;

import java.util.Optional;
import java.util.function.Function;

import de.factoryfx.data.Data;
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 Optional displayTextObservable=Optional.empty();
    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){
                displayTextObservable.ifPresent(readOnlyStringProperty -> readOnlyStringProperty.removeListener(changeListener));
                displayTextObservable= Optional.of(dataSupplier.apply(item).internal().getDisplayTextObservable());
                changeListenerGarbageCollectionSave = (observable, oldValue, newValue) -> {
                    setText(dataSupplier.apply(item).internal().getDisplayText());
                };
                changeListener = new WeakChangeListener<>(changeListenerGarbageCollectionSave);
                displayTextObservable.get().addListener(changeListener);
                changeListener.changed(displayTextObservable.get(),null,displayTextObservable.get().get());
            } else {
                setText(alternativeDisplayText.apply(item));
            }

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy