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.html.HtmlElement;
006: import fitnesse.html.HtmlTag;
007: import fitnesse.html.HtmlUtil;
008: import fitnesse.html.RawHtml;
009:
010: import java.util.Random;
011: import java.util.regex.Matcher;
012: import java.util.regex.Pattern;
013:
014: public class CollapsableWidget extends ParentWidget {
015: private static final String ENDL = LineBreakWidget.REGEXP;
016: public static final String REGEXP = "!\\*+>? .*?" + ENDL + ".*?"
017: + ENDL + "\\*+!" + ENDL + "?";
018: private static final Pattern pattern = Pattern.compile(
019: "!\\*+(>)? (.*?)" + ENDL + "(.*?)" + ENDL + "\\*+!",
020: Pattern.MULTILINE + Pattern.DOTALL);
021:
022: private static Random random = new Random();
023:
024: private String cssClass = "collapse_rim";
025: private ParentWidget titleWidget;
026: public boolean expanded = true;
027:
028: private static final String collapsableOpenCss = "collapsable";
029: private static final String collapsableClosedCss = "hidden";
030: private static final String collapsableOpenImg = "/files/images/collapsableOpen.gif";
031: private static final String collapsableClosedImg = "/files/images/collapsableClosed.gif";
032: private static final String collapseAllLink = "<a href=\"javascript:collapseAll();\">Collapse All</a>";
033: private static final String expandAllLink = "<a href=\"javascript:expandAll();\">Expand All</a>";
034:
035: public CollapsableWidget(ParentWidget parent) {
036: super (parent);
037: }
038:
039: public CollapsableWidget(ParentWidget parent, String text)
040: throws Exception {
041: this (parent);
042: Matcher match = pattern.matcher(text);
043: match.find();
044: expanded = match.group(1) == null;
045: String title = match.group(2);
046: String body = match.group(3);
047: init(title, body);
048: }
049:
050: public CollapsableWidget(ParentWidget parent, String title,
051: String body, String cssClass, boolean collapsed)
052: throws Exception {
053: this (parent);
054: init(title, body);
055: this .cssClass = cssClass;
056: this .expanded = !collapsed;
057: }
058:
059: private void init(String title, String body) throws Exception {
060: titleWidget = new BlankParentWidget(this , "!meta " + title);
061: addChildWidgets(body);
062: }
063:
064: public String render() throws Exception {
065: HtmlElement titleElement = new RawHtml(" "
066: + titleWidget.childHtml());
067: HtmlElement bodyElement = new RawHtml(childHtml());
068: HtmlElement html = makeCollapsableSection(titleElement,
069: bodyElement);
070: return html.html();
071: }
072:
073: public HtmlTag makeCollapsableSection(HtmlElement title,
074: HtmlElement content) {
075: String id = random.nextLong() + "";
076: HtmlTag outerDiv = HtmlUtil.makeDivTag(cssClass);
077:
078: HtmlTag image = new HtmlTag("img");
079: image.addAttribute("src", imageSrc());
080: image.addAttribute("class", "left");
081: image.addAttribute("id", "img" + id);
082:
083: HtmlTag anchor = new HtmlTag("a", image);
084: anchor.addAttribute("href", "javascript:toggleCollapsable('"
085: + id + "');");
086:
087: HtmlTag links = new HtmlTag("div");
088: links.addAttribute("style", "float: right;");
089: links.addAttribute("class", "meta");
090: links.add(expandAllLink + " | " + collapseAllLink);
091:
092: outerDiv.add(links);
093: outerDiv.add(anchor);
094: outerDiv.add(title);
095:
096: HtmlTag collapsablediv = makeCollapsableDiv();
097: collapsablediv.addAttribute("id", id);
098: collapsablediv.add(content);
099: outerDiv.add(collapsablediv);
100:
101: return outerDiv;
102: }
103:
104: private HtmlTag makeCollapsableDiv() {
105: if (!expanded)
106: return HtmlUtil.makeDivTag(collapsableClosedCss);
107: else
108: return HtmlUtil.makeDivTag(collapsableOpenCss);
109: }
110:
111: private String imageSrc() {
112: if (expanded)
113: return collapsableOpenImg;
114: else
115: return collapsableClosedImg;
116: }
117: }
|