io.github.factoryfx.javafx.util.DataTextFieldTreeCell Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javafxFactoryEditing Show documentation
Show all versions of javafxFactoryEditing Show documentation
factoryfx dependency injection framework
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));
}
}
}
}