KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > editors > fckeditor > CmsFCKEditorWidget


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/editors/fckeditor/CmsFCKEditorWidget.java,v $
3  * Date : $Date: 2006/10/25 09:55:59 $
4  * Version: $Revision: 1.3 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.editors.fckeditor;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.i18n.CmsEncoder;
36 import org.opencms.main.OpenCms;
37 import org.opencms.util.CmsStringUtil;
38 import org.opencms.widgets.A_CmsHtmlWidget;
39 import org.opencms.widgets.CmsHtmlWidgetOption;
40 import org.opencms.widgets.I_CmsWidget;
41 import org.opencms.widgets.I_CmsWidgetDialog;
42 import org.opencms.widgets.I_CmsWidgetParameter;
43 import org.opencms.workplace.CmsWorkplace;
44 import org.opencms.workplace.editors.CmsEditor;
45 import org.opencms.workplace.galleries.A_CmsGallery;
46
47 import java.util.ArrayList JavaDoc;
48 import java.util.Collections JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.Map JavaDoc;
53
54 /**
55  * Provides a widget that creates a rich input field using the "FCKeditor" component, for use on a widget dialog.<p>
56  *
57  * For configuration options, have a look at the documentation of {@link CmsHtmlWidgetOption}.<p>
58  *
59  * @author Andreas Zahner
60  *
61  * @version $Revision: 1.3 $
62  *
63  * @since 6.1.7
64  */

65 public class CmsFCKEditorWidget extends A_CmsHtmlWidget {
66
67     /** Request parameter name for the toolbar configuration parameter. */
68     public static final String JavaDoc PARAM_CONFIGURATION = "config";
69
70     /**
71      * Creates a new FCKeditor widget.<p>
72      */

73     public CmsFCKEditorWidget() {
74
75         // empty constructor is required for class registration
76
this("");
77     }
78
79     /**
80      * Creates a new FCKeditor widget with the given configuration.<p>
81      *
82      * @param configuration the configuration to use
83      */

84     public CmsFCKEditorWidget(CmsHtmlWidgetOption configuration) {
85
86         super(configuration);
87     }
88
89     /**
90      * Creates a new FCKeditor widget with the given configuration.<p>
91      *
92      * @param configuration the configuration to use
93      */

94     public CmsFCKEditorWidget(String JavaDoc configuration) {
95
96         super(configuration);
97     }
98
99     /**
100      * Builds the toolbar button configuration String for the OpenCms specific buttons of the editor widget.<p>
101      *
102      * @param toolbar the toolbar configuration defining the buttons to show
103      * @param widgetOptionsString the options String containing the button names to show
104      * @return true if at least one button was added to the toolbar, otherwise false
105      */

106     public static boolean buildOpenCmsButtonRow(StringBuffer JavaDoc toolbar, String JavaDoc widgetOptionsString) {
107
108         boolean buttonRendered = false;
109
110         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(widgetOptionsString)) {
111             // configuration String found, build buttons to show
112
CmsHtmlWidgetOption option = new CmsHtmlWidgetOption(widgetOptionsString);
113             StringBuffer JavaDoc custom = new StringBuffer JavaDoc(512);
114
115             // show source button if configured
116
if (option.showSourceEditor()) {
117                 custom.append("\"Source\"");
118                 buttonRendered = true;
119             }
120
121             // show format selector if configured
122
boolean showFormatSelect = false;
123             if (option.showFormatSelect()) {
124                 if (buttonRendered) {
125                     custom.append(",\"-\",");
126                 }
127                 custom.append("\"FontFormat\"");
128                 buttonRendered = true;
129                 showFormatSelect = true;
130             }
131
132             // show style selector if configured
133
if (option.showStylesXml()) {
134                 if (!showFormatSelect && buttonRendered) {
135                     custom.append(",\"-\",");
136                 } else if (buttonRendered) {
137                     custom.append(",");
138                 }
139                 custom.append("\"Style\"");
140                 buttonRendered = true;
141             }
142
143             // build the link and/or anchor buttons
144
boolean showLink = false;
145             if (option.showLinkDialog()) {
146                 if (buttonRendered) {
147                     custom.append(",");
148                 }
149                 custom.append("\"oc-link\"");
150                 buttonRendered = true;
151                 showLink = true;
152             }
153             if (option.showAnchorDialog()) {
154                 if (buttonRendered) {
155                     custom.append(",");
156                 }
157                 custom.append("\"Anchor\"");
158                 buttonRendered = true;
159                 showLink = true;
160             }
161             if (showLink) {
162                 // append the unlink button if at least one link button is configured
163
custom.append(", \"Unlink\"");
164             }
165
166             // build the gallery button row
167
Map JavaDoc galleryMap = OpenCms.getWorkplaceManager().getGalleries();
168             List JavaDoc galleries = new ArrayList JavaDoc(galleryMap.size());
169             Map JavaDoc typeMap = new HashMap JavaDoc(galleryMap.size());
170
171             Iterator JavaDoc i = galleryMap.entrySet().iterator();
172             while (i.hasNext()) {
173                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
174                 String JavaDoc key = (String JavaDoc)entry.getKey();
175                 A_CmsGallery currGallery = (A_CmsGallery)entry.getValue();
176                 galleries.add(currGallery);
177                 // put the type name to the type Map
178
typeMap.put(currGallery, key);
179             }
180
181             // sort the found galleries by their order
182
Collections.sort(galleries);
183
184             StringBuffer JavaDoc galleryResult = new StringBuffer JavaDoc(8);
185             boolean showGallery = false;
186             for (int k = 0; k < galleries.size(); k++) {
187                 A_CmsGallery currGallery = (A_CmsGallery)galleries.get(k);
188                 String JavaDoc galleryType = (String JavaDoc)typeMap.get(currGallery);
189                 if (option.getDisplayGalleries().contains(galleryType)) {
190                     // gallery is shown, build row configuration String
191
if (galleryResult.length() > 0) {
192                         galleryResult.append(", ");
193                     }
194                     galleryResult.append("\"oc-");
195                     galleryResult.append(galleryType);
196                     galleryResult.append("\"");
197                     showGallery = true;
198                 }
199             }
200
201             if (showGallery) {
202                 // show the galleries
203
if (buttonRendered) {
204                     custom.append("],[");
205                 }
206                 custom.append(galleryResult);
207                 buttonRendered = true;
208             }
209
210             // show image button if configured
211
if (option.showImageDialog()) {
212                 if (buttonRendered) {
213                     custom.append(",\"-\",");
214                 }
215                 custom.append("\"Image\"");
216                 buttonRendered = true;
217             }
218
219             // show table button if configured
220
if (option.showTableDialog()) {
221                 if (buttonRendered) {
222                     custom.append(",\"-\",");
223                 }
224                 custom.append("\"Table\"");
225                 buttonRendered = true;
226             }
227
228             if (buttonRendered) {
229                 // insert grouping bracket if at least one button was rendered
230
custom.insert(0, ",[");
231                 // append custom buttons to toolbar
232
toolbar.append(custom);
233             }
234
235         }
236
237         return buttonRendered;
238     }
239
240     /**
241      * Returns if the given button is configured to be shown.<p>
242      *
243      * @param buttonName the name of the button to check
244      * @param widgetOptions the options List containing the button names to show
245      * @return true if the given button is configured to be shown
246      */

247     protected static boolean showButton(String JavaDoc buttonName, List JavaDoc widgetOptions) {
248
249         return (widgetOptions.contains(buttonName));
250     }
251
252     /**
253      * @see org.opencms.widgets.I_CmsWidget#getDialogIncludes(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
254      */

255     public String JavaDoc getDialogIncludes(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
256
257         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
258         // general FCKeditor JS
259
result.append(getJSIncludeFile(CmsWorkplace.getSkinUri() + "editors/fckeditor/fckeditor.js"));
260         result.append("\n");
261         // special FCKeditor widget functions
262
result.append(getJSIncludeFile(CmsWorkplace.getSkinUri() + "components/widgets/fckeditor.js"));
263         return result.toString();
264     }
265
266     /**
267      * @see org.opencms.widgets.I_CmsWidget#getDialogInitCall(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
268      */

269     public String JavaDoc getDialogInitCall(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
270
271         // creates the FCKeditor instances
272
return "\tinitFCKeditor();\n";
273     }
274
275     /**
276      * @see org.opencms.widgets.I_CmsWidget#getDialogInitMethod(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
277      */

278     public String JavaDoc getDialogInitMethod(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
279
280         StringBuffer JavaDoc result = new StringBuffer JavaDoc(64);
281         result.append("function initFCKeditor() {\n");
282         // set time out for IE to avoid toolbar error message on direct publish button click
283
result.append("\tif (navigator.userAgent.toLowerCase().indexOf(\"msie\") != -1) {\n");
284         result.append("\t\tsetTimeout(\"generateEditors();\", 50);\n");
285         result.append("\t} else {");
286         result.append("\t\tgenerateEditors();\n");
287         result.append("\t}\n");
288         result.append("}\n");
289         return result.toString();
290     }
291
292     /**
293      * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
294      */

295     public String JavaDoc getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
296
297         String JavaDoc id = param.getId();
298         String JavaDoc value = param.getStringValue(cms);
299         StringBuffer JavaDoc result = new StringBuffer JavaDoc(4096);
300
301         result.append("<td class=\"xmlTd\">");
302
303         result.append("<textarea class=\"xmlInput maxwidth\" name=\"ta_");
304         result.append(id);
305         result.append("\" id=\"ta_");
306         result.append(id);
307         result.append("\" style=\"height: ");
308         result.append(getHtmlWidgetOption().getEditorHeight());
309         result.append(";\" rows=\"20\" cols=\"60\">");
310         result.append(CmsEncoder.escapeXml(value));
311         result.append("</textarea>");
312         result.append("<input type=\"hidden\" name=\"");
313         result.append(id);
314         result.append("\" id=\"");
315         result.append(id);
316         result.append("\" value=\"");
317         result.append(CmsEncoder.encode(value));
318         result.append("\">");
319
320         // generate the special configuration object for the current editor widget
321
result.append("<script type=\"text/javascript\">\n");
322         result.append("var editor = new FCKeditor(\"ta_").append(id).append("\");\n");
323         result.append("editor.BasePath = \"").append(CmsWorkplace.getSkinUri()).append("editors/fckeditor/\";\n");
324
325         // set CSS style sheet for current editor widget if configured
326
if (getHtmlWidgetOption().useCss()) {
327             result.append("editor.Config[\"EditorAreaCSS\"] = \"");
328             result.append(OpenCms.getLinkManager().substituteLink(cms, getHtmlWidgetOption().getCssPath()));
329             result.append("\";\n");
330             // set the css path to null (the created config String passed to JS will not include this path then)
331
getHtmlWidgetOption().setCssPath(null);
332         }
333
334         // set styles XML for current editor widget if configured
335
if (getHtmlWidgetOption().showStylesXml()) {
336             result.append("editor.Config[\"StylesXmlPath\"] = \"");
337             result.append(OpenCms.getLinkManager().substituteLink(cms, getHtmlWidgetOption().getStylesXmlPath()));
338             result.append("\";\n");
339             // set the styles XML path to a value that the JS will create the selector
340
getHtmlWidgetOption().setStylesXmlPath("true");
341         }
342
343         result.append("editor.Width = \"100%\";\n");
344         result.append("editor.Height = \"").append(getHtmlWidgetOption().getEditorHeight()).append("\";\n");
345         result.append("editor.ToolbarSet = \"OpenCmsWidget\";\n");
346
347         // generate the special configuration JS call for the current dialog widget
348
StringBuffer JavaDoc configJs = new StringBuffer JavaDoc(128);
349         configJs.append(CmsEditor.PATH_EDITORS);
350         configJs.append("fckeditor/configwidget.js");
351         configJs.append("?");
352         configJs.append(PARAM_CONFIGURATION);
353         configJs.append("=");
354         configJs.append(CmsHtmlWidgetOption.createConfigurationString(getHtmlWidgetOption()));
355         result.append("editor.Config[\"CustomConfigurationsPath\"] = \"");
356         result.append(OpenCms.getLinkManager().substituteLink(cms, configJs.toString()));
357         result.append("\";\n");
358         result.append("editorInstances[editorInstances.length] = editor;\n");
359         result.append("contentFields[contentFields.length] = document.getElementById(\"").append(id).append("\");\n");
360         result.append("</script>\n");
361
362         result.append("</td>");
363
364         return result.toString();
365     }
366
367     /**
368      * @see org.opencms.widgets.I_CmsWidget#newInstance()
369      */

370     public I_CmsWidget newInstance() {
371
372         return new CmsFCKEditorWidget(getHtmlWidgetOption());
373     }
374
375 }
Popular Tags