File hierarchy Jtree problem! Please help!
Hi! I need some help :).
I have 2 Vectors.
The first is vDirs and that vector contains names of dirs, like this: 1|New Folder;2|New Folder 2; ... The number before the dir is the index of the dir, !not the index of the vector.
The second vector is vFiles amd it contains names of files, like this: 1|file.jpg; 1|file2.gif; 2|file3.jpg;... The number before the name of the file is telling in wich dir the file is located.
I want to place the content of the 2 vectors in one JTree. For example:
"file.jpg" & "file2.gif" are child tree nodes of "New Folder"...
I thing you got my point.
PLEASE HELP! :(
p.s. The vector are fill with dirs and files from a CD/DVD. The vectors' sizes are big :)
[751 byte] By [
Dim3a] at [2007-10-2 20:15:16]

Use:
- StringTokenizer class or split() method from String class to
parse dirs and files name
- Vector<String> class to keep String data
- HashMap class to keep key/Element values
- DefaultMutableTreeNode class to create an inner structure of
JTree
This is an example:
Vector<String> vDdir = new Vector<String>();
Vector<String> vFile = new Vector<String>();
public void initVectors(){
vDdir.add("1|New Folder");
vDdir.add("2|New Folder 2");
vFile.add("1|file.jpg");
vFile.add("1|file2.gif");
vFile.add("2|file3.jpg");
}
public String[] splitString(String string) throws Exception{
String id = "";
String data = "";
StringTokenizer token = new StringTokenizer(string, "|");
if (token.hasMoreTokens()){
id = token.nextToken();
}else{
throw new Exception("Illegal Format: \"" + string + "\"");
}
if (token.hasMoreTokens()){
data = token.nextToken();
}else{
throw new Exception("Illegal Format: \"" + string + "\"");
}
return new String[] { id, data};
}
public JTree initJTree(Vector<String> vDdir, Vector<String> vFile) throws Exception{
MutableTreeNode root = new DefaultMutableTreeNode();
HashMap<String, List><String>> fileMap = new HashMap<String, List><String>> ();
for(String file: vFile){
String[] splitFile = splitString(file);
String fileID= splitFile[0];
String fileName = splitFile[1];
List<String> fileList = fileMap.get(fileID);
if(fileList == null){
fileList = new LinkedList<String>();
fileMap.put(fileID, fileList);
}
fileList.add(fileName);
}
for(String dir: vDdir){
String[] splitDir = splitString(dir);
String dirID= splitDir[0];
String dirName = splitDir[1];
MutableTreeNode dirNode = new DefaultMutableTreeNode(dirName);
root.insert(dirNode, root.getChildCount());
for(String fileName: fileMap.get(dirID)){
MutableTreeNode fileNode = new DefaultMutableTreeNode(fileName);
dirNode.insert(fileNode, dirNode.getChildCount());
}
}
return new JTree( root );
}
In any way the solution for dirs and subdirs is the same
as above solution beetween dirs and subfiles.
You should know information about name of dir, id and id of parent dir.
After that you could put all dirs into HashMap, and when for generating the
tree structure for dirs, you could get subdirs for every dir from the
HashMap and add them into the JTree.
> Oh yes. The sub dirs are in this format:
> 6|New Folder1/New folder7; 7|New Folder1/New
> Folder7/NewFolder18...
>
> The ID is for New Folder 7, so the files from vFiles
> with that ID are located in New Folder 7, and the New
> Folder 7 is located in New Folder1...
The format "dir Id|parent dir Id|dir name" is better.
Transform your format to this and adapt the previous algorithm
( dirs - sub files ) to the necessary ( dir - subdirs )