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

de.factoryfx.javafx.widget.datatreeview.DataTreeView Maven / Gradle / Ivy

package de.factoryfx.javafx.widget.datatreeview;

import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import de.factoryfx.data.Data;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TreeItem;

public class DataTreeView {

    private final ObservableList dataList= FXCollections.observableArrayList();
    private final Supplier> listSupplier;
    private final Function> treeSupplier;
    public DataTreeView(Supplier> listSupplier, Function> treeSupplier) {
        this.listSupplier = listSupplier;
        this.treeSupplier = treeSupplier;
    }

    Optional>> updateAction=Optional.empty();
    public void setUpdateAction(Consumer> updateAction) {
        this.updateAction= Optional.of(updateAction);
    }

    public void update(){
        dataList.setAll(listSupplier.get());
        updateAction.ifPresent(treeItemConsumer -> treeItemConsumer.accept(dataTree()));
    }


    private TreeItem dataTree(){
        TreeItem  root = new TreeItem<>(null);
        dataList.forEach(item -> {
            final TreeItem treeItem = treeSupplier.apply(item);
            root.getChildren().add(treeItem);

        });
        return root;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy