Search code examples
javaalgorithmswingjtree

Algorithm for converting directory tree (.txt file) to JTree


I'm making a program that will generate a text file that contains directories tree. I've done the file walking part, so I got my text file ready. Then I want to use this text file, read it and convert the data in to JTree. I've been stucking with the algorithm for 2 days!! Does anyone has some suggestions? plase help.

*note that I used "\t" for the spacing.

Some part of my path.txt

path.txt

Arcana Advanced
        ABC
        ABC
        AcnMiniGame
        client
        Data
        error
        patch
        patch_03451
        patch_03452
        patch_03453
        patch_03454
        patch_03455
        patch_03456
        patch_03458
        SaveDeck
                ModifiedDeck1 Earth Water
                ModifiedDeck2 Wind Fire
                ModifiedDeck3 Wind Earth
                ModifiedDeck4 Earth Fire
                ModifiedDeck5 Wind Water
                ModifiedDeck6 Fire Water
                Starter1 Earth Water
                Starter2 Fire Wind
                Starter3 Earth Wind
                Starter4 Earth Fire
                Starter5 Water Wind
                Starter6 Water Fire
        Tutorial
        unicows
        unins000
        unins000
ASActiveX
        Au_activeX
                ThaiGameStart
        nProtect
                npkcx
                        npkagt
                        npkcrypt
                        npkcrypt
                        npkcrypt
                        npkcsvc
                        npkcusb
                        npkcx
                        npkcx
                        npkpdb
                        npkuninst

This is what I've tried so far:

public final class FileTree extends JPanel {


JTree tree;
DefaultMutableTreeNode root;
Path path;
List<String> lines;

public FileTree(String filepath) {
    try {
        root = new DefaultMutableTreeNode("root", true);
        this.path = Paths.get(filepath);
        lines = Files.readAllLines(path);
        getList(root, 0);
        setLayout(new BorderLayout());
        tree = new JTree(root);
        tree.setRootVisible(false);
        add(new JScrollPane((JTree) tree), "Center");
    } catch (IOException ex) {
        Logger.getLogger(FileTree.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public int getTab(int line) {
    String text = lines.get(line);
    return text.lastIndexOf("\t");

}

public boolean noChild(int line) {
    if (getTab(line) < getTab(line + 1)) {
        return false;
    }
    return true;

}

public int getLastLine(int line) {
    int myTab = getTab(line);
    int myLine = line+1;
    int i = line+1;
    while (true) {
        if (myTab == getTab(myLine)) {
            return i;
        } else {
            myLine++;
            i++;
        }
    }
}

public int getChildList(int line) {
    int i = 0;
    int ChildTab = getTab(line + 1);
    int myLine = line + 1;
    while (true) {
        if (ChildTab == getTab(myLine)) {
            myLine++;
            i++;
        } else if (ChildTab < getTab(myLine)) {
            myLine++;
        } else if (ChildTab > getTab(myLine)) {
            return i;
        }
    }
}



public void getList(DefaultMutableTreeNode node, int line) {
    try {
        if (noChild(line)) {
            System.out.println("FILE  -  " + lines.get(line));
            DefaultMutableTreeNode child = new DefaultMutableTreeNode(lines.get(line).trim());
            node.add(child);
        } else {
            System.out.println("DIRECTORY  -  " + lines.get(line));
            DefaultMutableTreeNode child = new DefaultMutableTreeNode(lines.get(line).trim());
            node.add(child);
            int ChildList = getChildList(line);
            for (int i = 0; i < ChildList; i++) {
                getList(child, line + i + 1);
            }

        }
    } catch (Exception e) {
    }

}

}

the result: http://www.uppic.org/image-5E7B_55C470B7.jpg

My code problem seems to be that after it finished exploring the folder, the actual line doesn't know it, and keep reading the next line which is the file in the explored folder. (It's hard to explain in words, sorry for my bad English)

And the second problem is it doesn't read every primary folder, as you can see in the picture, the program stops working after finished exploring "Arcana Advanced" folder. I understand the reason for this problem, so I tried to make another method to check how many primary folders are they, and do a for loop. But that is very messy and time-consuming, anyone has any simpler method to do that ?


Solution

  • Finally, I got a solution for my question.

    public final class FileTree extends JPanel {
    
    JTree tree;
    DefaultMutableTreeNode root;
    Path path;
    List<String> lines;
    
    public FileTree(String filepath) {
        try {
            root = new DefaultMutableTreeNode("root", true);
            this.path = Paths.get(filepath);
            lines = Files.readAllLines(path);
            getList(root, 0);
            setLayout(new BorderLayout());
            tree = new JTree(root);
            tree.setRootVisible(false);
            add(new JScrollPane((JTree) tree), "Center");
        } catch (IOException ex) {
            Logger.getLogger(FileTree.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public int getTab(int line) {
        String text = lines.get(line);
        return text.lastIndexOf("\t");
    
    }
    
    public boolean noChild(int line) {
        if (getTab(line) < getTab(line + 1)) {
            return false;
        }
        return true;
    
    }
    
    
    public TreeNode getNewNode(int line, int line2, DefaultMutableTreeNode node) {
        TreeNode treenode = node;
        int time = getTab(line) - getTab(line2);
        for (int i = 0; i < time; i++) {
            treenode = treenode.getParent();
        }
        return treenode;
    
    }
    
    public void getList(DefaultMutableTreeNode node, int line) {
        DefaultMutableTreeNode child = new DefaultMutableTreeNode(lines.get(line).trim());
        node.add(child);
        if (line + 1 > lines.size() - 1) {
            System.out.println("Finished");
            return;
        }
        if (!noChild(line)) { // have Children
            getList(child, line + 1);
        } else {             // no Children
            if (getTab(line) > getTab(line + 1)) {
                getList((DefaultMutableTreeNode) getNewNode(line, line + 1, node), line + 1);
            } else if (getTab(line) == getTab(line + 1)) {
                getList(node, line + 1);
            }
        }
    
    
    
    
    }
    

    }