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.editing;
004:
005: import fitnesse.wikitext.Utils;
006: import fitnesse.wiki.*;
007: import fitnesse.http.*;
008: import fitnesse.components.SaveRecorder;
009: import fitnesse.*;
010: import fitnesse.html.*;
011:
012: public class MergeResponder implements Responder {
013: private Request request;
014:
015: private String newContent;
016:
017: private String existingContent;
018:
019: private String resource;
020:
021: public MergeResponder(Request request) {
022: this .request = request;
023: }
024:
025: public Response makeResponse(FitNesseContext context,
026: Request request) throws Exception {
027: SimpleResponse response = new SimpleResponse();
028: resource = this .request.getResource();
029: WikiPagePath path = PathParser.parse(resource);
030: WikiPage page = context.root.getPageCrawler().getPage(
031: context.root, path);
032: existingContent = page.getData().getContent();
033: newContent = (String) this .request
034: .getInput(EditResponder.CONTENT_INPUT_NAME);
035:
036: response.setContent(makePageHtml(context));
037:
038: return response;
039: }
040:
041: private String makePageHtml(FitNesseContext context)
042: throws Exception {
043: HtmlPage page = context.htmlPageFactory.newPage();
044: page.title.use("Merge " + resource);
045: page.header.use(HtmlUtil.makeBreadCrumbsWithPageType(resource,
046: "Merge Changes"));
047: page.main.use(makeRightColumn());
048: return page.html();
049: }
050:
051: private String makeRightColumn() throws Exception {
052: HtmlTag form = HtmlUtil.makeFormTag("post", resource);
053: form.add(HtmlUtil.makeInputTag("hidden", "responder",
054: "saveData"));
055: form.add(HtmlUtil.makeInputTag("hidden", EditResponder.SAVE_ID,
056: String.valueOf(SaveRecorder.newIdNumber())));
057: form.add(HtmlUtil.makeInputTag("hidden",
058: EditResponder.TICKET_ID, String.valueOf(SaveRecorder
059: .newTicket())));
060: HtmlTag title = HtmlUtil.makeDivTag("centered");
061: title
062: .use("This page has been recently modified. You may want to merge existing page content into your changes.");
063: form.add(title);
064: form.add(makeMergeNewDivTag());
065: form.add(makeMergeOldDivTag());
066: form.add(addHiddenAttributes());
067: return form.html();
068: }
069:
070: private HtmlTag makeMergeOldDivTag() {
071: HtmlTag mergeOld = HtmlUtil.makeDivTag("merge_old");
072: mergeOld.add("Existing Content (read only)");
073: mergeOld.add(HtmlUtil.BR);
074: mergeOld.add(makeOldContentTextArea());
075:
076: return mergeOld;
077: }
078:
079: private HtmlTag makeOldContentTextArea() {
080: HtmlTag oldContentTextArea = new HtmlTag("textarea");
081: oldContentTextArea.addAttribute("name", "oldContent");
082: oldContentTextArea.addAttribute("rows", "25");
083: oldContentTextArea.addAttribute("cols", "50");
084: oldContentTextArea.addAttribute("readonly", "");
085: oldContentTextArea.add(Utils.escapeText(existingContent));
086: return oldContentTextArea;
087: }
088:
089: private HtmlTag makeMergeNewDivTag() {
090: HtmlTag mergeNew = HtmlUtil.makeDivTag("merge_new");
091: mergeNew.add("Your Changes");
092: mergeNew.add(HtmlUtil.BR);
093: mergeNew.add(makeContentTextArea());
094: mergeNew.add(makeInputTagWithAccessKey());
095: return mergeNew;
096: }
097:
098: private HtmlTag makeInputTagWithAccessKey() {
099: HtmlTag input = HtmlUtil.makeInputTag("submit", "submit",
100: "Save");
101: input.addAttribute("accesskey", "s");
102: return input;
103: }
104:
105: private HtmlTag makeContentTextArea() {
106: HtmlTag contentTextArea = new HtmlTag("textarea");
107: contentTextArea.addAttribute("name",
108: EditResponder.CONTENT_INPUT_NAME);
109: contentTextArea.addAttribute("rows", "25");
110: contentTextArea.addAttribute("cols", "50");
111: contentTextArea.add(newContent);
112: return contentTextArea;
113: }
114:
115: private String addHiddenAttributes() {
116: StringBuffer buffer = new StringBuffer();
117: for (int i = 0; i < WikiPage.NON_SECURITY_ATTRIBUTES.length; i++) {
118: String attribute = WikiPage.NON_SECURITY_ATTRIBUTES[i];
119: if (request.hasInput(attribute))
120: buffer.append("<input type=\"hidden\" name=\""
121: + attribute + "\" value=\"On\">");
122: }
123: return buffer.toString();
124: }
125: }
|