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.refactoring;
004:
005: import java.util.*;
006:
007: import fitnesse.*;
008: import fitnesse.responders.SecureResponder;
009: import fitnesse.util.CollectionsUtil;
010: import fitnesse.wikitext.widgets.ContentsTreeWidget;
011: import fitnesse.authentication.*;
012: import fitnesse.html.*;
013: import fitnesse.http.*;
014:
015: /** Create a new page.
016: */
017: public class NewStiqPageResponder implements SecureResponder {
018:
019: public static final String RESPONDER_KEY = "newStiqPage";
020: public static final String STIQ_PAGE_NAME_INPUT_NAME = "stiqPageName";
021: public static final String STIQ_PAGE_TYPE_SELECT_NAME = "newStiqPageType";
022:
023: // FIXME: this cries out to be put in an enumeration.
024: public static final String STIQ_PAGE_TYPE_TEST = "Test";
025: public static final String STIQ_PAGE_TYPE_TEST_SUITE = "Test Suite";
026: public static final String STIQ_PAGE_TYPE_COMPONENT = "Component";
027: public static final String STIQ_PAGE_TYPE_COMPONENT_SUITE = "Component Suite";
028: public static final String STIQ_PAGE_TYPE_CONTAINER = "Container";
029: public static final String STIQ_PAGE_TYPE_TAG_SUITE = "Tag Suite";
030:
031: public static final Map TEST_TYPES_ICONS = Collections
032: .unmodifiableMap(CollectionsUtil.createMap(new Object[] {
033: STIQ_PAGE_TYPE_TEST,
034: ContentsTreeWidget.STIQ_TEST_IMAGE,
035: STIQ_PAGE_TYPE_TEST_SUITE,
036: ContentsTreeWidget.STIQ_SUITE_IMAGE,
037: STIQ_PAGE_TYPE_COMPONENT,
038: ContentsTreeWidget.STIQ_COMPONENT_IMAGE,
039: STIQ_PAGE_TYPE_COMPONENT_SUITE,
040: ContentsTreeWidget.STIQ_SUITE_IMAGE,
041: STIQ_PAGE_TYPE_CONTAINER,
042: ContentsTreeWidget.STIQ_CONTAINER_IMAGE,
043: STIQ_PAGE_TYPE_TAG_SUITE,
044: ContentsTreeWidget.STIQ_SUITE_IMAGE, }));
045:
046: public static final List STIQ_PAGE_TYPES = Collections
047: .unmodifiableList(CollectionsUtil
048: .createList(new Object[] { STIQ_PAGE_TYPE_TEST,
049: STIQ_PAGE_TYPE_TEST_SUITE,
050: STIQ_PAGE_TYPE_COMPONENT,
051: STIQ_PAGE_TYPE_COMPONENT_SUITE,
052: STIQ_PAGE_TYPE_CONTAINER,
053: STIQ_PAGE_TYPE_TAG_SUITE, }));
054:
055: private String resource;
056:
057: public Response makeResponse(FitNesseContext context,
058: Request request) throws Exception {
059: resource = request.getResource();
060: SimpleResponse response = new SimpleResponse();
061: response.setContent(html(context));
062: return response;
063: }
064:
065: public String html(FitNesseContext context) throws Exception {
066: HtmlPage html = context.htmlPageFactory.newPage();
067: html.title.use("New STIQ Page in " + resource);
068: html.header.use(HtmlUtil.makeBreadCrumbsWithPageType(resource,
069: "New STIQ Page"));
070: html.main.use(mainContent());
071: return html.html();
072: }
073:
074: private HtmlTag mainContent() throws Exception {
075: TagGroup group = new TagGroup();
076: group.add(createPageForm());
077: return group;
078: }
079:
080: private HtmlTag makeHeaderTag(String content) throws Exception {
081: return new HtmlTag("h3", content);
082: }
083:
084: private HtmlTag createPageForm() throws Exception {
085: TagGroup group = new TagGroup();
086: group.add(makeHeaderTag("New Page:"));
087: group.add("A child page will be created.");
088: group.add(makeCreatePageForm());
089: return group;
090: }
091:
092: private HtmlTag makeCreatePageForm() throws Exception {
093: HtmlTag form = HtmlUtil.makeFormTag("get", resource);
094: form.add(HtmlUtil.makeInputTag("hidden", "responder",
095: "createStiqPage"));
096: form.add(HtmlUtil.P);
097: form.add(" Name: ");
098: form.add(HtmlUtil.makeInputTag("text",
099: STIQ_PAGE_NAME_INPUT_NAME, ""));
100: form.add(HtmlUtil.BR);
101:
102: for (int i = 0, len = STIQ_PAGE_TYPES.size(); i < len; ++i) {
103: String testType = (String) STIQ_PAGE_TYPES.get(i);
104: form.add(HtmlUtil.P);
105: HtmlTag button = HtmlUtil.makeRadioTag(
106: STIQ_PAGE_TYPE_SELECT_NAME, testType, i == 0);
107: button.setAddNewlineAfter(false);
108: form.add(button + " ");
109:
110: HtmlTag img = new HtmlTag("img");
111: img.addAttribute("src", (String) TEST_TYPES_ICONS
112: .get(testType));
113: img.addAttribute("alt", testType);
114: img.addAttribute("width", String
115: .valueOf(ContentsTreeWidget.STIQ_ICON_WIDTH));
116: img.addAttribute("height", String
117: .valueOf(ContentsTreeWidget.STIQ_ICON_HEIGHT));
118: form.add(img);
119:
120: form.add(testType + "\n");
121: }
122:
123: form.add(HtmlUtil.P);
124: form.add(HtmlUtil.makeInputTag("submit", "create",
125: "Create Page"));
126: return form;
127: }
128:
129: public SecureOperation getSecureOperation() {
130: return new AlwaysSecureOperation();
131: }
132: }
|