Habe inzwischen etwas gefunden, das funktioniert. Vieleicht interessiert die Lösung ja auch den ein oder anderen :)
An die Funktion wird übergeben:
ein Vector, in dem sich die Objekte befinden, die den Baum darstellen sollen
Ein File objekt (custom Klasse), welches das Vaterelement darstellt. (Beim ersten Aufruf also das root-element des Baumes).
Ein Int-Wert start, der angibt, ab welchem Punkt der Vector durchlaufen werden soll.
Näheres dazu steht auch nochmal im Kommentar zur Funktion.
Ich hoffe, man kann es so halbwegs verstehen :)
Falls jemandem Unstimmigkeiten auffallen sollten (etwa im comment), dann wäre ich über einen Hinweis dankbar.
|
Java Quellcode
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
// files --> Vector holding File objects
DefaultMutableTreeNode root = getNode(files, files.get(0), 1);
JTree tree = new JTree(root);
/**
* This function will accept a Vector from which the tree will be build, a
* parent object of type File (package main.File) and an integer start. The
* function will search through the Vector, starting at position "start",
* looking for File objects that have a higher level than parent. This
* means, that they are children of parent. At this point, recursion appears
* and the function will be called again for each child, this time using the
* child as parent of a new subtree, which will hold all the child's
* children. The search stops as soon as the next File object has a
* level-value lower than the parent. According to the structure of the
* Vector, the current branch ends here. In a "recursion-call", all the File
* objects will be traversed again, even those that have been added to the
* tree BUT have a lower number than the current parent-node. This would
* cause an unwanted return of the function. To counter this, the part of
* the Vector that is being searched needs to start at the element that
* comes right after the parent element.
*
* Sample Vector structure:
* File a - level 0
* File b - level 1
* File c - level 2
* File d - level 2
* File e - level 1
* File f - level 2
*
* According tree structure:
* a
* . b
* . . c
* . . d
* . e
* . . f
*
*
* @param v
* - Vector containing all File objects that are to be inserted
* into the tree.
* @param parent
* - parent File.
* @param start
* - indicates the position from where to go through the vector.
* @return DefaultMutableTreeNode that already contains all children.
*/
public DefaultMutableTreeNode getNode(Vector<MyFile> v, MyFile parent, int start) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(parent);
if (!parent.isDirectory()) {
node.setAllowsChildren(false);
}
// loop through the vector
for (int i = start; i < v.size(); i++) {
// only consider entries that have a level 1 point higher than the parent (parent level + 1)
if (v.get(i).getLevel() == parent.getLevel() + 1) {
// recursion - get subtree of the new child and add it to the parent
node.add(getNode(v, v.get(i), i + 1));
} else {
// stop looping once an element is encountered that has a lower
// level than the parent
if (v.get(i).getLevel() <= parent.getLevel()) {
break;
}
}
}
return node;
}
|