001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/widgets/A_CmsWidget.java,v $
003: * Date : $Date: 2008-02-27 12:05:44 $
004: * Version: $Revision: 1.23 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.widgets;
033:
034: import org.opencms.file.CmsObject;
035: import org.opencms.main.OpenCms;
036: import org.opencms.util.CmsStringUtil;
037:
038: import java.util.Map;
039: import java.util.Set;
040:
041: /**
042: * Base class for XML editor widgets.<p>
043: *
044: * @author Alexander Kandzior
045: *
046: * @version $Revision: 1.23 $
047: *
048: * @since 6.0.0
049: */
050: public abstract class A_CmsWidget implements I_CmsWidget {
051:
052: /** Postfix for melp message locale. */
053: public static final String HELP_POSTFIX = ".help";
054:
055: /** Prefix for message locales. */
056: public static final String LABEL_PREFIX = "label.";
057:
058: /** The configuration options of this widget. */
059: private String m_configuration;
060:
061: /**
062: * Default constructor.<p>
063: */
064: protected A_CmsWidget() {
065:
066: setConfiguration("");
067: }
068:
069: /**
070: * Constructor for preprocessing the configuration string.<p>
071: *
072: * @param configuration the configuration string
073: */
074: protected A_CmsWidget(String configuration) {
075:
076: setConfiguration(configuration);
077: }
078:
079: /**
080: * Returns the localized help key for the provided widget parameter.<p>
081: * @param param the widget parameter to return the localized help key for
082: *
083: * @return the localized help key for the provided widget parameter
084: */
085: public static String getHelpKey(I_CmsWidgetParameter param) {
086:
087: // calculate the key
088: StringBuffer result = new StringBuffer(64);
089: result.append(LABEL_PREFIX);
090: result.append(param.getKey());
091: result.append(HELP_POSTFIX);
092:
093: return result.toString();
094: }
095:
096: /**
097: * Returns the localized label key for the provided widget parameter.<p>
098: * @param param the widget parameter to return the localized label key for
099: *
100: * @return the localized label key for the provided widget parameter
101: */
102: public static String getLabelKey(I_CmsWidgetParameter param) {
103:
104: // calculate the key
105: StringBuffer result = new StringBuffer(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 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()
122: .equals(obj.getClass().getName());
123: }
124: return false;
125: }
126:
127: /**
128: * Returns the configuration string.<p>
129: *
130: * @return the configuration string
131: */
132: public String getConfiguration() {
133:
134: return m_configuration;
135: }
136:
137: /**
138: * @see org.opencms.widgets.I_CmsWidget#getDialogHtmlEnd(org.opencms.file.CmsObject, I_CmsWidgetDialog, I_CmsWidgetParameter)
139: */
140: public String getDialogHtmlEnd(CmsObject cms,
141: I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter value) {
142:
143: return getHelpText(widgetDialog, value);
144: }
145:
146: /**
147: * @see org.opencms.widgets.I_CmsWidget#getDialogIncludes(org.opencms.file.CmsObject, I_CmsWidgetDialog)
148: */
149: public String getDialogIncludes(CmsObject cms,
150: I_CmsWidgetDialog widgetDialog) {
151:
152: return "";
153: }
154:
155: /**
156: * @see org.opencms.widgets.I_CmsWidget#getDialogInitCall(org.opencms.file.CmsObject, I_CmsWidgetDialog)
157: */
158: public String getDialogInitCall(CmsObject cms,
159: I_CmsWidgetDialog widgetDialog) {
160:
161: return "";
162: }
163:
164: /**
165: * @see org.opencms.widgets.I_CmsWidget#getDialogInitMethod(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
166: */
167: public String getDialogInitMethod(CmsObject cms,
168: I_CmsWidgetDialog widgetDialog) {
169:
170: return "";
171: }
172:
173: /**
174: * @see org.opencms.widgets.I_CmsWidget#getHelpBubble(org.opencms.file.CmsObject, I_CmsWidgetDialog, I_CmsWidgetParameter)
175: */
176: public String getHelpBubble(CmsObject cms,
177: I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
178:
179: StringBuffer result = new StringBuffer(128);
180: String locKey = getHelpKey(param);
181: String locValue = widgetDialog.getMessages().key(locKey, true);
182: if (!widgetDialog.useNewStyle()) {
183: // use real ID for XML contents to avoid display issues
184: locKey = param.getId();
185: }
186: if (locValue == null) {
187: // there was no help message found for this key, so return a spacer cell
188: return widgetDialog.dialogHorizontalSpacer(16);
189: } else {
190: result.append("<td>");
191: result.append("<img id=\"img");
192: result.append(locKey);
193: result.append("\" src=\"");
194: result.append(OpenCms.getLinkManager().substituteLink(cms,
195: "/system/workplace/resources/commons/help.png"));
196: result.append("\" alt=\"\" border=\"0\"");
197: if (widgetDialog.useNewStyle()) {
198: // static divs are used in admin
199: result.append(getJsHelpMouseHandler(widgetDialog,
200: locKey, null));
201: } else {
202: // can't use method in CmsEncoder because we need to keep < > for HTML in help text
203: locValue = CmsStringUtil.substitute(locValue, "\"",
204: """);
205: // dynamic help texts in xml content editor
206: result.append(getJsHelpMouseHandler(widgetDialog,
207: locKey, CmsStringUtil
208: .escapeJavaScript(locValue)));
209: }
210: result.append("></td>");
211: return result.toString();
212: }
213: }
214:
215: /**
216: * @see org.opencms.widgets.I_CmsWidget#getHelpText(I_CmsWidgetDialog, I_CmsWidgetParameter)
217: */
218: public String getHelpText(I_CmsWidgetDialog widgetDialog,
219: I_CmsWidgetParameter param) {
220:
221: String helpId = getHelpKey(param);
222: Set helpIdsShown = widgetDialog.getHelpMessageIds();
223: if (helpIdsShown.contains(helpId)) {
224: // help hey has already been included in output
225: return "";
226: }
227: helpIdsShown.add(helpId);
228:
229: // calculate the key
230: String locValue = widgetDialog.getMessages().key(helpId, true);
231: if (locValue == null) {
232: // there was no help message found for this key, so return an empty string
233: return "";
234: } else {
235: if (widgetDialog.useNewStyle()) {
236: StringBuffer result = new StringBuffer(128);
237: result.append("<div class=\"help\" id=\"help");
238: result.append(helpId);
239: result.append("\"");
240: result.append(getJsHelpMouseHandler(widgetDialog,
241: helpId, helpId));
242: result.append(">");
243: result.append(locValue);
244: result.append("</div>\n");
245: return result.toString();
246: } else {
247: // create no static divs for xml content editor
248: return "";
249: }
250:
251: }
252: }
253:
254: /**
255: * @see org.opencms.widgets.I_CmsWidget#getWidgetStringValue(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
256: */
257: public String getWidgetStringValue(CmsObject cms,
258: I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
259:
260: if (param != null) {
261: return param.getStringValue(cms);
262: }
263: return null;
264: }
265:
266: /**
267: * @see java.lang.Object#hashCode()
268: */
269: public int hashCode() {
270:
271: return getClass().getName().hashCode();
272: }
273:
274: /**
275: * @see org.opencms.widgets.I_CmsWidget#setConfiguration(java.lang.String)
276: */
277: public void setConfiguration(String configuration) {
278:
279: m_configuration = configuration;
280: }
281:
282: /**
283: * @see org.opencms.widgets.I_CmsWidget#setEditorValue(org.opencms.file.CmsObject, java.util.Map, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
284: */
285: public void setEditorValue(CmsObject cms, Map formParameters,
286: I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
287:
288: String[] values = (String[]) formParameters.get(param.getId());
289: if ((values != null) && (values.length > 0)) {
290: param.setStringValue(cms, values[0]);
291: }
292: }
293:
294: /**
295: * Returns the HTML for the JavaScript mouse handlers that show / hide the help text.<p>
296: *
297: * This is required since the handler differs between the "Dialog" and the "Administration" mode.<p>
298: *
299: * @param widgetDialog the dialog where the widget is displayed on
300: * @param key the key for the help bubble
301: * @param value the localized help text, has to be an escaped String for JS usage, is only used in XML content editor
302: *
303: * @return the HTML for the JavaScript mouse handlers that show / hide the help text
304: */
305: protected String getJsHelpMouseHandler(
306: I_CmsWidgetDialog widgetDialog, String key, String value) {
307:
308: String jsShow;
309: String jsHide;
310: String keyHide;
311: if (widgetDialog.useNewStyle()) {
312: // Administration style
313: jsShow = "sMH";
314: jsHide = "hMH";
315: keyHide = "'" + key + "'";
316: } else {
317: // Dialog style
318: jsShow = "showHelpText";
319: jsHide = "hideHelpText";
320: keyHide = "";
321: }
322: StringBuffer result = new StringBuffer(128);
323: result.append(" onmouseover=\"");
324: result.append(jsShow);
325: result.append("('");
326: result.append(key);
327: if (!widgetDialog.useNewStyle()) {
328: result.append("', '");
329: result.append(value);
330: }
331: result.append("');\" onmouseout=\"");
332: result.append(jsHide);
333: result.append("(");
334: result.append(keyHide);
335: result.append(");\"");
336:
337: return result.toString();
338: }
339:
340: /**
341: * Creates the tags to include external javascript files.<p>
342: *
343: * @param fileName the absolute path to the javascript file
344: * @return the tags to include external javascript files
345: */
346: protected String getJSIncludeFile(String fileName) {
347:
348: StringBuffer result = new StringBuffer(8);
349: result.append("<script type=\"text/javascript\" src=\"");
350: result.append(fileName);
351: result.append("\"></script>");
352: return result.toString();
353: }
354: }
|