KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > editors > htmlarea > CmsHtmlAreaWidget


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/editors/htmlarea/CmsHtmlAreaWidget.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.htmlarea;
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.galleries.A_CmsGallery;
45
46 import java.util.ArrayList JavaDoc;
47 import java.util.Collections JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Locale JavaDoc;
52 import java.util.Map JavaDoc;
53
54 /**
55  * Provides a widget that creates a rich input field using the "HtmlArea" component, for use on a widget dialog.<p>
56  *
57  * @author Alexander Kandzior
58  * @author Andreas Zahner
59  *
60  * @version $Revision: 1.3 $
61  *
62  * @since 6.0.0
63  */

64 public class CmsHtmlAreaWidget extends A_CmsHtmlWidget {
65
66     /** VFS path to the available HtmlArea Locales. */
67     public static final String JavaDoc HTMLAREA_LOCALES_VFS = CmsWorkplace.VFS_PATH_WORKPLACE
68         + "resources/editors/htmlarea/lang/";
69
70     /**
71      * Creates a new html area editor widget.<p>
72      */

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

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

94     public CmsHtmlAreaWidget(String JavaDoc configuration) {
95
96         super(configuration);
97     }
98
99     /**
100      * Returns the Locale for which HtmlArea has localization data available.<p>
101      *
102      * @param cms the initialized cmsobject
103      * @param wantedLocale the preferred Locale
104      * @return the Locale for which HtmlArea has localization data available
105      */

106     public static Locale JavaDoc getHtmlAreaLocale(CmsObject cms, Locale JavaDoc wantedLocale) {
107
108         // check the Locale to use for editor localization
109
if (!cms.existsResource(HTMLAREA_LOCALES_VFS + wantedLocale.toString() + ".js")) {
110             boolean foundLocale = false;
111             Iterator JavaDoc i = OpenCms.getLocaleManager().getDefaultLocales().iterator();
112             // check the available default Locales
113
while (i.hasNext()) {
114                 wantedLocale = (Locale JavaDoc)i.next();
115                 if (cms.existsResource(HTMLAREA_LOCALES_VFS + wantedLocale.toString() + ".js")) {
116                     // found a valid Locale
117
foundLocale = true;
118                     break;
119                 }
120             }
121             if (!foundLocale) {
122                 // did not find any matching Locale, fall back to english
123
wantedLocale = Locale.ENGLISH;
124             }
125         }
126         return wantedLocale;
127     }
128
129     /**
130      * @see org.opencms.widgets.I_CmsWidget#getDialogIncludes(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
131      */

132     public String JavaDoc getDialogIncludes(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
133
134         StringBuffer JavaDoc result = new StringBuffer JavaDoc(16);
135         result.append("<script type=\"text/javascript\">\n<!--\n");
136         result.append("\tvar _editor_url = \"");
137         result.append(CmsWorkplace.getSkinUri());
138         result.append("editors/htmlarea/\";\n");
139         result.append("\tvar _editor_lang = \"");
140         result.append(getHtmlAreaLocale(cms, widgetDialog.getLocale()));
141         result.append("\";\n");
142         result.append("//-->\n</script>\n");
143         // general HtmlArea JS
144
result.append(getJSIncludeFile(CmsWorkplace.getSkinUri() + "editors/htmlarea/htmlarea.js"));
145         result.append("\n");
146         // special functions required for the OpenCms dialogs
147
result.append(getJSIncludeFile(CmsWorkplace.getSkinUri() + "editors/htmlarea/htmlarea-ocms.js"));
148         result.append("\n");
149         // special HtmlArea widget functions
150
result.append(getJSIncludeFile(CmsWorkplace.getSkinUri() + "components/widgets/htmlarea.js"));
151         return result.toString();
152     }
153
154     /**
155      * @see org.opencms.widgets.I_CmsWidget#getDialogInitCall(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
156      */

157     public String JavaDoc getDialogInitCall(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
158
159         // the timeout setting prevents IE from jumping to the last html area widget
160
return "\tinitHtmlArea();\n";
161     }
162
163     /**
164      * @see org.opencms.widgets.I_CmsWidget#getDialogInitMethod(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
165      */

166     public String JavaDoc getDialogInitMethod(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
167
168         StringBuffer JavaDoc result = new StringBuffer JavaDoc(8);
169         result.append("function initHtmlArea() {\n");
170         result.append("\tgenerateHtmlAreas();\n");
171         result.append("}\n\n");
172         // generate the special OpenCms buttons to use in the HtmlArea toolbar
173
result.append("function configHtmlAreaToolbar(currentConf) {\n");
174         result.append(buildOpenCmsButtons(widgetDialog));
175         result.append("}\n");
176         return result.toString();
177     }
178
179     /**
180      * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
181      */

182     public String JavaDoc getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
183
184         String JavaDoc id = param.getId();
185         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
186
187         result.append("<td class=\"xmlTd\">");
188
189         result.append("<textarea class=\"xmlInput maxwidth\" name=\"");
190         result.append(id);
191         result.append("\" id=\"");
192         result.append(id);
193         result.append("\" style=\"height: ");
194         result.append(getHtmlWidgetOption().getEditorHeight());
195         result.append(";\" rows=\"20\" cols=\"60\">");
196         result.append(CmsEncoder.escapeXml(param.getStringValue(cms)));
197         result.append("</textarea>");
198
199         // generate the special configuration object for the current editor widget
200
result.append("<script type=\"text/javascript\">\n<!--\n");
201         result.append("\tconfig = getHtmlAreaConfiguration();\n");
202
203         // set CSS style sheet for current editor widget if configured
204
if (getHtmlWidgetOption().useCss()) {
205             result.append("config.pageStyle = \"@import url(");
206             result.append(OpenCms.getLinkManager().substituteLink(cms, getHtmlWidgetOption().getCssPath()));
207             result.append(");\";\n");
208             getHtmlWidgetOption().setCssPath(null);
209         }
210
211         result.append("\tconfigHtmlAreaToolbar(config);\n");
212         // generate the special toolbar object
213
result.append(buildOpenCmsButtonRow());
214         result.append("\n\thtmlAreaConfigs[\"");
215         result.append(id);
216         result.append("\"] = config;\n");
217         result.append("//-->\n</script>\n");
218
219         result.append("</td>");
220
221         return result.toString();
222     }
223
224     /**
225      * @see org.opencms.widgets.I_CmsWidget#newInstance()
226      */

227     public I_CmsWidget newInstance() {
228
229         return new CmsHtmlAreaWidget(getHtmlWidgetOption());
230     }
231
232     /**
233      * Returns the configuration String for the gallery button row in HtmlArea.<p>
234      *
235      * @return the html String for the gallery buttons
236      */

237     protected String JavaDoc buildOpenCmsButtonRow() {
238
239         StringBuffer JavaDoc result = new StringBuffer JavaDoc(16);
240
241         result.append("\tconfig.toolbar = [\n");
242         result.append("\t\t[\n");
243         result.append("\t\t\t\"copy\", \"cut\", \"paste\", \"separator\",\n");
244         result.append("\t\t\t");
245         // show format block if configured
246
if (getHtmlWidgetOption().showFormatSelect()) {
247             result.append("\"formatblock\", \"space\", ");
248         }
249         result.append("\"bold\", \"italic\", \"underline\", \"separator\",\n");
250         result.append("\t\t\t\"strikethrough\", \"subscript\", \"superscript\", \"separator\",\n");
251         result.append("\t\t\t\"justifyleft\", \"justifycenter\", \"justifyright\", \"justifyfull\", \"separator\",\n");
252         result.append("\t\t\t\"insertorderedlist\", \"insertunorderedlist\", \"outdent\", \"indent\"");
253
254         // build the link buttons
255
boolean showLink = false;
256         StringBuffer JavaDoc custom = new StringBuffer JavaDoc(8);
257         if (getHtmlWidgetOption().showLinkDialog()) {
258             custom.append("\"oc-link\"");
259             showLink = true;
260         }
261         if (getHtmlWidgetOption().showAnchorDialog()) {
262             if (showLink) {
263                 custom.append(", ");
264             }
265             custom.append("\"oc-anchor\"");
266             showLink = true;
267         }
268         if (showLink) {
269             result.append(", \"separator\",\n\t\t\t");
270             result.append(custom);
271         }
272
273         // build the gallery button row
274
Map JavaDoc galleryMap = OpenCms.getWorkplaceManager().getGalleries();
275         List JavaDoc galleries = new ArrayList JavaDoc(galleryMap.size());
276         Map JavaDoc typeMap = new HashMap JavaDoc(galleryMap.size());
277
278         Iterator JavaDoc i = galleryMap.entrySet().iterator();
279         while (i.hasNext()) {
280             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
281             String JavaDoc key = (String JavaDoc)entry.getKey();
282             A_CmsGallery currGallery = (A_CmsGallery)entry.getValue();
283             galleries.add(currGallery);
284             // put the type name to the type Map
285
typeMap.put(currGallery, key);
286         }
287
288         // sort the found galleries by their order
289
Collections.sort(galleries);
290
291         StringBuffer JavaDoc galleryResult = new StringBuffer JavaDoc(8);
292         boolean showGallery = false;
293         for (int k = 0; k < galleries.size(); k++) {
294             A_CmsGallery currGallery = (A_CmsGallery)galleries.get(k);
295             String JavaDoc galleryType = (String JavaDoc)typeMap.get(currGallery);
296             if (getHtmlWidgetOption().showGalleryDialog(galleryType)) {
297                 // gallery is shown, build row configuration String
298
if (galleryResult.length() > 0) {
299                     galleryResult.append(", ");
300                 }
301                 galleryResult.append("\"");
302                 galleryResult.append(galleryType);
303                 galleryResult.append("\"");
304                 showGallery = true;
305             }
306         }
307
308         if (showGallery) {
309             result.append(", \"separator\",\n\t\t\t");
310             result.append(galleryResult);
311         }
312
313         // show image button if configured
314
if (getHtmlWidgetOption().showImageDialog()) {
315             result.append(", \"separator\",\n\t\t\t\"insertimage\"\n");
316         }
317
318         // show table button if configured
319
if (getHtmlWidgetOption().showTableDialog()) {
320             result.append(", \"separator\",\n\t\t\t\"inserttable\"\n");
321         }
322
323         // show source button
324
if (getHtmlWidgetOption().showSourceEditor()) {
325             result.append(", \"separator\",\n\t\t\t\"htmlmode\"\n");
326         }
327
328         result.append("\t\t]\n\t];");
329         return result.toString();
330     }
331
332     /**
333      * Returns the JavaScript to configure the OpenCms buttons for HtmlArea.<p>
334      *
335      * @param widgetDialog the dialog where the widget is used on
336      * @return the JavaScript to configure the OpenCms buttons for HtmlArea
337      */

338     protected String JavaDoc buildOpenCmsButtons(I_CmsWidgetDialog widgetDialog) {
339
340         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
341
342         // build the link button configuration
343
result.append("\tconfig.registerButton(\"");
344         result.append("oc-link");
345         result.append("\", \"");
346         result.append(widgetDialog.getMessages().key("button.linkto"));
347         result.append("\", _editor_url + \"../../buttons/link.png");
348         result.append("\", false, function(e) { setActiveEditor(e); openLinkDialog(\'");
349         result.append(widgetDialog.getMessages().key("editor.message.noselection"));
350         result.append("\'); });\n");
351
352         // build the anchor button configuration
353
result.append("\tconfig.registerButton(\"");
354         result.append("oc-anchor");
355         result.append("\", \"");
356         result.append(widgetDialog.getMessages().key("button.anchor"));
357         result.append("\", _editor_url + \"../../buttons/anchor.png");
358         result.append("\", false, function(e) { setActiveEditor(e); openAnchorDialog(\'");
359         result.append(widgetDialog.getMessages().key("editor.message.noselection"));
360         result.append("\'); });\n");
361
362         // build the gallery button configurations
363
Iterator JavaDoc i = OpenCms.getWorkplaceManager().getGalleries().keySet().iterator();
364         while (i.hasNext()) {
365             String JavaDoc galleryType = (String JavaDoc)i.next();
366             String JavaDoc galleryName = CmsStringUtil.substitute(galleryType, "gallery", "");
367             // create gallery button code
368
result.append("\tconfig.registerButton(\"");
369             result.append(galleryType);
370             result.append("\", \"");
371             result.append(widgetDialog.getMessages().key("button." + galleryName + "list"));
372             result.append("\", _editor_url + \"/images/opencms/");
373             result.append(galleryType);
374             result.append(".gif\", false, function(e) { setActiveEditor(e); openGallery(\'");
375             result.append(galleryType);
376             result.append("\'); });\n");
377         }
378
379         return result.toString();
380     }
381 }
Popular Tags