Example usage for javafx.scene.control TreeItem setGraphic

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

Introduction

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

Prototype

public final void setGraphic(Node value) 

Source Link

Document

Sets the node that is generally shown to the left of the value property.

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   w  w w  . ja v a2s  .  co  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());
    }
}

From source file:jduagui.Controller.java

private TreeItem<File> buildFileTree(String path) throws IOException {
    File f = new File(path);
    TreeItem<File> root = createNode(f);
    root.setExpanded(true);/*from  ww  w  .  j av  a  2 s .  c om*/
    root.setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/16x16/apps/gnome-disks.png"))));
    return root;
}