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.responders.versions;
004:
005: import fitnesse.*;
006: import fitnesse.responders.NotFoundResponder;
007: import fitnesse.wiki.*;
008: import fitnesse.http.*;
009: import fitnesse.html.*;
010: import java.util.*;
011:
012: public class VersionSelectionResponder implements Responder {
013: private WikiPage page;
014:
015: private List versions;
016:
017: private List ageStrings;
018:
019: private PageData pageData;
020:
021: private String resource;
022:
023: public Response makeResponse(FitNesseContext context,
024: Request request) throws Exception {
025: SimpleResponse response = new SimpleResponse();
026: resource = request.getResource();
027: WikiPagePath path = PathParser.parse(resource);
028: page = context.root.getPageCrawler()
029: .getPage(context.root, path);
030: if (page == null)
031: return new NotFoundResponder().makeResponse(context,
032: request);
033:
034: pageData = page.getData();
035: versions = getVersionsList(pageData);
036: ageStrings = new ArrayList();
037: Date now = new GregorianCalendar().getTime();
038: for (Iterator iterator = versions.iterator(); iterator
039: .hasNext();) {
040: VersionInfo version = (VersionInfo) iterator.next();
041: ageStrings.add(howLongAgoString(now, version
042: .getCreationTime()));
043: }
044:
045: response.setContent(makeHtml(context));
046:
047: return response;
048: }
049:
050: public String makeHtml(FitNesseContext context) throws Exception {
051: HtmlPage html = context.htmlPageFactory.newPage();
052: html.title.use("Version Selection: " + resource);
053: html.header.use(HtmlUtil.makeBreadCrumbsWithPageType(resource,
054: "Version Selection"));
055: html.main.use(makeRightColumn());
056: return html.html();
057: }
058:
059: public HtmlTag makeRightColumn() throws Exception {
060: HtmlTag group = new TagGroup();
061: group.add(new HtmlTag("h3", "Select a version."));
062:
063: HtmlTag form = new HtmlTag("form");
064: String fullPathName = PathParser.render(page.getPageCrawler()
065: .getFullPath(page));
066: form.addAttribute("action", fullPathName);
067: form.addAttribute("method", "get");
068: form.add(HtmlUtil.makeInputTag("hidden", "responder",
069: "viewVersion"));
070:
071: HtmlTag table = new HtmlTag("table");
072: table.addAttribute("cellspacing", "0");
073: table.add(makeRow("th", new RawHtml(" "), "Name",
074: "Author", "Age"));
075:
076: for (int i = 0; i < versions.size(); i++)
077: table.add(makeVersionRow(i));
078:
079: form.add(table);
080: form.add(HtmlUtil
081: .makeInputTag("submit", "save", "View Version"));
082:
083: group.add(form);
084:
085: return group;
086: }
087:
088: private HtmlTag makeVersionRow(int index) {
089: VersionInfo version = (VersionInfo) versions.get(index);
090: HtmlTag input = HtmlUtil.makeInputTag("radio", "version",
091: version.getName());
092: return makeRow("td", input, version.getName(), version
093: .getAuthor(), ageStrings.get(index).toString());
094: }
095:
096: private HtmlTag makeRow(String cellType, HtmlElement input,
097: String name, String author, String age) {
098: HtmlTag row = new HtmlTag("tr");
099: row.add(new HtmlTag(cellType, input));
100: row.add(new HtmlTag(cellType, name));
101: row.add(new HtmlTag(cellType, author));
102: row.add(new HtmlTag(cellType, age));
103: return row;
104: }
105:
106: public static List getVersionsList(PageData data) {
107: List list = new ArrayList(data.getVersions());
108: Collections.sort(list);
109: Collections.reverse(list);
110: return list;
111: }
112:
113: public static String howLongAgoString(Date now, Date then) {
114: long time = Math.abs(now.getTime() - then.getTime()) / 1000;
115:
116: if (time < 60)
117: return pluralize(time, "second");
118: else if (time < 3600)
119: return pluralize(time / 60, "minute");
120: else if (time < 86400)
121: return pluralize(time / (3600), "hour");
122: else if (time < 31536000)
123: return pluralize(time / (86400), "day");
124: else
125: return pluralize(time / (31536000), "year");
126: }
127:
128: private static String pluralize(long time, String unit) {
129: String age = time + " " + unit;
130: if (time > 1)
131: age = age + "s";
132:
133: return age;
134: }
135:
136: }
|