KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > widgets > CmsHtmlWidgetOption


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/widgets/CmsHtmlWidgetOption.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.widgets;
33
34 import org.opencms.main.OpenCms;
35 import org.opencms.util.CmsStringUtil;
36
37 import java.util.ArrayList JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40
41 /**
42  * An option of a Html type widget.<p>
43  *
44  * Options can be defined for each element of the type <code>OpenCmsHtml</code> using the widget <code>HtmlWidget</code>.
45  * They have to be placed in the annotation section of a XSD describing an xml content. The <code>configuration</code> attribute
46  * in the <code>layout</code> node for the element must contain the activated options as a comma separated String value:<p>
47  *
48  * <code><layout element="Text" widget="HtmlWidget" configuration="height:400px,link,anchor,imagegallery,downloadgallery,formatselect,source" /></code><p>
49  *
50  * Available options are:
51  * <ul>
52  * <li><code>anchor</code>: the anchor dialog button</li>
53  * <li><code>css:/vfs/path/to/cssfile.css</code>: the absolute path in the OpenCms VFS to the CSS style sheet
54  * to use to render the contents in the editor (availability depends on the integrated editor)</li>
55  * <li><code>formatselect</code>: the format selector for selecting text format like paragraph or headings</li>
56  * <li><code>${gallerytype}</code>: Shows a gallery dialog button, e.g. <code>imagegallery</code> displays
57  * the image gallery button or <code>downloadgallery</code> displays the download gallery button</li>
58  * <li><code>height:${editorheight}</code>: the editor height, where the height can be specified in px or %, e.g. <code>400px</code></li>
59  * <li><code>image</code>: the image dialog button (availability depends on the integrated editor)</li>
60  * <li><code>link</code>: the link dialog button</li>
61  * <li><code>source</code>: shows the source code toggle button(s)</li>
62  * <li><code>stylesxml:/vfs/path/to/stylefile.xml</code>: the absolute path in the OpenCms VFS to the user defined
63  * styles that should be displayed in the style selector (availability depends on the integrated editor)</li>
64  * <li><code>table</code>: the table dialog button (availability depends on the integrated editor)</li>
65  * </ul>
66  * If an option key is not found in the configuration options, the corresponding button will be hidden in the editor widget.<p>
67  *
68  * @author Andreas Zahner
69  *
70  * @version $Revision: 1.3 $
71  *
72  * @since 6.0.1
73  */

74 public class CmsHtmlWidgetOption {
75
76     /** The editor widget default height to use. */
77     public static final String JavaDoc EDITOR_DEFAULTHEIGHT = "260px";
78
79     /** Option for the "anchor" dialog. */
80     public static final String JavaDoc OPTION_ANCHOR = "anchor";
81
82     /** Option for the css style sheet VFS path to use in the widget area. */
83     public static final String JavaDoc OPTION_CSS = "css:";
84
85     /** The delimiter to use in the configuration String. */
86     public static final String JavaDoc OPTION_DELIMITER = ",";
87
88     /** Option for the "formatselect" selector. */
89     public static final String JavaDoc OPTION_FORMATSELECT = "formatselect";
90
91     /** Option for the "height" configuration. */
92     public static final String JavaDoc OPTION_HEIGHT = "height:";
93
94     /** Option for the "image" dialog. */
95     public static final String JavaDoc OPTION_IMAGE = "image";
96
97     /** Option for the "link" dialog. */
98     public static final String JavaDoc OPTION_LINK = "link";
99
100     /** Option for the "source" code mode. */
101     public static final String JavaDoc OPTION_SOURCE = "source";
102
103     /** Option for the styles XML VFS path to use in the widget area. */
104     public static final String JavaDoc OPTION_STYLES = "stylesxml:";
105
106     /** Option for the "table" dialog. */
107     public static final String JavaDoc OPTION_TABLE = "table";
108
109     private String JavaDoc m_cssPath;
110     private List JavaDoc m_displayGalleries;
111     private String JavaDoc m_editorHeight;
112     private boolean m_showAnchorDialog;
113     private boolean m_showFormatSelect;
114     private boolean m_showImageDialog;
115     private boolean m_showLinkDialog;
116     private boolean m_showSourceEditor;
117     private boolean m_showTableDialog;
118     private String JavaDoc m_stylesXmlPath;
119
120     /**
121      * Creates a new empty html widget object object.<p>
122      */

123     public CmsHtmlWidgetOption() {
124
125         // initialize the members
126
m_displayGalleries = new ArrayList JavaDoc();
127         m_editorHeight = EDITOR_DEFAULTHEIGHT;
128     }
129
130     /**
131      * Creates a new html widget object object, configured by the given configuration String.<p>
132      *
133      * @param configuration configuration String to parse
134      */

135     public CmsHtmlWidgetOption(String JavaDoc configuration) {
136
137         // initialize the widget options
138
m_displayGalleries = new ArrayList JavaDoc();
139         m_editorHeight = EDITOR_DEFAULTHEIGHT;
140         parseOptions(configuration);
141     }
142
143     /**
144      * Returns a html widget configuration String created from the given html widget option.<p>
145      *
146      * @param option the html widget options to create the configuration String for
147      *
148      * @return a select widget configuration String created from the given html widget option object
149      */

150     public static String JavaDoc createConfigurationString(CmsHtmlWidgetOption option) {
151
152         StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
153         boolean added = false;
154         if (!option.getEditorHeight().equals(EDITOR_DEFAULTHEIGHT)) {
155             // append the height configuration
156
result.append(OPTION_HEIGHT);
157             result.append(option.getEditorHeight());
158             added = true;
159         }
160         if (option.showAnchorDialog()) {
161             // append the anchor configuration
162
if (added) {
163                 result.append(OPTION_DELIMITER);
164             }
165             result.append(OPTION_ANCHOR);
166             added = true;
167         }
168         if (option.showLinkDialog()) {
169             // append the link configuration
170
if (added) {
171                 result.append(OPTION_DELIMITER);
172             }
173             result.append(OPTION_LINK);
174             added = true;
175         }
176         if (option.showFormatSelect()) {
177             // append the format selector configuration
178
if (added) {
179                 result.append(OPTION_DELIMITER);
180             }
181             result.append(OPTION_FORMATSELECT);
182             added = true;
183         }
184         if (option.showSourceEditor()) {
185             // append the source code configuration
186
if (added) {
187                 result.append(OPTION_DELIMITER);
188             }
189             result.append(OPTION_SOURCE);
190             added = true;
191         }
192         if (option.showTableDialog()) {
193             // append the table configuration
194
if (added) {
195                 result.append(OPTION_DELIMITER);
196             }
197             result.append(OPTION_TABLE);
198             added = true;
199         }
200         if (option.showImageDialog()) {
201             // append the image configuration
202
if (added) {
203                 result.append(OPTION_DELIMITER);
204             }
205             result.append(OPTION_IMAGE);
206             added = true;
207         }
208         if (option.useCss()) {
209             // append the CSS VFS path
210
if (added) {
211                 result.append(OPTION_DELIMITER);
212             }
213             result.append(OPTION_CSS);
214             result.append(option.getCssPath());
215             added = true;
216         }
217         if (option.showStylesXml()) {
218             // append the styles XML VFS path
219
if (added) {
220                 result.append(OPTION_DELIMITER);
221             }
222             result.append(OPTION_STYLES);
223             result.append(option.getStylesXmlPath());
224             added = true;
225         }
226
227         boolean isFirst = true;
228         for (int i = 0; i < option.getDisplayGalleries().size(); i++) {
229             // append the galleries configuration
230
String JavaDoc gallery = (String JavaDoc)option.getDisplayGalleries().get(i);
231             if (added || !isFirst) {
232                 result.append(OPTION_DELIMITER);
233             }
234             result.append(gallery);
235             isFirst = false;
236         }
237
238         return result.toString();
239     }
240
241     /**
242      * Returns the css style sheet VFS path to use in the widget area.<p>
243      *
244      * @return the css style sheet VFS path to use in the widget area
245      */

246     public String JavaDoc getCssPath() {
247
248         return m_cssPath;
249     }
250
251     /**
252      * Returns the displayed gallery names.<p>
253      *
254      * @return the displayed gallery names
255      */

256     public List JavaDoc getDisplayGalleries() {
257
258         return m_displayGalleries;
259     }
260
261     /**
262      * Returns the widget editor height.<p>
263      *
264      * @return the widget editor height
265      */

266     public String JavaDoc getEditorHeight() {
267
268         return m_editorHeight;
269     }
270
271     /**
272      * Returns the styles XML VFS path to use in the widget area.<p>
273      *
274      * @return the styles XML VFS path to use in the widget area
275      */

276     public String JavaDoc getStylesXmlPath() {
277
278         return m_stylesXmlPath;
279     }
280
281     /**
282      * Sets the css style sheet VFS path to use in the widget area.<p>
283      *
284      * @param cssPath the css style sheet VFS path to use in the widget area
285      */

286     public void setCssPath(String JavaDoc cssPath) {
287
288         m_cssPath = cssPath;
289     }
290
291     /**
292      * Sets the displayed gallery names.<p>
293      *
294      * @param displayGalleries the displayed gallery names
295      */

296     public void setDisplayGalleries(List JavaDoc displayGalleries) {
297
298         m_displayGalleries = displayGalleries;
299     }
300
301     /**
302      * Sets the widget editor height.<p>
303      *
304      * @param editorHeight the widget editor height
305      */

306     public void setEditorHeight(String JavaDoc editorHeight) {
307
308         m_editorHeight = editorHeight;
309     }
310
311     /**
312      * Sets if the anchor dialog button should be available.<p>
313      *
314      * @param showAnchorDialog true if the anchor dialog button should be available, otherwise false
315      */

316     public void setShowAnchorDialog(boolean showAnchorDialog) {
317
318         m_showAnchorDialog = showAnchorDialog;
319     }
320
321     /**
322      * Sets if the format selector should be available.<p>
323      *
324      * @param showFormatSelect true if the format selector should be available, otherwise false
325      */

326     public void setShowFormatSelect(boolean showFormatSelect) {
327
328         m_showFormatSelect = showFormatSelect;
329     }
330
331     /**
332      * Sets if the image dialog button should be available.<p>
333      *
334      * @param showImageDialog true if the image dialog button should be available, otherwise false
335      */

336     public void setShowImageDialog(boolean showImageDialog) {
337
338         m_showImageDialog = showImageDialog;
339     }
340
341     /**
342      * Sets if the link dialog button should be available.<p>
343      *
344      * @param showLinkDialog true if the link dialog button should be available, otherwise false
345      */

346     public void setShowLinkDialog(boolean showLinkDialog) {
347
348         m_showLinkDialog = showLinkDialog;
349     }
350
351     /**
352      * Sets if the source code button should be available.<p>
353      *
354      * @param showSourceEditor true if the source code button should be available, otherwise false
355      */

356     public void setShowSourceEditor(boolean showSourceEditor) {
357
358         m_showSourceEditor = showSourceEditor;
359     }
360
361     /**
362      * Sets if the table dialog button should be available.<p>
363      *
364      * @param showTableDialog true if the table dialog button should be available, otherwise false
365      */

366     public void setShowTableDialog(boolean showTableDialog) {
367
368         m_showTableDialog = showTableDialog;
369     }
370
371     /**
372      * Sets the styles XML VFS path to use in the widget area.<p>
373      *
374      * @param stylesXmlPath the styles XML VFS path to use in the widget area
375      */

376     public void setStylesXmlPath(String JavaDoc stylesXmlPath) {
377
378         m_stylesXmlPath = stylesXmlPath;
379     }
380
381     /**
382      * Returns true if the anchor dialog button should be available.<p>
383      *
384      * @return if the anchor dialog button should be available
385      */

386     public boolean showAnchorDialog() {
387
388         return m_showAnchorDialog;
389     }
390
391     /**
392      * Returns true if the format selector should be available.<p>
393      *
394      * @return if the format selector should be available
395      */

396     public boolean showFormatSelect() {
397
398         return m_showFormatSelect;
399     }
400
401     /**
402      * Returns true if the specified gallery type dialog button is shown.<p>
403      *
404      * @param galleryType the gallery type to check
405      * @return true if the specified gallery type dialog button is shown, otherwise false
406      */

407     public boolean showGalleryDialog(String JavaDoc galleryType) {
408
409         return getDisplayGalleries().contains(galleryType);
410     }
411
412     /**
413      * Returns true if the image dialog button should be available.<p>
414      *
415      * @return if the image dialog button should be available
416      */

417     public boolean showImageDialog() {
418
419         return m_showImageDialog;
420     }
421
422     /**
423      * Returns true if the link dialog button should be available.<p>
424      *
425      * @return if the link dialog button should be available
426      */

427     public boolean showLinkDialog() {
428
429         return m_showLinkDialog;
430     }
431
432     /**
433      * Returns true if the source code button should be available.<p>
434      *
435      * @return if the source code button should be available
436      */

437     public boolean showSourceEditor() {
438
439         return m_showSourceEditor;
440     }
441
442     /**
443      * Returns true if the styles selector should be available.<p>
444      *
445      * @return if the styles selector should be available
446      */

447     public boolean showStylesXml() {
448
449         return CmsStringUtil.isNotEmpty(getStylesXmlPath());
450     }
451
452     /**
453      * Returns true if the table dialog button should be available.<p>
454      *
455      * @return if the table dialog button should be available
456      */

457     public boolean showTableDialog() {
458
459         return m_showTableDialog;
460     }
461
462     /**
463      * Returns true if the widget editor should use a defined CSS style sheet.<p>
464      *
465      * @return if the widget editor should use a defined CSS style sheet
466      */

467     public boolean useCss() {
468
469         return CmsStringUtil.isNotEmpty(getCssPath());
470     }
471
472     /**
473      * Parses the given configuration String.<p>
474      *
475      * @param configuration the configuration String to parse
476      */

477     protected void parseOptions(String JavaDoc configuration) {
478
479         if (CmsStringUtil.isNotEmpty(configuration)) {
480             List JavaDoc options = CmsStringUtil.splitAsList(configuration, OPTION_DELIMITER, true);
481             Iterator JavaDoc i = options.iterator();
482             while (i.hasNext()) {
483                 String JavaDoc option = (String JavaDoc)i.next();
484                 if (OPTION_LINK.equals(option)) {
485                     // show link dialog
486
setShowLinkDialog(true);
487                 } else if (OPTION_ANCHOR.equals(option)) {
488                     // show anchor dialog
489
setShowAnchorDialog(true);
490                 } else if (OPTION_SOURCE.equals(option)) {
491                     // show source button
492
setShowSourceEditor(true);
493                 } else if (OPTION_FORMATSELECT.equals(option)) {
494                     // show format selector
495
setShowFormatSelect(true);
496                 } else if (OPTION_IMAGE.equals(option)) {
497                     // show image dialog
498
setShowImageDialog(true);
499                 } else if (OPTION_TABLE.equals(option)) {
500                     // show table dialog
501
setShowTableDialog(true);
502                 } else if (option.startsWith(OPTION_HEIGHT)) {
503                     // the editor height
504
option = option.substring(OPTION_HEIGHT.length());
505                     if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(option)) {
506                         setEditorHeight(option);
507                     }
508                 } else if (option.startsWith(OPTION_CSS)) {
509                     // the editor CSS
510
option = option.substring(OPTION_CSS.length());
511                     setCssPath(option);
512                 } else if (option.startsWith(OPTION_STYLES)) {
513                     // the editor styles XML path
514
option = option.substring(OPTION_STYLES.length());
515                     setStylesXmlPath(option);
516                 } else {
517                     // check if option describes a gallery
518
if (OpenCms.getWorkplaceManager().getGalleries().get(option) != null) {
519                         // add the option to the displayed galleries
520
m_displayGalleries.add(option);
521                     }
522                 }
523             }
524         }
525     }
526
527 }
Popular Tags