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