01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.wikitext.widgets;
04:
05: import fitnesse.wikitext.WikiWidget;
06: import fitnesse.wiki.*;
07: import fitnesse.html.*;
08:
09: import java.util.*;
10:
11: public class ContentsWidget extends WikiWidget {
12: public static final String REGEXP = "(?:^!contents[ \t]*$)|(?:^!contents -R[ \t]*$)";
13:
14: private boolean recursive;
15:
16: public ContentsWidget(ParentWidget parent, String text) {
17: super (parent);
18: setRecursive(text);
19: }
20:
21: private void setRecursive(String text) {
22: recursive = (text.indexOf("-R") > -1);
23: }
24:
25: public String render() throws Exception {
26: return buildContentsDiv(getWikiPage(), 1).html();
27: }
28:
29: private HtmlTag buildContentsDiv(WikiPage wikiPage, int currentDepth)
30: throws Exception {
31: HtmlTag div = makeDivTag(currentDepth);
32: div.add(buildList(wikiPage, currentDepth));
33: return div;
34: }
35:
36: private HtmlTag buildList(WikiPage wikiPage, int currentDepth)
37: throws Exception {
38: HtmlTag list = new HtmlTag("ul");
39: for (Iterator iterator = buildListOfChildPages(wikiPage)
40: .iterator(); iterator.hasNext();) {
41: list.add(buildListItem((WikiPage) iterator.next(),
42: currentDepth));
43: }
44: return list;
45: }
46:
47: private HtmlTag buildListItem(WikiPage wikiPage, int currentDepth)
48: throws Exception {
49: HtmlTag listItem = new HtmlTag("li");
50: listItem.add(HtmlUtil.makeLink(getHref(wikiPage),
51: getLinkText(wikiPage)));
52: if (isRecursive() && buildListOfChildPages(wikiPage).size() > 0) {
53: listItem.add(buildContentsDiv(wikiPage, currentDepth + 1));
54: }
55: return listItem;
56: }
57:
58: private List buildListOfChildPages(WikiPage wikiPage)
59: throws Exception {
60: List childPageList = new ArrayList(wikiPage.getChildren());
61: if (wikiPage.hasExtension(VirtualCouplingExtension.NAME)) {
62: VirtualCouplingExtension extension = (VirtualCouplingExtension) wikiPage
63: .getExtension(VirtualCouplingExtension.NAME);
64: WikiPage virtualCoupling = extension.getVirtualCoupling();
65: childPageList.addAll(virtualCoupling.getChildren());
66: }
67: Collections.sort(childPageList);
68: return childPageList;
69: }
70:
71: private HtmlTag makeDivTag(int currentDepth) {
72: return HtmlUtil.makeDivTag("toc" + currentDepth);
73: }
74:
75: public boolean isRecursive() {
76: return recursive;
77: }
78: }
|