001: package fitnesse.wikitext.widgets;
002:
003: import fitnesse.wikitext.WikiWidget;
004: import fitnesse.wiki.*;
005: import fitnesse.html.*;
006:
007: import java.util.*;
008:
009: public class ComponentsWidget extends WikiWidget {
010: public static final String WIDGET_TITLE = "STIQ Components";
011:
012: public static final String WIDGET_NAME = "Components";
013:
014: public static final String REGEXP = //
015: "(?:^!" + WIDGET_NAME + "[ \t]*$)|" //
016: + "(?:^!" + WIDGET_NAME + " -R[ \t]*$)|" //
017: + "(?:^!" + WIDGET_NAME.toLowerCase() + "[ \t]*$)|" //
018: + "(?:^!" + WIDGET_NAME.toLowerCase() + " -R[ \t]*$)";
019:
020: private boolean recursive;
021:
022: private HtmlTag table;
023:
024: public ComponentsWidget(ParentWidget parent, String text) {
025: super (parent);
026: setRecursive(text);
027: }
028:
029: private void setRecursive(String text) {
030: recursive = (text.indexOf("-R") > -1);
031: }
032:
033: public String render() throws Exception {
034: return buildContentsDiv(getWikiPage(), 1).html();
035: }
036:
037: private HtmlTag buildContentsDiv(WikiPage wikiPage, int currentDepth)
038: throws Exception {
039: HtmlTag div = makeDivTag(currentDepth);
040: buildTable(wikiPage);
041: div.add(table);
042: return div;
043: }
044:
045: private void buildTable(WikiPage wikiPage) throws Exception {
046: table = new HtmlTag("table");
047: table.addAttribute("class", "suite-table");
048: addTableHeading();
049: for (Iterator iterator = buildListOfChildPages(wikiPage)
050: .iterator(); iterator.hasNext();) {
051: addPageToTable((WikiPage) iterator.next());
052: }
053: }
054:
055: private void addTableHeading() {
056: HtmlTag row = new HtmlTag("tr");
057: HtmlTag data = new HtmlTag("td");
058: data.add("<span class=\"title\">" + WIDGET_TITLE + "</span>");
059: row.add(data);
060: table.add(row);
061: }
062:
063: private void addPageToTable(WikiPage wikiPage) throws Exception {
064: if (wikiPage.isSTIQTestComponent()) {
065: // Add the page link row to the table
066: HtmlTag row = new HtmlTag("tr");
067: HtmlTag data = new HtmlTag("td");
068: String linkText = wikiPage.getName();
069:
070: // If !components is recursive, we might include the parent
071: if ((getShowParentInSuiteLink().equals("true"))
072: && recursive) {
073: linkText = wikiPage.getParent().getName()
074: + getParentChildLinkSeparator()
075: + wikiPage.getName();
076: }
077:
078: data.add(HtmlUtil.makeLink(getHref(wikiPage), linkText));
079: row.add(data);
080: table.add(row);
081: }
082:
083: // Add children if necessary
084: if (isRecursive() && buildListOfChildPages(wikiPage).size() > 0) {
085: for (Iterator iterator = buildListOfChildPages(wikiPage)
086: .iterator(); iterator.hasNext();) {
087: addPageToTable((WikiPage) iterator.next());
088: }
089: }
090: }
091:
092: private List buildListOfChildPages(WikiPage wikiPage)
093: throws Exception {
094: List childPageList = new ArrayList(wikiPage.getChildren());
095: if (wikiPage.hasExtension(VirtualCouplingExtension.NAME)) {
096: VirtualCouplingExtension extension = (VirtualCouplingExtension) wikiPage
097: .getExtension(VirtualCouplingExtension.NAME);
098: WikiPage virtualCoupling = extension.getVirtualCoupling();
099: childPageList.addAll(virtualCoupling.getChildren());
100: }
101: Collections.sort(childPageList);
102: return childPageList;
103: }
104:
105: private HtmlTag makeDivTag(int currentDepth) {
106: return HtmlUtil.makeDivTag("STIQ" + WIDGET_NAME + currentDepth);
107: }
108:
109: public boolean isRecursive() {
110: return recursive;
111: }
112: }
|