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 fitnesse.wikitext.WikiWidget;
006: import fitnesse.wiki.*;
007: import fitnesse.html.*;
008:
009: import java.util.*;
010:
011: public class CommentAggregatorWidget extends WikiWidget {
012: public static final String AGGREGATE_DIV_ID = "stiqAggregateDiv";
013:
014: public static final String AGGREGATE_TITLE_CLASS = "stiqAggregateTitle";
015:
016: public static final String TEST_LINK_CLASS = "stiqTestLink";
017:
018: public static final String TEST_COMMENT_CLASS = "stiqTestComment";
019:
020: public static final String TEST_TITLE_CLASS = "stiqTestTitle";
021:
022: public static final String TEST_TITLE_CELL_CLASS = "stiqTestTitleRow";
023:
024: public static final String WIDGET_NAME = "Comments";
025:
026: private static final String WIDGET_NAME_OLD = "STIQcomments";
027:
028: private static final String WIDGET_NAME_OLD_2 = "seleniumcomments";
029:
030: private static final String collapsableClosedCss = "hidden";
031:
032: private static final String collapsableClosedImg = "/files/images/collapsableClosed.gif";
033:
034: private static final String commentsTableTitle = " STIQ Test Comments ";
035:
036: public static final String REGEXP = "(?:^!" + WIDGET_NAME
037: + "[ \t]*$)|(?:^!" + WIDGET_NAME + " -R[ \t]*$)|" + "(?:^!"
038: + WIDGET_NAME.toLowerCase() + "[ \t]*$)|(?:^!"
039: + WIDGET_NAME.toLowerCase() + " -R[ \t]*$)|" + "(?:^!"
040: + WIDGET_NAME_OLD + "[ \t]*$)|(?:^!" + WIDGET_NAME_OLD
041: + " -R[ \t]*$)|" + "(?:^!" + WIDGET_NAME_OLD_2
042: + "[ \t]*$)|(?:^!" + WIDGET_NAME_OLD_2 + " -R[ \t]*$)";
043:
044: private boolean recursive;
045:
046: private HtmlTag table;
047:
048: private String endl = HtmlElement.endl;
049:
050: private static final Random random = new Random(System
051: .currentTimeMillis());
052:
053: public CommentAggregatorWidget(ParentWidget parent, String text) {
054: super (parent);
055: setRecursive(text);
056: }
057:
058: private void setRecursive(String text) {
059: recursive = (text.indexOf("-R") > -1);
060: }
061:
062: public String render() throws Exception {
063:
064: return makeCollapsableSection(commentsTableTitle,
065: buildCommentsDiv(getWikiPage(), 1)).html();
066:
067: }
068:
069: private HtmlTag buildCommentsDiv(WikiPage wikiPage, int currentDepth)
070: throws Exception {
071: HtmlTag div = HtmlUtil.makeDivTag(AGGREGATE_DIV_ID);
072: buildTable(wikiPage);
073: div.add(table);
074: div.addAttribute("id", AGGREGATE_DIV_ID);
075: return div;
076: }
077:
078: private void buildTable(WikiPage wikiPage) throws Exception {
079: table = new HtmlTag("table");
080: // AddTableHeading();
081: for (Iterator iterator = buildListOfChildPages(wikiPage)
082: .iterator(); iterator.hasNext();) {
083: addPageToTable((WikiPage) iterator.next());
084: }
085: }
086:
087: // private void AddTableHeading() {
088: // HtmlTag row = new HtmlTag("tr");
089: // HtmlTag data = new HtmlTag("td");
090: // data.addAttribute("class", AGGREGATE_TITLE_CLASS);
091: // data.add(commentsTableTitle);
092: // row.add(data);
093: // table.add(row);
094: // }
095:
096: private void addPageToTable(WikiPage wikiPage) throws Exception {
097: if (wikiPage.isSTIQTest()) {
098: // addPageTitleToTable(wikiPage);
099: addPageCommentsToTable(wikiPage);
100: }
101:
102: // Add children if necessary
103: if (isRecursive() && buildListOfChildPages(wikiPage).size() > 0) {
104: for (Iterator iterator = buildListOfChildPages(wikiPage)
105: .iterator(); iterator.hasNext();) {
106: addPageToTable((WikiPage) iterator.next());
107: }
108: }
109: }
110:
111: private String firstRowOfTable(String[] contentLines) {
112: for (int i = 0; i < contentLines.length - 1; ++i) {
113: int tdStart = contentLines[i].indexOf("<td");
114: if (tdStart != -1) {
115: tdStart = contentLines[i].indexOf(">", tdStart);
116: String commentLine = getTdString(contentLines, i);
117: int tdEnd = commentLine.indexOf("</td>");
118: if (tdStart < tdEnd) {
119: return commentLine.substring(tdStart + 1, tdEnd)
120: .trim();
121: }
122: }
123: }
124: return null;
125: }
126:
127: private void addPageTitleToTable(WikiPage wikiPage,
128: String[] contentLines) {
129: HtmlTag row = new HtmlTag("tr");
130: HtmlTag cell = new HtmlTag("td");
131: cell.addAttribute("class", TEST_TITLE_CELL_CLASS);
132: HtmlTag link = new HtmlTag("a");
133: link.addAttribute("class", TEST_LINK_CLASS);
134: String pageUri = "";
135: try {
136: pageUri = getHref(wikiPage);
137: } catch (Exception e1) {
138: // TODO Auto-generated catch block
139: e1.printStackTrace();
140: }
141:
142: try {
143: link.addAttribute("href", pageUri);
144: link.add(wikiPage.getName());
145: cell.add(link);
146: HtmlTag span = new HtmlTag("span");
147: span.addAttribute("class", TEST_TITLE_CLASS);
148: span.add(firstRowOfTable(contentLines));
149: cell.add(span);
150: } catch (Exception e) {
151: // TODO Auto-generated catch block
152: e.printStackTrace();
153: }
154: row.add(cell);
155: table.add(row);
156: }
157:
158: private void addPageCommentsToTable(WikiPage wikiPage) {
159: try {
160: String htmlContent = wikiPage.getData().getHtml();
161: String[] contentLines = htmlContent.split(endl);
162: addPageTitleToTable(wikiPage, contentLines);
163:
164: for (int i = 0; i < contentLines.length - 1; ++i) {
165: String lookForComment = contentLines[i].replaceAll(" ",
166: "");
167: if (lookForComment
168: .indexOf("<tdclass=\"column1\">comment</td>") != -1) {
169: String commentLine = contentLines[i + 1].trim();
170: commentLine = commentLine.replaceAll(
171: "<td class=\"column2\">", "");
172: commentLine = commentLine.replaceAll("</td>", "");
173: HtmlTag row = new HtmlTag("tr");
174: HtmlTag cell = new HtmlTag("td");
175: cell.addAttribute("class", TEST_COMMENT_CLASS);
176: cell.add(commentLine.trim());
177: row.add(cell);
178: table.add(row);
179: }
180: }
181: } catch (Exception e) {
182: }
183: }
184:
185: private String getTdString(String[] contentLines, int i) {
186: String response = contentLines[i];
187: while (i < contentLines.length - 1
188: && -1 == response.indexOf("</td>")) {
189: response = response + contentLines[++i];
190: }
191: return response.trim();
192: }
193:
194: private List buildListOfChildPages(WikiPage wikiPage)
195: throws Exception {
196: List childPageList = new ArrayList(wikiPage.getChildren());
197: if (wikiPage.hasExtension(VirtualCouplingExtension.NAME)) {
198: VirtualCouplingExtension extension = (VirtualCouplingExtension) wikiPage
199: .getExtension(VirtualCouplingExtension.NAME);
200: WikiPage virtualCoupling = extension.getVirtualCoupling();
201: childPageList.addAll(virtualCoupling.getChildren());
202: }
203: Collections.sort(childPageList);
204: return childPageList;
205: }
206:
207: public boolean isRecursive() {
208: return recursive;
209: }
210:
211: public HtmlTag makeCollapsableSection(String title, HtmlTag content) {
212: String id = "" + random.nextLong();
213: HtmlTag outerDiv = HtmlUtil.makeDivTag("collapse_rim");
214:
215: HtmlTag image = new HtmlTag("img");
216: image.addAttribute("src", collapsableClosedImg);
217: image.addAttribute("class", "imageleft");
218: image.addAttribute("id", "img" + id);
219: HtmlTag anchor = new HtmlTag("a", image);
220: anchor.addAttribute("class", "anchoredimage");
221: anchor.addAttribute("href", "javascript:toggleCollapsable('"
222: + id + "');");
223: outerDiv.add(anchor);
224: outerDiv.add(title);
225:
226: HtmlTag collapsablediv = HtmlUtil
227: .makeDivTag(collapsableClosedCss);
228: collapsablediv.addAttribute("id", id);
229: collapsablediv.add(content);
230: outerDiv.add(collapsablediv);
231:
232: return outerDiv;
233: }
234: }
|