001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2004 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.tags;
021:
022: import java.io.IOException;
023:
024: import javax.servlet.jsp.JspException;
025: import javax.servlet.jsp.PageContext;
026: import javax.servlet.jsp.tagext.BodyContent;
027:
028: import com.ecyrd.jspwiki.WikiContext;
029: import com.ecyrd.jspwiki.WikiEngine;
030: import com.ecyrd.jspwiki.WikiPage;
031:
032: import org.apache.ecs.xhtml.*;
033: import org.apache.ecs.GenericElement;
034: import org.apache.ecs.ConcreteElement;
035:
036: /**
037: * Creates an editor component with all the necessary parts
038: * to get it working.
039: * <p>
040: * In the future, this component should be expanded to provide
041: * a customized version of the editor according to user preferences.
042: *
043: * @author Janne Jalkanen
044: * @since 2.2
045: */
046: public class EditorTag extends WikiBodyTag {
047: private String m_submit = "Save";
048: private String m_preview = "Preview";
049: private String m_cancel = "Cancel";
050: private String m_formName = "editForm";
051: private String m_action = null;
052:
053: public void setSubmit(String s) {
054: m_submit = s;
055: }
056:
057: public void setPreview(String s) {
058: m_preview = s;
059: }
060:
061: public void setCancel(String s) {
062: m_cancel = s;
063: }
064:
065: public void setName(String s) {
066: m_formName = s;
067: }
068:
069: public void setAction(String s) {
070: m_action = s;
071: }
072:
073: private GenericElement createInput(String name, String value) {
074: input in = new input();
075: in.setType("hidden");
076: in.setName(name);
077: in.setValue(value);
078:
079: return in;
080: }
081:
082: private GenericElement createSubmit(String name, String value) {
083: input in = new input();
084: in.setType("submit");
085: in.setName(name);
086: in.setValue(value);
087:
088: return in;
089: }
090:
091: private form createSimpleEditor() {
092: WikiPage page = m_wikiContext.getPage();
093: WikiEngine engine = m_wikiContext.getEngine();
094:
095: form f = new form();
096: f.setName(m_formName);
097:
098: if (m_action != null) {
099: f.setAction(m_action);
100: } else if (m_wikiContext.getRequestContext().equals(
101: WikiContext.COMMENT)
102: || "comment".equals(m_wikiContext
103: .getHttpParameter("action"))) {
104: f.setAction(m_wikiContext.getURL(WikiContext.COMMENT, page
105: .getName()));
106: } else {
107: f.setAction(m_wikiContext.getURL(WikiContext.EDIT, page
108: .getName()));
109: }
110:
111: f.setMethod("post");
112: f.setAcceptCharset(engine.getContentEncoding());
113:
114: p para1 = new p();
115:
116: // Kludge to get preview working from Comment.jsp
117: if (m_wikiContext.getHttpParameter("author") != null) {
118: para1.addElement(createInput("author", m_wikiContext
119: .getHttpParameter("author")));
120: }
121:
122: f.addElement(para1);
123:
124: para1.addElement(createInput("page", page.getName()));
125: para1.addElement(createInput("wiki.action", "save"));
126: para1
127: .addElement(createInput("edittime",
128: (String) pageContext.getAttribute("lastchange",
129: PageContext.REQUEST_SCOPE)));
130:
131: if (m_wikiContext.getRequestContext().equals("comment")) {
132: para1.addElement(createInput("comment", "true"));
133: }
134:
135: return f;
136: }
137:
138: private ConcreteElement getEditorArea() {
139: return EditorAreaTag.getEditorArea(m_wikiContext);
140: }
141:
142: /**
143: * Returns an edit button block.
144: *
145: * @return
146: */
147: private ConcreteElement getButtons() {
148: p para2 = new p();
149:
150: para2.addElement(createSubmit("ok", m_wikiContext
151: .translate(m_submit)));
152: para2
153: .addElement(" ");
154: para2.addElement(createSubmit("preview", m_wikiContext
155: .translate(m_preview)));
156: para2
157: .addElement(" ");
158: para2.addElement(createSubmit("cancel", m_wikiContext
159: .translate(m_cancel)));
160:
161: return para2;
162: }
163:
164: public final int doWikiStartTag() throws IOException {
165: return EVAL_BODY_TAG;
166: }
167:
168: public int doEndTag() throws JspException {
169: BodyContent bc = getBodyContent();
170:
171: form editor = createSimpleEditor();
172:
173: //
174: // If there is no body tag content, then we'll assume old
175: // behaviour and append the stuff ourselves.
176: //
177: if (bc == null || bc.getString().length() == 0) {
178: editor.addElement(getEditorArea());
179:
180: editor.addElement(getButtons());
181: } else {
182: editor.addElement(bc.getString());
183: }
184:
185: try {
186: pageContext.getOut().print(editor.toString());
187: } catch (IOException e) {
188: throw new JspException("Could not print Editor tag: "
189: + e.getMessage());
190: }
191:
192: return EVAL_PAGE;
193: }
194: }
|