KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > fredck > FCKeditor


1 /*
2  * FCKeditor - The text editor for internet
3  * Copyright (C) 2003-2005 Frederico Caldeira Knabben
4  *
5  * Licensed under the terms of the GNU Lesser General Public License:
6  * http://www.opensource.org/licenses/lgpl-license.php
7  *
8  * For further information visit:
9  * http://www.fckeditor.net/
10  *
11  * File Name: FCKeditor.java
12  * FCKeditor control class.
13  *
14  * Version: 2.3
15  * Modified: 2005-08-11 16:29:00
16  *
17  * File Authors:
18  * Simone Chiaretta (simo@users.sourceforge.net)
19  */

20  
21  
22 package com.fredck.FCKeditor;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26 /**
27  * The main class of the class lib.<br>
28  * It's the container for all properties and the class that generate the output based on browser capabilities and configurations passed by the developer.
29  *
30  * @author Simone Chiaretta (simo@users.sourceforge.net)
31  */

32 public class FCKeditor {
33     
34     private FCKeditorConfigurations oConfig;
35     private String JavaDoc instanceName;
36     private String JavaDoc value = "";
37     private String JavaDoc basePath;
38     private String JavaDoc toolbarSet = "Default";
39     private String JavaDoc width = "100%";
40     private String JavaDoc height = "200";
41     
42     HttpServletRequest JavaDoc request;
43     
44     /**
45      * Get the unique name of the editor
46      *
47      * @return name
48      */

49     public String JavaDoc getInstanceName() {
50         return instanceName;
51     }
52
53     /**
54      * Set the unique name of the editor
55      *
56      * @param value name
57      */

58     public void setInstanceName(String JavaDoc value) {
59         instanceName=value;
60     }
61
62     /**
63      * Get the initial value to be edited.<br>
64      * In HTML code
65      *
66      * @return value
67      */

68     public String JavaDoc getValue() {
69         return value;
70     }
71     
72     /**
73      * Set the initial value to be edited.<br>
74      * In HTML code
75      *
76      * @param value value
77      */

78     public void setValue(String JavaDoc value) {
79         this.value=value;
80     }
81
82     /**
83      * Get the dir where the FCKeditor files reside on the server
84      *
85      * @return path
86      */

87     public String JavaDoc getBasePath() {
88         return basePath;
89     }
90     
91     /**
92      * Set the dir where the FCKeditor files reside on the server.<br>
93      *<b>Remarks</b>:<br>
94      *Avoid using relative paths. It is preferable to set the base path starting from the root (/).<br>
95      *Always finish the path with a slash (/).
96      *
97      * @param value path
98      */

99     public void setBasePath(String JavaDoc value) {
100         basePath=value;
101     }
102
103     /**
104      * Get the name of the toolbar to display
105      *
106      * @return toolbar name
107      */

108     public String JavaDoc getToolbarSet() {
109         return toolbarSet;
110     }
111
112     /**
113      * Set the name of the toolbar to display
114      *
115      * @param value toolbar name
116      */

117     public void setToolbarSet(String JavaDoc value) {
118         toolbarSet=value;
119     }
120
121     /**
122      * Get the width of the textarea
123      *
124      * @return width
125      */

126     public String JavaDoc getWidth() {
127         return width;
128     }
129
130     /**
131      * Set the width of the textarea
132      *
133      * @param value width
134      */

135     public void setWidth(String JavaDoc value) {
136         width=value;
137     }
138
139     /**
140      * Get the height of the textarea
141      *
142      * @return height
143      */

144     public String JavaDoc getHeight() {
145         return height;
146     }
147
148     /**
149      * Set the height of the textarea
150      *
151      * @param value height
152      */

153     public void setHeight(String JavaDoc value) {
154         height=value;
155     }
156
157
158     /**
159      * Get the advanced configuation set.<br>
160      * Adding element to this collection you can override the settings specified in the config.js file.
161      *
162      * @return configuration collection
163      */

164     public FCKeditorConfigurations getConfig() {
165         return oConfig;
166     }
167
168     /**
169      * Set the advanced configuation set.
170      *
171      * @param value configuration collection
172      */

173     public void setConfig(FCKeditorConfigurations value) {
174         oConfig=value;
175     }
176
177     /**
178      * Initialize the object setting all value to the default ones.
179      * <p>
180      * <ul>
181      * <li>width: 100%</li>
182      * <li>height: 200</li>
183      * <li>toolbar name: Default</li>
184      * <li>basePath: context root + "/FCKeditor/"</li>
185      * </ul>
186      * </p>
187      *
188      * @param req request object
189      */

190     public FCKeditor(HttpServletRequest JavaDoc req){
191         request=req;
192         basePath = request.getContextPath() + "/FCKeditor/";
193         oConfig = new FCKeditorConfigurations() ;
194     }
195
196     /**
197      * Initialize the object setting the unique name and then all value to the default ones.
198      * <p>
199      * <ul>
200      * <li>width: 100%</li>
201      * <li>height: 200</li>
202      * <li>toolbar name: Default</li>
203      * <li>basePath: context root + "/FCKeditor/"</li>
204      * </ul>
205      * </p>
206      *
207      * @param req request object
208      * @param parInstanceName unique name
209      */

210     public FCKeditor(HttpServletRequest JavaDoc req, String JavaDoc parInstanceName){
211         request=req;
212         basePath = request.getContextPath() + "/FCKeditor/";
213         instanceName=parInstanceName;
214         oConfig = new FCKeditorConfigurations() ;
215     }
216
217     /**
218      * Initialize the object setting all basic configurations.<br>
219      *
220      * The basePath is context root + "/FCKeditor/"
221      *
222      * @param req request object
223      * @param parInstanceName unique name
224      * @param parWidth width
225      * @param parHeight height
226      * @param parToolbarSet toolbarSet name
227      * @param parValue initial value
228      */

229     public FCKeditor(HttpServletRequest JavaDoc req, String JavaDoc parInstanceName, String JavaDoc parWidth, String JavaDoc parHeight, String JavaDoc parToolbarSet, String JavaDoc parValue){
230         request=req;
231         basePath = request.getContextPath() + "/FCKeditor/";
232         instanceName=parInstanceName;
233         width=parWidth;
234         height=parHeight;
235         toolbarSet=parToolbarSet;
236         value=parValue;
237         oConfig = new FCKeditorConfigurations() ;
238     }
239     
240     
241     private boolean isCompatible() {
242         String JavaDoc userAgent=request.getHeader("user-agent");
243         if(userAgent==null)
244             return false;
245         userAgent=userAgent.toLowerCase();
246         if ((userAgent.indexOf("msie") !=-1) && (userAgent.indexOf("mac") == -1) && (userAgent.indexOf("opera") == -1)) {
247             if(retrieveBrowserVersion(userAgent)>=5.5)
248                 return true;
249         }
250         else if (userAgent.indexOf("gecko") !=-1){
251             if(retrieveBrowserVersion(userAgent)>=20030210)
252                 return true;
253         }
254         return false;
255     }
256     
257     private double retrieveBrowserVersion(String JavaDoc userAgent) {
258         if(userAgent.indexOf("msie")>-1) {
259             String JavaDoc str = userAgent.substring(userAgent.indexOf("msie") + 5);
260             return Double.parseDouble(str.substring(0, str.indexOf(";")));
261         }
262         else {
263             String JavaDoc str = userAgent.substring(userAgent.indexOf("gecko") + 6);
264             return Double.parseDouble(str.substring(0, 8));
265         }
266     }
267     
268     private String JavaDoc HTMLEncode(String JavaDoc txt) {
269         txt=txt.replaceAll("&","&amp;");
270         txt=txt.replaceAll("<","&lt;");
271         txt=txt.replaceAll(">","&gt;");
272         txt=txt.replaceAll("\"","&quot;");
273         txt=txt.replaceAll("'","&#146;");
274         return txt;
275     }
276     
277
278     /**
279      * Generate the HTML Code for the editor.
280      * <br>
281      * Evalute the browser capabilities and generate the editor if IE 5.5 or Gecko 20030210 or greater,
282      * or a simple textarea otherwise.
283      *
284      * @return html code
285      */

286     public String JavaDoc create() {
287         StringBuffer JavaDoc strEditor=new StringBuffer JavaDoc();
288         
289         strEditor.append("<div>");
290         String JavaDoc encodedValue=HTMLEncode(value);
291         
292         if(isCompatible()) {
293         
294             strEditor.append("<input type=\"hidden\" id=\"" + instanceName + "\" name=\"" + instanceName + "\" value=\"" + encodedValue + "\">");
295         
296             strEditor.append(createConfigHTML());
297             strEditor.append(createIFrameHTML());
298         
299         }
300         else{
301             strEditor.append("<TEXTAREA name=\"" + instanceName + "\" rows=\"4\" cols=\"40\" style=\"WIDTH: " + width + "; HEIGHT: " + height + "\" wrap=\"virtual\">"+encodedValue+"</TEXTAREA>");
302         }
303         strEditor.append("</div>");
304         return strEditor.toString();
305     }
306     
307     private String JavaDoc createConfigHTML() {
308         String JavaDoc configStr=oConfig.getUrlParams();
309         
310         
311         if(!configStr.equals(""))
312             configStr=configStr.substring(1);
313             
314         return "<input type=\"hidden\" id=\"" + instanceName + "___Config\" value=\"" + configStr + "\">" ;
315     }
316
317     private String JavaDoc createIFrameHTML() {
318     
319         String JavaDoc sLink=basePath + "editor/fckeditor.html?InstanceName=" + instanceName;
320         
321         if (!toolbarSet.equals(""))
322             sLink+="&Toolbar=" + toolbarSet;
323         
324          return "<iframe id=\"" + instanceName + "___Frame\" SRC=\"" + sLink + "\" width=\"" + width + "\" height=\"" + height + "\" frameborder=\"no\" scrolling=\"no\"></iframe>";
325         
326     }
327     
328     
329 }
330
Popular Tags