KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/widgets/CmsHtmlWidget.java,v $
3  * Date : $Date: 2006/03/27 14:52:20 $
4  * Version: $Revision: 1.2 $
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.file.CmsObject;
35 import org.opencms.i18n.CmsEncoder;
36 import org.opencms.main.CmsLog;
37 import org.opencms.main.OpenCms;
38 import org.opencms.util.CmsStringUtil;
39
40 import java.util.Map JavaDoc;
41
42 import org.apache.commons.logging.Log;
43
44 /**
45  * Provides a widget that creates a rich input field using the matching component, for use on a widget dialog.<p>
46  *
47  * The matching component is determined by checking the installed editors for the best matching component to use.<p>
48  *
49  * @author Andreas Zahner
50  *
51  * @version $Revision: 1.2 $
52  *
53  * @since 6.0.1
54  */

55 public class CmsHtmlWidget extends A_CmsHtmlWidget {
56
57     /** The log object for this class. */
58     private static final Log LOG = CmsLog.getLog(CmsHtmlWidget.class);
59
60     /** The editor widget to use depending on the current users settings, current browser and installed editors. */
61     private I_CmsWidget m_editorWidget;
62
63     /**
64      * Creates a new html editing widget.<p>
65      */

66     public CmsHtmlWidget() {
67
68         // empty constructor is required for class registration
69
this("");
70     }
71
72     /**
73      * Creates a new html editing widget with the given configuration.<p>
74      *
75      * @param configuration the configuration to use
76      */

77     public CmsHtmlWidget(CmsHtmlWidgetOption configuration) {
78
79         super(configuration);
80     }
81
82     /**
83      * Creates a new html editing widget with the given configuration.<p>
84      *
85      * @param configuration the configuration to use
86      */

87     public CmsHtmlWidget(String JavaDoc configuration) {
88
89         super(configuration);
90     }
91
92     /**
93      * @see org.opencms.widgets.I_CmsWidget#getDialogIncludes(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
94      */

95     public String JavaDoc getDialogIncludes(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
96
97         return getEditorWidget(cms, widgetDialog).getDialogIncludes(cms, widgetDialog);
98     }
99
100     /**
101      * @see org.opencms.widgets.I_CmsWidget#getDialogInitCall(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
102      */

103     public String JavaDoc getDialogInitCall(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
104
105         return getEditorWidget(cms, widgetDialog).getDialogInitCall(cms, widgetDialog);
106     }
107
108     /**
109      * @see org.opencms.widgets.I_CmsWidget#getDialogInitMethod(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
110      */

111     public String JavaDoc getDialogInitMethod(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
112
113         return getEditorWidget(cms, widgetDialog).getDialogInitMethod(cms, widgetDialog);
114     }
115
116     /**
117      * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
118      */

119     public String JavaDoc getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
120
121         return getEditorWidget(cms, widgetDialog).getDialogWidget(cms, widgetDialog, param);
122     }
123
124     /**
125      * @see org.opencms.widgets.I_CmsWidget#newInstance()
126      */

127     public I_CmsWidget newInstance() {
128
129         return new CmsHtmlWidget(getConfiguration());
130     }
131
132     /**
133      * @see org.opencms.widgets.I_CmsWidget#setEditorValue(org.opencms.file.CmsObject, java.util.Map, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
134      */

135     public void setEditorValue(
136         CmsObject cms,
137         Map JavaDoc formParameters,
138         I_CmsWidgetDialog widgetDialog,
139         I_CmsWidgetParameter param) {
140
141         String JavaDoc[] values = (String JavaDoc[])formParameters.get(param.getId());
142         if ((values != null) && (values.length > 0)) {
143             String JavaDoc val = CmsEncoder.decode(values[0], CmsEncoder.ENCODING_UTF_8);
144             param.setStringValue(cms, val);
145         }
146     }
147
148     /**
149      * Returns the editor widget to use depending on the current users settings, current browser and installed editors.<p>
150      *
151      * @param cms the current CmsObject
152      * @param widgetDialog the dialog where the widget is used on
153      * @return the editor widget to use depending on the current users settings, current browser and installed editors
154      */

155     private I_CmsWidget getEditorWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
156
157         if (m_editorWidget == null) {
158             // get HTML widget to use from editor manager
159
String JavaDoc widgetClassName = OpenCms.getWorkplaceManager().getWorkplaceEditorManager().getWidgetEditor(
160                 cms.getRequestContext(),
161                 widgetDialog.getUserAgent());
162             boolean foundWidget = true;
163             if (CmsStringUtil.isEmpty(widgetClassName)) {
164                 // no installed widget found, use default text area to edit HTML value
165
widgetClassName = CmsTextareaWidget.class.getName();
166                 foundWidget = false;
167             }
168             try {
169                 if (foundWidget) {
170                     // get widget instance and set the widget configuration
171
Class JavaDoc widgetClass = Class.forName(widgetClassName);
172                     A_CmsHtmlWidget editorWidget = (A_CmsHtmlWidget)widgetClass.newInstance();
173                     editorWidget.setHtmlWidgetOption(getHtmlWidgetOption());
174                     m_editorWidget = editorWidget;
175                 } else {
176                     // set the text area to display 15 rows for editing
177
Class JavaDoc widgetClass = Class.forName(widgetClassName);
178                     I_CmsWidget editorWidget = (I_CmsWidget)widgetClass.newInstance();
179                     editorWidget.setConfiguration("15");
180                     m_editorWidget = editorWidget;
181                 }
182             } catch (Exception JavaDoc e) {
183                 // failed to create widget instance
184
LOG.error(Messages.get().container(Messages.LOG_CREATE_HTMLWIDGET_INSTANCE_FAILED_1, widgetClassName).key());
185             }
186
187         }
188         return m_editorWidget;
189     }
190 }
Popular Tags