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.components.SaveRecorder;
006: import fitnesse.http.*;
007: import fitnesse.wiki.*;
008: import fitnesse.wikitext.*;
009: import fitnesse.html.*;
010: import fitnesse.*;
011: import fitnesse.responders.SecureResponder;
012: import fitnesse.authentication.*;
013: import java.util.*;
014:
015: public class EditResponder implements SecureResponder {
016: public static final String CONTENT_INPUT_NAME = "pageContent";
017: public static final String SAVE_ID = "saveId";
018: public static final String TICKET_ID = "ticketId";
019:
020: protected String content;
021: protected WikiPage page;
022: protected WikiPage root;
023: protected PageData pageData;
024: protected Request request;
025:
026: public EditResponder() {
027: }
028:
029: public Response makeResponse(FitNesseContext context,
030: Request request) throws Exception {
031: initializeResponder(context.root, request);
032:
033: SimpleResponse response = new SimpleResponse();
034: String resource = request.getResource();
035: WikiPagePath path = PathParser.parse(resource);
036: PageCrawler crawler = context.root.getPageCrawler();
037: if (!crawler.pageExists(root, path)) {
038: crawler.setDeadEndStrategy(new MockingPageCrawler());
039: page = crawler.getPage(root, path);
040: } else
041: page = crawler.getPage(root, path);
042:
043: pageData = page.getData();
044: content = createPageContent();
045:
046: String html = makeHtml(resource, context);
047:
048: response.setContent(html);
049: response.setMaxAge(0);
050:
051: return response;
052: }
053:
054: protected void initializeResponder(WikiPage root, Request request) {
055: this .root = root;
056: this .request = request;
057: }
058:
059: protected String createPageContent() throws Exception {
060: return pageData.getContent();
061: }
062:
063: public String makeHtml(String resource, FitNesseContext context)
064: throws Exception {
065: HtmlPage html = context.htmlPageFactory.newPage();
066: html.title.use("Edit " + resource);
067: html.body.addAttribute("onload", "document.f."
068: + CONTENT_INPUT_NAME + ".focus()");
069: html.header.use(HtmlUtil.makeBreadCrumbsWithPageType(resource,
070: "Edit Page"));
071: html.main.use(makeEditForm(resource));
072:
073: return html.html();
074: }
075:
076: public HtmlTag makeEditForm(String resource) throws Exception {
077: HtmlTag form = new HtmlTag("form");
078: form.addAttribute("name", "f");
079: form.addAttribute("action", resource);
080: form.addAttribute("method", "post");
081: form.add(HtmlUtil.makeInputTag("hidden", "responder",
082: "saveData"));
083: form.add(HtmlUtil.makeInputTag("hidden", SAVE_ID, String
084: .valueOf(SaveRecorder.newIdNumber())));
085: form.add(HtmlUtil.makeInputTag("hidden", TICKET_ID, String
086: .valueOf((SaveRecorder.newTicket()))));
087: if (request.hasInput("redirectToReferer")
088: && request.hasHeader("Referer")) {
089: String redirectUrl = request.getHeader("Referer")
090: .toString();
091: int questionMarkIndex = redirectUrl.indexOf("?");
092: if (questionMarkIndex > 0)
093: redirectUrl = redirectUrl.substring(0,
094: questionMarkIndex);
095: redirectUrl += "?"
096: + request.getInput("redirectAction").toString();
097: form.add(HtmlUtil.makeInputTag("hidden", "redirect",
098: redirectUrl));
099: }
100:
101: form.add(createTextarea());
102: form.add(createButtons());
103:
104: HtmlTag wizardForm = makeWizardForm(resource);
105:
106: TagGroup group = new TagGroup();
107: group.add(form);
108: group.add(wizardForm);
109:
110: return group;
111: }
112:
113: private HtmlTag makeWizardForm(String resource) {
114: HtmlTag wizardForm = new HtmlTag("form");
115: wizardForm.addAttribute("name", "tableWizardForm");
116: wizardForm.addAttribute("action", resource);
117: wizardForm.addAttribute("method", "post");
118: wizardForm.add(HtmlUtil.makeInputTag("hidden", "responder",
119: "tableWizard"));
120: wizardForm.add(HtmlUtil.makeInputTag("hidden", "text", ""));
121: wizardForm.add(HtmlUtil.makeInputTag("hidden", "fixture", ""));
122: return wizardForm;
123: }
124:
125: private HtmlTag createButtons() throws Exception {
126: HtmlTag buttons = HtmlUtil.makeDivTag("edit_buttons");
127: buttons.add(makeSaveButton());
128: buttons.add(makeScriptButtons());
129: buttons.add(makeWizardOptions());
130: return buttons;
131: }
132:
133: private HtmlTag makeWizardOptions() throws Exception {
134: HtmlTag wizardOptions = new HtmlTag("select");
135: wizardOptions.addAttribute("name", "fixtureTable");
136: wizardOptions.addAttribute("onchange", "addFixture()");
137: wizardOptions.add(HtmlUtil.makeOptionTag("default",
138: "- Insert Fixture Table -"));
139:
140: List fixtureNames = new FixtureListBuilder()
141: .getFixtureNames(this .page);
142: for (Iterator fixtures = fixtureNames.iterator(); fixtures
143: .hasNext();) {
144: String fixture = (String) fixtures.next();
145: wizardOptions.add(HtmlUtil.makeOptionTag(fixture, fixture));
146: }
147: return wizardOptions;
148: }
149:
150: private HtmlTag makeScriptButtons() {
151: TagGroup scripts = new TagGroup();
152:
153: includeJavaScriptFile(
154: "/files/javascript/SpreadsheetTranslator.js", scripts);
155: includeJavaScriptFile(
156: "/files/javascript/spreadsheetSupport.js", scripts);
157:
158: HtmlTag wizardScript = new HtmlTag("script");
159: wizardScript
160: .add("\nfunction addFixture()\n"
161: + "{\n"
162: + "\tdocument.tableWizardForm.text.value = document.f."
163: + CONTENT_INPUT_NAME
164: + ".value;\n"
165: + "\tdocument.tableWizardForm.fixture.value = document.f.fixtureTable.options[document.f.fixtureTable.selectedIndex].value;\n"
166: + "\tdocument.tableWizardForm.submit();\n"
167: + "}");
168: scripts.add(wizardScript);
169:
170: return scripts;
171: }
172:
173: private void includeJavaScriptFile(String jsFile, TagGroup scripts) {
174: HtmlTag scriptTag = HtmlUtil.makeJavascriptLink(jsFile);
175: scripts.add(scriptTag);
176: }
177:
178: private HtmlTag makeSaveButton() {
179: HtmlTag saveButton = HtmlUtil.makeInputTag("submit", "save",
180: "Save");
181: saveButton.addAttribute("tabindex", "2");
182: saveButton.addAttribute("accesskey", "s");
183: return saveButton;
184: }
185:
186: private HtmlTag createTextarea() {
187: HtmlTag textarea = new HtmlTag("textarea");
188: textarea.addAttribute("name", CONTENT_INPUT_NAME);
189: textarea.addAttribute("rows", "25");
190: textarea.addAttribute("cols", "70");
191: textarea.addAttribute("tabindex", "1");
192: textarea.add(Utils.escapeText(content));
193: return textarea;
194: }
195:
196: public SecureOperation getSecureOperation() {
197: return new SecureReadOperation();
198: }
199: }
|