KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > dialog > DialogFckEdit


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.gui.dialog;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.gui.control.ControlSuper;
17 import info.magnolia.cms.util.LinkUtil;
18
19 import java.io.IOException JavaDoc;
20 import java.io.Writer JavaDoc;
21
22 import javax.jcr.RepositoryException;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.log4j.Logger;
28
29
30 /**
31  * An Magnolia dialog for the universal usage and configuration of the fckeditor. Credits for FCKEditor:
32  * http://www.fckeditor.net/
33  * @author bert schulzki
34  * @author Fabrizio Giustina
35  * @version 1.0 11.10.2004
36  */

37 public class DialogFckEdit extends DialogBox {
38
39     /**
40      * Logger.
41      */

42     private static Logger log = Logger.getLogger(DialogFckEdit.class);
43
44     /**
45      * The new .BasePath of the editor
46      */

47     public static final String JavaDoc FCKEDIT_PATH = "/admindocroot/fckeditor/"; //$NON-NLS-1$
48

49     /**
50      * This parameter defines the startup script. This parameter is searched in the dialog configuration.
51      */

52     public static final String JavaDoc PARAM_JS_INIT_FILE = "jsInitFile"; //$NON-NLS-1$
53

54     /**
55      * This parameter defines the configuration script
56      */

57     public static final String JavaDoc PARAM_CUSTOM_CONFIGURATION_PATH = "customConfigurationPath"; //$NON-NLS-1$
58

59     /**
60      * If jsInitFile is not defined
61      */

62     public static final String JavaDoc PARAM_JS_INIT_FILE_DEFAULT = "/admindocroot/fckeditor/custom/init/magnoliaStandard.js"; //$NON-NLS-1$
63

64     /**
65      * If customConfigurationPath is not defined
66      */

67     public static final String JavaDoc PARAM_CUSTOM_CONFIGURATION_PATH_DEFAULT = "/admindocroot/fckeditor/custom/config/magnoliaStandard.js"; //$NON-NLS-1$
68

69     /**
70      * the configuration script name
71      */

72     private String JavaDoc customConfigurationsPath = PARAM_CUSTOM_CONFIGURATION_PATH_DEFAULT;
73
74     /**
75      * the initialization script name
76      */

77     private String JavaDoc jsInitFile = PARAM_JS_INIT_FILE_DEFAULT;
78
79     /**
80      * Empty constructor should only be used by DialogFactory.
81      */

82     protected DialogFckEdit() {
83     }
84
85     /**
86      * @return The name of the variable for the editor object
87      */

88     public String JavaDoc getVarName() {
89         String JavaDoc id = getId();
90         if (id == null) {
91             id = getName();
92         }
93         return "fck_" + id.replace('-', '_'); //$NON-NLS-1$
94
}
95
96     /**
97      * @param name script name
98      */

99     public void setCustomConfigurationPath(String JavaDoc name) {
100         if (name != null) {
101             customConfigurationsPath = name;
102         }
103     }
104
105     /**
106      * @param name init file
107      */

108     public void setJSInitFile(String JavaDoc name) {
109         if (name != null) {
110             jsInitFile = name;
111         }
112     }
113
114     /**
115      * @see info.magnolia.cms.gui.dialog.DialogInterface#init(HttpServletRequest, HttpServletResponse, Content, Content)
116      */

117     public void init(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Content websiteNode, Content configNode)
118         throws RepositoryException {
119         super.init(request, response, websiteNode, configNode);
120         String JavaDoc jsInitFile = this.getConfigValue(PARAM_JS_INIT_FILE, PARAM_JS_INIT_FILE_DEFAULT);
121         String JavaDoc customConfigurationPath = this.getConfigValue(
122             PARAM_CUSTOM_CONFIGURATION_PATH,
123             PARAM_CUSTOM_CONFIGURATION_PATH_DEFAULT);
124         this.setJSInitFile(jsInitFile);
125         this.setCustomConfigurationPath(customConfigurationPath);
126     }
127
128     /**
129      * @see info.magnolia.cms.gui.dialog.DialogInterface#drawHtml(Writer)
130      */

131     public void drawHtml(Writer JavaDoc out) throws IOException JavaDoc {
132         this.drawHtmlPre(out);
133
134         // load the script onece: if there are multiple instances
135
if (getRequest().getAttribute("__fcked_loaded") == null) { //$NON-NLS-1$
136
out.write("<script type=\"text/javascript\" SRC=\"" //$NON-NLS-1$
137
+ this.getRequest().getContextPath() + "/admindocroot/fckeditor/fckeditor.js\"></script>"); //$NON-NLS-1$
138
getRequest().setAttribute("__fcked_loaded", "true"); //$NON-NLS-1$ //$NON-NLS-2$
139
}
140
141         String JavaDoc id = getName();
142
143         if (id == null) {
144             log.error("Missing id for fckEditor instance"); //$NON-NLS-1$
145
}
146
147         String JavaDoc var = getVarName();
148         String JavaDoc value = convertToView(getValue());
149         value = LinkUtil.convertUUIDsToAbsoluteLinks(value);
150         out.write("<script type=\"text/javascript\">"); //$NON-NLS-1$
151
out.write("var " + var + " = null;"); //$NON-NLS-1$ //$NON-NLS-2$
152
out.write("fckInstance = new FCKeditor( '" + id + "' );"); //$NON-NLS-1$ //$NON-NLS-2$
153
out.write("fckInstance.Value = '" + escapeJsValue(value) + "';"); //$NON-NLS-1$ //$NON-NLS-2$
154
out.write("fckInstance.BasePath = '" + this.getRequest().getContextPath() + FCKEDIT_PATH + "';"); //$NON-NLS-1$ //$NON-NLS-2$
155
if (customConfigurationsPath.length() > 0) {
156             out.write("fckInstance.Config['CustomConfigurationsPath'] = '" //$NON-NLS-1$
157
+ this.getRequest().getContextPath() + customConfigurationsPath + "';"); //$NON-NLS-1$
158
}
159         if (jsInitFile.length() > 0) {
160             out.write("</script>"); //$NON-NLS-1$
161
out.write("<script type=\"text/javascript\" SRC=\"" //$NON-NLS-1$
162
+ this.getRequest().getContextPath() + jsInitFile + "\"></script>\n"); //$NON-NLS-1$
163
out.write("<script type=\"text/javascript\">"); //$NON-NLS-1$
164
}
165         out.write("fckInstance.Create();"); //$NON-NLS-1$
166
out.write(var + " = fckInstance;"); //$NON-NLS-1$
167
out.write("</script>"); //$NON-NLS-1$
168

169         // write the saveInfo for the writting back to the repository
170
out.write("<input type='hidden' name='mgnlSaveInfo' value='" //$NON-NLS-1$
171
+ id + ",String," //$NON-NLS-1$
172
+ ControlSuper.VALUETYPE_SINGLE + "," //$NON-NLS-1$
173
+ ControlSuper.RICHEDIT_FCK + "," //$NON-NLS-1$
174
+ ControlSuper.ENCODING_NO + "' />"); //$NON-NLS-1$
175

176         this.drawHtmlPost(out);
177
178         // out.write("</td>");
179
// out.write("</tr>");
180
}
181
182     /**
183      * @param value
184      * @return
185      */

186     private static String JavaDoc convertToView(String JavaDoc value) {
187         String JavaDoc tmp = value;
188         if (tmp != null) {
189             tmp = tmp.replaceAll("\r\n", "<br />"); //$NON-NLS-1$ //$NON-NLS-2$
190
tmp = tmp.replaceAll("\n", "<br />"); //$NON-NLS-1$ //$NON-NLS-2$
191
return tmp;
192         }
193         return StringUtils.EMPTY;
194     }
195
196     /**
197      * Replacements:
198      *
199      * <pre>
200      * ' -> \'
201      * " -> \"
202      * \r\n -> \\r\\n
203      * \n -> \\n
204      * \ -> \\
205      * </pre>
206      *
207      * @param src
208      * @return escaped js String
209      */

210     public static String JavaDoc escapeJsValue(String JavaDoc src) {
211         if (src == null) {
212             return null;
213         }
214         String JavaDoc escapedSrc = src.replaceAll("'", "\\\\'"); //$NON-NLS-1$ //$NON-NLS-2$
215
escapedSrc = escapedSrc.replaceAll("\"", "\\\""); //$NON-NLS-1$ //$NON-NLS-2$
216
escapedSrc = escapedSrc.replaceAll("\\r\\n", "\\\\r\\\\n"); //$NON-NLS-1$ //$NON-NLS-2$
217
escapedSrc = escapedSrc.replaceAll("\\n", "\\\\n"); //$NON-NLS-1$ //$NON-NLS-2$
218

219         return escapedSrc;
220     }
221 }
Popular Tags