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.*;
006: import fitnesse.authentication.*;
007: import fitnesse.html.*;
008: import fitnesse.responders.*;
009: import fitnesse.wiki.*;
010: import fitnesse.http.*;
011: import java.util.*;
012:
013: public class PropertiesResponder implements SecureResponder {
014: private WikiPage page;
015:
016: public PageData pageData;
017:
018: private String resource;
019:
020: public Response makeResponse(FitNesseContext context,
021: Request request) throws Exception {
022: SimpleResponse response = new SimpleResponse();
023: this .resource = request.getResource();
024: WikiPagePath path = PathParser.parse(this .resource);
025: PageCrawler crawler = context.root.getPageCrawler();
026: if (crawler.pageExists(context.root, path) == false) {
027: crawler.setDeadEndStrategy(new MockingPageCrawler());
028: this .page = crawler.getPage(context.root, path);
029: } else {
030: this .page = crawler.getPage(context.root, path);
031: }
032: if (this .page == null) {
033: return new NotFoundResponder().makeResponse(context,
034: request);
035: }
036:
037: this .pageData = this .page.getData();
038: String html = makeHtml(context);
039:
040: response.setContent(html);
041: response.setMaxAge(0);
042:
043: return response;
044: }
045:
046: private String makeHtml(FitNesseContext context) throws Exception {
047: HtmlPage page = context.htmlPageFactory.newPage();
048: page.title.use("Properties: " + this .resource);
049: page.header.use(HtmlUtil.makeBreadCrumbsWithPageType(
050: this .resource, "Page Properties"));
051: page.main.use(makeLastModifiedTag());
052: page.main.add(makeFormSections());
053:
054: return page.html();
055: }
056:
057: private HtmlTag makeAttributeCheckbox(String attribute,
058: PageData pageData) throws Exception {
059: HtmlTag checkbox = makeCheckbox(attribute);
060: if (pageData.hasAttribute(attribute)) {
061: checkbox.addAttribute("checked", "true");
062: }
063: return checkbox;
064: }
065:
066: private HtmlTag makeCheckbox(String attribute) {
067: HtmlTag checkbox = HtmlUtil.makeInputTag("checkbox", attribute);
068: checkbox.tail = " - " + attribute;
069: return checkbox;
070: }
071:
072: private HtmlTag makeLastModifiedTag() throws Exception {
073: HtmlTag tag = HtmlUtil.makeDivTag("right");
074: String username = this .pageData
075: .getAttribute(WikiPage.LAST_MODIFYING_USER);
076: if (username == null || "".equals(username)) {
077: tag.use("Last modified anonymously");
078: } else {
079: tag.use("Last modified by " + username);
080: }
081: return tag;
082: }
083:
084: private HtmlTag makeFormSections() throws Exception {
085: TagGroup html = new TagGroup();
086: html.add(makePropertiesForm());
087:
088: if (this .pageData.hasAttribute("WikiImportRoot")) {
089: html.add(makeImportUpdateForm(this .pageData
090: .getAttribute("WikiImportRoot"), true));
091: } else if (this .pageData.hasAttribute("WikiImportSource")) {
092: html.add(makeImportUpdateForm(this .pageData
093: .getAttribute("WikiImportSource"), false));
094: } else {
095: html.add(makeImportForm());
096: }
097:
098: html.add(makeSymbolicLinkSection());
099:
100: return html;
101: }
102:
103: private HtmlTag makePropertiesForm() throws Exception {
104: HtmlTag form = HtmlUtil.makeFormTag("post", this .resource);
105: form.add(HtmlUtil.makeInputTag("hidden", "responder",
106: "saveProperties"));
107:
108: HtmlTag trisection = new HtmlTag("div");
109: trisection.addAttribute("style", "height: 200px");
110: trisection.add(makeSTIQPageTypeCheckboxesHtml(this .pageData));
111: trisection.add(makeTestActionCheckboxesHtml(this .pageData));
112: trisection.add(makeNavigationCheckboxesHtml(this .pageData));
113: trisection.add(makeSecurityCheckboxesHtml(this .pageData));
114: trisection.add(makeVirtualWikiHtml());
115: form.add(trisection);
116:
117: HtmlTag saveButton = HtmlUtil.makeInputTag("submit", "Save",
118: "Save Properties");
119: saveButton.addAttribute("accesskey", "s");
120: form.add(HtmlUtil.BR);
121: form.add(saveButton);
122: return form;
123: }
124:
125: private HtmlTag makeVirtualWikiHtml() throws Exception {
126: HtmlTag virtualWiki = new HtmlTag("div");
127: virtualWiki.addAttribute("style", "float: left;");
128: virtualWiki.add("VirtualWiki URL: ");
129: HtmlTag deprecated = new HtmlTag("span", "(DEPRECATED)");
130: deprecated.addAttribute("style", "color: #FF0000;");
131: virtualWiki.add(deprecated);
132: virtualWiki.add(HtmlUtil.BR);
133: HtmlTag vwInput = HtmlUtil.makeInputTag("text", "VirtualWiki",
134: PropertiesResponder.getVirtualWikiValue(this .pageData));
135: vwInput.addAttribute("size", "40");
136: virtualWiki.add(vwInput);
137: return virtualWiki;
138: }
139:
140: private HtmlTag makeImportForm() {
141: HtmlTag form = HtmlUtil.makeFormTag("post", this .resource
142: + "#end");
143: form.add(HtmlUtil.HR);
144: form
145: .add("Wiki Import. Supply the URL for the wiki you'd like to import.");
146: form.add(HtmlUtil.BR);
147: form.add("Remote Wiki URL:");
148: HtmlTag remoteUrlField = HtmlUtil.makeInputTag("text",
149: "remoteUrl");
150: remoteUrlField.addAttribute("size", "40");
151: form.add(remoteUrlField);
152: form
153: .add(HtmlUtil.makeInputTag("hidden", "responder",
154: "import"));
155: form.add(HtmlUtil.makeInputTag("submit", "save", "Import"));
156: return form;
157: }
158:
159: private HtmlTag makeImportUpdateForm(String hostUrl, boolean isRoot)
160: throws Exception {
161: HtmlTag form = HtmlUtil.makeFormTag("post", this .resource
162: + "#end");
163: form.add(HtmlUtil.HR);
164: form.add("Wiki Import Update.");
165: form.add(HtmlUtil.BR);
166: String buttonMessage = "";
167: form.add(HtmlUtil.makeLink(this .page.getName(), this .page
168: .getName()));
169: if (isRoot) {
170: form.add(" imports its subpages from ");
171: buttonMessage = "Update Subpages";
172: } else {
173: form.add(" imports its content and subpages from ");
174: buttonMessage = "Update Content and Subpages";
175: }
176: form.add(HtmlUtil.makeLink(hostUrl, hostUrl));
177: form.add(".");
178: form.add(HtmlUtil.BR);
179: form
180: .add(HtmlUtil.makeInputTag("hidden", "responder",
181: "import"));
182: form
183: .add(HtmlUtil.makeInputTag("submit", "save",
184: buttonMessage));
185:
186: return form;
187: }
188:
189: private HtmlTag makeSymbolicLinkSection() throws Exception {
190: HtmlTag form = HtmlUtil.makeFormTag("get", this .resource);
191: form.add(HtmlUtil.HR);
192: form.add(HtmlUtil
193: .makeInputTag("hidden", "responder", "symlink"));
194: form.add("Symbolic Links");
195:
196: HtmlTableListingBuilder table = new HtmlTableListingBuilder();
197: table.addRow(new HtmlElement[] { new HtmlTag("strong", "Name"),
198: new HtmlTag("strong", "Path to Page"),
199: new HtmlTag("strong", "Action") });
200: addSymbolicLinkRows(table);
201: addFormRow(table);
202: form.add(table.getTable());
203:
204: return form;
205: }
206:
207: private void addFormRow(HtmlTableListingBuilder table)
208: throws Exception {
209: HtmlTag nameInput = HtmlUtil.makeInputTag("text", "linkName");
210: HtmlTag pathInput = HtmlUtil.makeInputTag("text", "linkPath");
211: pathInput.addAttribute("size", "40");
212: HtmlTag submitButton = HtmlUtil.makeInputTag("submit",
213: "submit", "Create Symbolic Link");
214: table.addRow(new HtmlElement[] { nameInput, pathInput,
215: submitButton });
216: }
217:
218: private void addSymbolicLinkRows(HtmlTableListingBuilder table)
219: throws Exception {
220: WikiPageProperties props = this .pageData.getProperties();
221: Set symbolicLinkNames = props.getSymbolicLinkNames();
222: for (Iterator iterator = symbolicLinkNames.iterator(); iterator
223: .hasNext();) {
224: String linkName = (String) iterator.next();
225: WikiPagePath path = props.getSymbolicLink(linkName);
226: HtmlElement nameItem = new RawHtml(linkName);
227: HtmlTag pathItem = HtmlUtil.makeLink(PathParser
228: .render(path), PathParser.render(path));
229: HtmlTag actionItem = HtmlUtil.makeLink(this .resource
230: + "?responder=symlink&removal=" + linkName,
231: "remove");
232: table.addRow(new HtmlElement[] { nameItem, pathItem,
233: actionItem });
234: }
235: }
236:
237: public static String getVirtualWikiValue(PageData data)
238: throws Exception {
239: String value = data
240: .getAttribute(WikiPageProperties.VIRTUAL_WIKI_ATTRIBUTE);
241: if (value == null) {
242: return "";
243: } else {
244: return value;
245: }
246: }
247:
248: public SecureOperation getSecureOperation() {
249: return new SecureReadOperation();
250: }
251:
252: public HtmlTag makeSTIQPageTypeCheckboxesHtml(PageData pageData)
253: throws Exception {
254: return makeAttributeCheckboxesHtml("STIQ Page Types:",
255: WikiPage.PAGE_TYPE_ATTRIBUTES, pageData);
256: }
257:
258: public HtmlTag makeTestActionCheckboxesHtml(PageData pageData)
259: throws Exception {
260: return makeAttributeCheckboxesHtml("Actions:",
261: WikiPage.ACTION_ATTRIBUTES, pageData);
262: }
263:
264: public HtmlElement makeNavigationCheckboxesHtml(PageData pageData)
265: throws Exception {
266: return makeAttributeCheckboxesHtml("Navigation:",
267: WikiPage.NAVIGATION_ATTRIBUTES, pageData);
268: }
269:
270: public HtmlTag makeSecurityCheckboxesHtml(PageData pageData)
271: throws Exception {
272: return makeAttributeCheckboxesHtml("Security:",
273: WikiPage.SECURITY_ATTRIBUTES, pageData);
274: }
275:
276: private HtmlTag makeAttributeCheckboxesHtml(String label,
277: String[] attributes, PageData pageData) throws Exception {
278: HtmlTag div = new HtmlTag("div");
279: div.addAttribute("style", "float: left; width: 150px;");
280:
281: div.add(label);
282: for (int i = 0; i < attributes.length; i++) {
283: String attribute = attributes[i];
284: div.add(HtmlUtil.BR);
285: div.add(makeAttributeCheckbox(attribute, pageData));
286: }
287: return div;
288: }
289:
290: }
|