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:
08: import org.libresource.LibresourceResourceValue;
09: import org.libresource.kernel.comparator.FactoryComparator;
10:
11: public class FlatStructure extends ListStructure {
12: private ArrayList<LibresourceResourceValue> data;
13:
14: public FlatStructure() {
15: data = new ArrayList<LibresourceResourceValue>();
16: setLimit(Integer.MAX_VALUE);
17: }
18:
19: public void addItem(LibresourceResourceValue item) {
20: data.add(item);
21: }
22:
23: public void print(Writer writer) throws IOException {
24: // Sort before any print
25: Collections.sort(data, FactoryComparator.getComparator(
26: getGroupBy(), getSort()));
27: // Local vars
28: String currentGroupBy = null;
29: int currentLimitValue = getLimit();
30:
31: // Print the flat list
32: writer.write("<ul>");
33: for (LibresourceResourceValue item : data) {
34: if (getGroupBy() != null
35: && (currentGroupBy == null || !getInfo(item,
36: getGroupBy()).equals(currentGroupBy))) {
37: if (currentGroupBy != null) {
38: writer.write("</ul>");
39: }
40: currentGroupBy = getInfo(item, getGroupBy());
41: writer.write("<li><b>");
42: writer.write(currentGroupBy);
43: writer.write("</b></li><ul>");
44: // Reset de limit
45: currentLimitValue = getLimit();
46: }
47: // show my resource
48: if (currentLimitValue-- > 0) {
49: writer.write("<li><a href=\"");
50: writer.write(item.getUri().getPath().substring(1));
51: writer.write("\">");
52: writer.write(item.getShortResourceName());
53: writer.write("</a></li>");
54: }
55: }
56: // Close the last group by
57: if (currentGroupBy != null)
58: writer.write("</ul>");
59: // Close the root node
60: writer.write("</ul>");
61: }
62:
63: }
|