Example usage for javafx.scene.control TreeItem addEventHandler

List of usage examples for javafx.scene.control TreeItem addEventHandler

Introduction

In this page you can find the example usage for javafx.scene.control TreeItem addEventHandler.

Prototype

public <E extends Event> void addEventHandler(EventType<E> eventType, EventHandler<E> eventHandler) 

Source Link

Document

Registers an event handler to this TreeItem.

Usage

From source file:com.heliosdecompiler.helios.gui.controller.FileTreeController.java

public void updateTree(List<TreeNode> add, List<TreeNode> remove) {
    Set<TreeItem<TreeNode>> updated = new HashSet<>();

    ArrayDeque<TreeNode> queue = new ArrayDeque<>();
    queue.addAll(add);/*from  www  .  j  a  v  a2 s  . c  o m*/

    while (!queue.isEmpty()) {
        TreeNode thisNode = queue.pop();

        TreeItem<TreeNode> parent;

        if (thisNode.getParent() == null) {
            parent = rootItem;
        } else {
            parent = itemMap.get(thisNode.getParent());
        }

        updated.add(parent);

        TreeItem<TreeNode> thisItem = new TreeItem<>(thisNode);
        thisItem.addEventHandler(TreeItem.<TreeNode>branchExpandedEvent(), event -> {
            if (thisItem.getChildren().size() == 1) {
                thisItem.getChildren().get(0).setExpanded(true);
            }
        });
        thisItem.setGraphic(new ImageView(new Image(getIconForTreeItem(thisNode))));
        FutureTask<Void> call = new FutureTask<>(() -> {
            parent.getChildren().add(thisItem);
            return null;
        });
        Platform.runLater(call);
        try {
            call.get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }

        itemMap.put(thisNode, thisItem);

        queue.addAll(thisNode.getChildren());
    }

    for (TreeItem<TreeNode> parent : updated) {
        if (parent.getChildren().size() > 1) {
            FutureTask<Void> call = new FutureTask<>(() -> {
                parent.getChildren().sort((a, b) -> {
                    int ac = a.getValue().getChildren().size();
                    int bc = b.getValue().getChildren().size();

                    if (ac == 0 && bc != 0)
                        return 1;
                    else if (ac != 0 && bc == 0)
                        return -1;
                    return a.getValue().getDisplayName().compareTo(b.getValue().getDisplayName());
                });
                return null;
            });
            Platform.runLater(call);
            try {
                call.get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }
    }

    queue.addAll(remove);

    while (!queue.isEmpty()) {
        TreeNode thisNode = queue.pop();
        TreeItem<TreeNode> thisItem = itemMap.remove(thisNode);
        thisItem.getParent().getChildren().remove(thisItem);
        queue.addAll(thisNode.getChildren());
    }
}