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