01: package org.libresource.kernel.comparator.util;
02:
03: import java.io.IOException;
04: import java.io.Writer;
05: import java.util.ArrayList;
06: import java.util.Collections;
07: import java.util.Hashtable;
08:
09: import org.libresource.LibresourceResourceValue;
10: import org.libresource.kernel.UriManager;
11: import org.libresource.kernel.comparator.FactoryComparator;
12: import org.libresource.kernel.comparator.util.TreeNode;
13:
14: public class TreeStructure extends ListStructure {
15: private ArrayList<TreeNode> data;
16: private Hashtable<String, TreeNode> tree;
17: private boolean showACL;
18:
19: public TreeStructure() {
20: this (false);
21: }
22:
23: public TreeStructure(boolean showACL) {
24: data = new ArrayList<TreeNode>();
25: tree = new Hashtable<String, TreeNode>();
26: this .showACL = showACL;
27: }
28:
29: public void addItem(LibresourceResourceValue item) {
30: TreeNode parentNode = tree.get(UriManager.getParentPath(item
31: .getUri()));
32: TreeNode currentNode = new TreeNode(item);
33: if (parentNode == null) {
34: data.add(currentNode);
35: tree.put(item.getUri().getPath(), currentNode);
36: } else {
37: parentNode.addChild(currentNode);
38: tree.put(item.getUri().getPath(), currentNode);
39: }
40: }
41:
42: public void print(Writer writer) throws IOException {
43: Collections.sort(data, FactoryComparator.getComparator(
44: getGroupBy(), getSort()));
45: String currentGroupBy = null;
46: writer.write("<ul>");
47: for (TreeNode currentNode : data) {
48: if (getGroupBy() != null
49: && (currentGroupBy == null || !currentGroupBy
50: .equals(ListStructure.getInfo(currentNode
51: .getValue(), getGroupBy())))) {
52: if (currentGroupBy != null) {
53: writer.write("</ul>");
54: }
55: currentGroupBy = ListStructure.getInfo(currentNode
56: .getValue(), getGroupBy());
57: writer.write("<li><b>");
58: writer.write(currentGroupBy);
59: writer.write("</b></li><ul>");
60: }
61: currentNode.print(writer, getGroupBy(), getSort(), showACL);
62: }
63: if (getGroupBy() != null) {
64: writer.write("</ul>");
65: }
66: writer.write("</ul>");
67: }
68:
69: }
|