001: /*
002: * (C) Janne Jalkanen 2005
003: *
004: */
005: package com.ecyrd.jspwiki.tags;
006:
007: import org.apache.commons.lang.StringEscapeUtils;
008: import org.apache.ecs.ConcreteElement;
009: import org.apache.ecs.xhtml.br;
010: import org.apache.ecs.xhtml.div;
011: import org.apache.ecs.xhtml.h3;
012: import org.apache.ecs.xhtml.noscript;
013: import org.apache.ecs.xhtml.script;
014: import org.apache.ecs.xhtml.textarea;
015:
016: import com.ecyrd.jspwiki.NoSuchVariableException;
017: import com.ecyrd.jspwiki.TranslatorReader;
018: import com.ecyrd.jspwiki.WikiContext;
019: import com.ecyrd.jspwiki.WikiEngine;
020:
021: import com.sun.portal.app.wiki.WikiPortletContext;
022:
023: /**
024: * @author jalkanen
025: *
026: * @since
027: */
028: public class EditorAreaTag extends WikiTagBase {
029: public static final String PROP_EDITORTYPE = "jspwiki.editor";
030:
031: public static final String EDITOR_PLAIN = "Plain";
032: public static final String EDITOR_FCK = "FCK";
033:
034: public static final String AREA_NAME = "text"; // TODO: Change
035:
036: public int doWikiStartTag() throws Exception {
037: pageContext.getOut().print(
038: getEditorArea(m_wikiContext).toString());
039:
040: return SKIP_BODY;
041: }
042:
043: private static ConcreteElement getFCKEditorArea(WikiContext context) {
044: WikiEngine engine = context.getEngine();
045:
046: // FIXME: Should this change the properties?
047: context.setVariable(WikiEngine.PROP_RUNFILTERS, "false");
048: context.setVariable(TranslatorReader.PROP_RUNPLUGINS, "false");
049: String pageAsHtml = StringEscapeUtils.escapeJavaScript(engine
050: .textToHTML(context, getText(context)));
051:
052: div container = new div();
053: script area = new script();
054:
055: area.setType("text/javascript");
056:
057: area
058: .addElement("var oFCKeditor = new FCKeditor( 'htmlPageText' );");
059: area.addElement("oFCKeditor.BasePath = 'scripts/fckeditor/';");
060: area.addElement("oFCKeditor.Value = '" + pageAsHtml + "' ;");
061: area.addElement("oFCKeditor.Width = '100%';");
062: area.addElement("oFCKeditor.Height = '500';");
063: area.addElement("oFCKeditor.ToolbarSet = 'JSPWiki';");
064: area
065: .addElement("oFCKeditor.Config['CustomConfigurationsPath'] = '"
066: + context.getEngine().getURL(WikiContext.NONE,
067: "scripts/fckconfig.js", null, true)
068: + "';");
069: area.addElement("oFCKeditor.Create() ;");
070:
071: noscript noscriptarea = new noscript();
072:
073: noscriptarea.addElement(new br());
074: noscriptarea.addElement(new h3().addElement(
075: context.translate("editorarea.need_javascript"))
076: .setStyle("previewnote"));
077: noscriptarea.addElement(new br());
078:
079: container.addElement(area);
080: container.addElement(noscriptarea);
081:
082: area.setPrettyPrint(true);
083: container.setPrettyPrint(true);
084: return container;
085: }
086:
087: private static String getText(WikiContext context) {
088: String usertext = null;
089:
090: if (context.getRequestContext().equals(WikiContext.EDIT)) {
091: usertext = context.getHttpParameter(AREA_NAME);
092: if (usertext == null) {
093: usertext = context.getEngine().getText(context,
094: context.getPage());
095: }
096: } else if (context.getRequestContext().equals(
097: WikiContext.COMMENT)) {
098: usertext = context.getHttpParameter(AREA_NAME);
099: }
100:
101: return usertext;
102: }
103:
104: /**
105: * Returns an element for constructing an editor.
106: *
107: * @param context Current WikiContext
108: * @return
109: */
110: public static ConcreteElement getEditorArea(WikiContext context) {
111: try {
112: String editor = context.getEngine().getVariableManager()
113: .getValue(context, PROP_EDITORTYPE);
114:
115: if (EDITOR_FCK.equals(editor))
116: return getFCKEditorArea(context);
117: } catch (NoSuchVariableException e) {
118: } // This is fine
119:
120: return getPlainEditorArea(context);
121: }
122:
123: private static ConcreteElement getPlainEditorArea(
124: WikiContext context) {
125: textarea area = new textarea();
126:
127: area.setClass("editor");
128: area.setWrap("virtual");
129: area.setName(AREA_NAME);
130: area.setRows(25);
131: area.setCols(80);
132: area.setStyle("width:100%;");
133: area.setID(context.getNamespace() + "_editorarea"); // FIXME: Should really be settable in case there are several
134:
135: String text = getText(context);
136: if (text != null)
137: area.addElement(text);
138:
139: return area;
140: }
141: }
|