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