h3llboy
h3llboy

Reputation: 11

Primefaces TreeNode File Browser

I'm trying to do a primefaces treenode that shows the names of the files or directories that contents one directory. This is what I got at the moment.

The problem is that the code saves the nodes into previous node, but what i want is, if we pass to next value in list, it checks p.example if node C:/ exists, and if it exists, it checks if the next node inside of it, exists and if it exists, it does the same recursively.

public void init() {
    root = new DefaultTreeNode("Files", null);

    try (Stream<Path> paths = Files
            .walk(Paths.get("C:\\Path"))) {
        List<String> list = paths.map(path -> Files.isDirectory(path) ? path.toString() + '/' : path.toString())
                .collect(Collectors.toList());

        for (int i = 0; i < list.size(); i++) {
            String[] prueba = list.get(i).split("\\\\");
             List<String> listaprueba = new ArrayList<String>(Arrays.asList(prueba));
            //removes C:/
            listaprueba.remove(0);
             prueba = listaprueba.toArray(new String[0]);
            for (int e = 0; e < prueba.length; e++) {

                if (root.getChildCount() == 0) {
                    setNode0(new DefaultTreeNode(prueba[0], root));

                } else {

                    if (prueba[e].equals(prueba[prueba.length - 1])) {
                        node0.getChildren().add(new DefaultTreeNode(prueba[e], node0));
                    } else {
                        setNode0(new DefaultTreeNode(prueba[e], node0));

                    }

                }
            }
        }
    }

    catch (

    IOException e) {
        System.out.println("Archivo no encontrado.");
    }
}

Upvotes: 0

Views: 189

Answers (1)

h3llboy
h3llboy

Reputation: 11

Just fixed it, was easier than i tought.

private class Tree{
private TreeNode root;

    private void process(File mainDir, TreeNode root    ) {
        if (mainDir.isDirectory()) {
            TreeNode dad= new DefaultTreeNode(mainDir.getName(), root);
            for (File son: mainDir.listFiles()) {
                process(son, dad);
            }
        } else {
            root.getChildren().add(new DefaultTreeNode(mainDir.getName()));
        }
    }   

    @PostConstruct
    public void init() {

        File mainDir = new File("InsertPathHere");
        root = new DefaultTreeNode("", null);

        process(mainDir, root);
    }

    public TreeNode getRoot() {
        return root;
    }

    public void setRoot(TreeNode root) {
        this.root = root;
    }
}

Upvotes: 1

Related Questions