KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/widgets/A_CmsWidget.java,v $
3  * Date : $Date: 2006/05/19 08:34:36 $
4  * Version: $Revision: 1.20 $
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.main.OpenCms;
36 import org.opencms.util.CmsStringUtil;
37
38 import java.util.Map JavaDoc;
39 import java.util.Set JavaDoc;
40
41 /**
42  * Base class for XML editor widgets.<p>
43  *
44  * @author Alexander Kandzior
45  *
46  * @version $Revision: 1.20 $
47  *
48  * @since 6.0.0
49  */

50 public abstract class A_CmsWidget implements I_CmsWidget {
51
52     /** Postfix for melp message locale. */
53     public static final String JavaDoc HELP_POSTFIX = ".help";
54
55     /** Prefix for message locales. */
56     public static final String JavaDoc LABEL_PREFIX = "label.";
57
58     /** The configuration options of this widget. */
59     private String JavaDoc m_configuration;
60
61     /**
62      * Default constructor.<p>
63      */

64     protected A_CmsWidget() {
65
66         setConfiguration("");
67     }
68
69     /**
70      * Constructor for preprocessing the configuration string.<p>
71      *
72      * @param configuration the configuration string
73      */

74     protected A_CmsWidget(String JavaDoc configuration) {
75
76         setConfiguration(configuration);
77     }
78
79     /**
80      * Returns the localized help key for the provided widget parameter.<p>
81      * @param param the widget parameter to return the localized help key for
82      *
83      * @return the localized help key for the provided widget parameter
84      */

85     public static String JavaDoc getHelpKey(I_CmsWidgetParameter param) {
86
87         // calculate the key
88
StringBuffer JavaDoc result = new StringBuffer JavaDoc(64);
89         result.append(LABEL_PREFIX);
90         result.append(param.getKey());
91         result.append(HELP_POSTFIX);
92
93         return result.toString();
94     }
95
96     /**
97      * Returns the localized label key for the provided widget parameter.<p>
98      * @param param the widget parameter to return the localized label key for
99      *
100      * @return the localized label key for the provided widget parameter
101      */

102     public static String JavaDoc getLabelKey(I_CmsWidgetParameter param) {
103
104         // calculate the key
105
StringBuffer JavaDoc result = new StringBuffer JavaDoc(64);
106         result.append(LABEL_PREFIX);
107         result.append(param.getKey());
108         return result.toString();
109     }
110
111     /**
112      * @see java.lang.Object#equals(java.lang.Object)
113      */

114     public boolean equals(Object JavaDoc obj) {
115
116         if (obj == this) {
117             return true;
118         }
119         if (obj instanceof A_CmsWidget) {
120             // widgets are equal if they use the same class
121
return getClass().getName().equals(obj.getClass().getName());
122         }
123         return false;
124     }
125
126     /**
127      * Returns the configuration string.<p>
128      *
129      * @return the configuration string
130      */

131     public String JavaDoc getConfiguration() {
132
133         return m_configuration;
134     }
135
136     /**
137      * @see org.opencms.widgets.I_CmsWidget#getDialogHtmlEnd(org.opencms.file.CmsObject, I_CmsWidgetDialog, I_CmsWidgetParameter)
138      */

139     public String JavaDoc getDialogHtmlEnd(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter value) {
140
141         return getHelpText(widgetDialog, value);
142     }
143
144     /**
145      * @see org.opencms.widgets.I_CmsWidget#getDialogIncludes(org.opencms.file.CmsObject, I_CmsWidgetDialog)
146      */

147     public String JavaDoc getDialogIncludes(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
148
149         return "";
150     }
151
152     /**
153      * @see org.opencms.widgets.I_CmsWidget#getDialogInitCall(org.opencms.file.CmsObject, I_CmsWidgetDialog)
154      */

155     public String JavaDoc getDialogInitCall(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
156
157         return "";
158     }
159
160     /**
161      * @see org.opencms.widgets.I_CmsWidget#getDialogInitMethod(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
162      */

163     public String JavaDoc getDialogInitMethod(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
164
165         return "";
166     }
167
168     /**
169      * @see org.opencms.widgets.I_CmsWidget#getHelpBubble(org.opencms.file.CmsObject, I_CmsWidgetDialog, I_CmsWidgetParameter)
170      */

171     public String JavaDoc getHelpBubble(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
172
173         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
174         String JavaDoc locKey = getHelpKey(param);
175         String JavaDoc locValue = widgetDialog.getMessages().key(locKey, true);
176         if (!widgetDialog.useNewStyle()) {
177             // use real ID for XML contents to avoid display issues
178
locKey = param.getId();
179         }
180         if (locValue == null) {
181             // there was no help message found for this key, so return a spacer cell
182
return widgetDialog.dialogHorizontalSpacer(16);
183         } else {
184             result.append("<td>");
185             result.append("<img id=\"img");
186             result.append(locKey);
187             result.append("\" SRC=\"");
188             result.append(OpenCms.getLinkManager().substituteLink(cms, "/system/workplace/resources/commons/help.png"));
189             result.append("\" alt=\"\" border=\"0\"");
190             if (widgetDialog.useNewStyle()) {
191                 // static divs are used in admin
192
result.append(getJsHelpMouseHandler(widgetDialog, locKey, null));
193             } else {
194                 // can't use method in CmsEncoder because we need to keep < > for HTML in help text
195
locValue = CmsStringUtil.substitute(locValue, "\"", "&quot;");
196                 // dynamic help texts in xml content editor
197
result.append(getJsHelpMouseHandler(widgetDialog, locKey, CmsStringUtil.escapeJavaScript(locValue)));
198             }
199             result.append("></td>");
200             return result.toString();
201         }
202     }
203
204     /**
205      * @see org.opencms.widgets.I_CmsWidget#getHelpText(I_CmsWidgetDialog, I_CmsWidgetParameter)
206      */

207     public String JavaDoc getHelpText(I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
208
209         String JavaDoc helpId = getHelpKey(param);
210         Set JavaDoc helpIdsShown = widgetDialog.getHelpMessageIds();
211         if (helpIdsShown.contains(helpId)) {
212             // help hey has already been included in output
213
return "";
214         }
215         helpIdsShown.add(helpId);
216
217         // calculate the key
218
String JavaDoc locValue = widgetDialog.getMessages().key(helpId, true);
219         if (locValue == null) {
220             // there was no help message found for this key, so return an empty string
221
return "";
222         } else {
223             if (widgetDialog.useNewStyle()) {
224                 StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
225                 result.append("<div class=\"help\" id=\"help");
226                 result.append(helpId);
227                 result.append("\"");
228                 result.append(getJsHelpMouseHandler(widgetDialog, helpId, helpId));
229                 result.append(">");
230                 result.append(locValue);
231                 result.append("</div>\n");
232                 return result.toString();
233             } else {
234                 // create no static divs for xml content editor
235
return "";
236             }
237
238         }
239     }
240
241     /**
242      * @see org.opencms.widgets.I_CmsWidget#getWidgetStringValue(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
243      */

244     public String JavaDoc getWidgetStringValue(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
245
246         if (param != null) {
247             return param.getStringValue(cms);
248         }
249         return null;
250     }
251
252     /**
253      * @see java.lang.Object#hashCode()
254      */

255     public int hashCode() {
256
257         return getClass().getName().hashCode();
258     }
259
260     /**
261      * @see org.opencms.widgets.I_CmsWidget#setConfiguration(java.lang.String)
262      */

263     public void setConfiguration(String JavaDoc configuration) {
264
265         m_configuration = configuration;
266     }
267
268     /**
269      * @see org.opencms.widgets.I_CmsWidget#setEditorValue(org.opencms.file.CmsObject, java.util.Map, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
270      */

271     public void setEditorValue(
272         CmsObject cms,
273         Map JavaDoc formParameters,
274         I_CmsWidgetDialog widgetDialog,
275         I_CmsWidgetParameter param) {
276
277         String JavaDoc[] values = (String JavaDoc[])formParameters.get(param.getId());
278         if ((values != null) && (values.length > 0)) {
279             param.setStringValue(cms, values[0]);
280         }
281     }
282
283     /**
284      * Returns the HTML for the JavaScript mouse handlers that show / hide the help text.<p>
285      *
286      * This is required since the handler differs between the "Dialog" and the "Administration" mode.<p>
287      *
288      * @param widgetDialog the dialog where the widget is displayed on
289      * @param key the key for the help bubble
290      * @param value the localized help text, has to be an escaped String for JS usage, is only used in XML content editor
291      *
292      * @return the HTML for the JavaScript mouse handlers that show / hide the help text
293      */

294     protected String JavaDoc getJsHelpMouseHandler(I_CmsWidgetDialog widgetDialog, String JavaDoc key, String JavaDoc value) {
295
296         String JavaDoc jsShow;
297         String JavaDoc jsHide;
298         String JavaDoc keyHide;
299         if (widgetDialog.useNewStyle()) {
300             // Administration style
301
jsShow = "sMH";
302             jsHide = "hMH";
303             keyHide = "'" + key + "'";
304         } else {
305             // Dialog style
306
jsShow = "showHelpText";
307             jsHide = "hideHelpText";
308             keyHide = "";
309         }
310         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
311         result.append(" onmouseover=\"");
312         result.append(jsShow);
313         result.append("('");
314         result.append(key);
315         if (!widgetDialog.useNewStyle()) {
316             result.append("', '");
317             result.append(value);
318         }
319         result.append("');\" onmouseout=\"");
320         result.append(jsHide);
321         result.append("(");
322         result.append(keyHide);
323         result.append(");\"");
324
325         return result.toString();
326     }
327
328     /**
329      * Creates the tags to include external javascript files.<p>
330      *
331      * @param fileName the absolute path to the javascript file
332      * @return the tags to include external javascript files
333      */

334     protected String JavaDoc getJSIncludeFile(String JavaDoc fileName) {
335
336         StringBuffer JavaDoc result = new StringBuffer JavaDoc(8);
337         result.append("<script type=\"text/javascript\" SRC=\"");
338         result.append(fileName);
339         result.append("\"></script>");
340         return result.toString();
341     }
342 }
Popular Tags