001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.widgets; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.i18n.CmsEncoder; 033import org.opencms.i18n.CmsMessages; 034import org.opencms.main.OpenCms; 035import org.opencms.util.CmsStringUtil; 036 037import java.util.Locale; 038import java.util.Map; 039import java.util.Set; 040 041/** 042 * Base class for XML editor widgets.<p> 043 * 044 * @since 6.0.0 045 */ 046public abstract class A_CmsWidget implements I_CmsWidget { 047 048 /** Inner class to generate the I_CmsWidgetDialog. */ 049 public class CmsDummyWidgetDialog implements I_CmsWidgetDialog { 050 051 /** The locale of this widget. */ 052 private Locale m_locale; 053 054 /** The massage of this widget. */ 055 private CmsMessages m_message; 056 057 /** The resource being edited. */ 058 private CmsResource m_resource; 059 060 /** Constructor.<p> 061 * @param locale the locale of the dialog 062 * @param message the message of the dialog 063 */ 064 public CmsDummyWidgetDialog(Locale locale, CmsMessages message) { 065 066 m_locale = locale; 067 m_message = message; 068 } 069 070 /** 071 * @see org.opencms.widgets.I_CmsWidgetDialog#button(java.lang.String, java.lang.String, java.lang.String, java.lang.String, int) 072 */ 073 public String button(String href, String target, String image, String label, int type) { 074 075 return null; 076 } 077 078 /** 079 * @see org.opencms.widgets.I_CmsWidgetDialog#buttonBar(int) 080 */ 081 public String buttonBar(int segment) { 082 083 return null; 084 } 085 086 /** 087 * @see org.opencms.widgets.I_CmsWidgetDialog#buttonBarHorizontalLine() 088 */ 089 public String buttonBarHorizontalLine() { 090 091 return null; 092 } 093 094 /** 095 * @see org.opencms.widgets.I_CmsWidgetDialog#buttonBarSeparator(int, int) 096 */ 097 public String buttonBarSeparator(int leftPixel, int rightPixel) { 098 099 return null; 100 } 101 102 /** 103 * @see org.opencms.widgets.I_CmsWidgetDialog#buttonBarSpacer(int) 104 */ 105 public String buttonBarSpacer(int width) { 106 107 return null; 108 } 109 110 /** 111 * @see org.opencms.widgets.I_CmsWidgetDialog#buttonBarStartTab(int, int) 112 */ 113 public String buttonBarStartTab(int leftPixel, int rightPixel) { 114 115 return null; 116 } 117 118 /** 119 * @see org.opencms.widgets.I_CmsWidgetDialog#dialogHorizontalSpacer(int) 120 */ 121 public String dialogHorizontalSpacer(int width) { 122 123 return null; 124 } 125 126 /** 127 * @see org.opencms.widgets.I_CmsWidgetDialog#getButtonStyle() 128 */ 129 public int getButtonStyle() { 130 131 return 0; 132 } 133 134 /** 135 * @see org.opencms.widgets.I_CmsWidgetDialog#getHelpMessageIds() 136 */ 137 public Set<String> getHelpMessageIds() { 138 139 return null; 140 } 141 142 /** 143 * @see org.opencms.widgets.I_CmsWidgetDialog#getLocale() 144 */ 145 public Locale getLocale() { 146 147 return m_locale; 148 } 149 150 /** 151 * @see org.opencms.widgets.I_CmsWidgetDialog#getMessages() 152 */ 153 public CmsMessages getMessages() { 154 155 return m_message; 156 } 157 158 /** 159 * Gets the resource being edited. 160 * 161 * @return the resource being edited 162 */ 163 public CmsResource getResource() { 164 165 return m_resource; 166 } 167 168 /** 169 * @see org.opencms.widgets.I_CmsWidgetDialog#getUserAgent() 170 */ 171 public String getUserAgent() { 172 173 return null; 174 } 175 176 /** 177 * Sets the resource being edited.<p> 178 * 179 * @param resource the resource being edited 180 */ 181 public void setResource(CmsResource resource) { 182 183 m_resource = resource; 184 } 185 186 /** 187 * @see org.opencms.widgets.I_CmsWidgetDialog#useNewStyle() 188 */ 189 public boolean useNewStyle() { 190 191 return false; 192 } 193 194 } 195 196 /** Postfix for melp message locale. */ 197 public static final String HELP_POSTFIX = ".help"; 198 199 /** Prefix for message locales. */ 200 public static final String LABEL_PREFIX = "label."; 201 202 /** The configuration options of this widget. */ 203 private String m_configuration; 204 205 /** 206 * Default constructor.<p> 207 */ 208 protected A_CmsWidget() { 209 210 setConfiguration(""); 211 } 212 213 /** 214 * Constructor for preprocessing the configuration string.<p> 215 * 216 * @param configuration the configuration string 217 */ 218 protected A_CmsWidget(String configuration) { 219 220 setConfiguration(configuration); 221 } 222 223 /** 224 * Returns the localized help key for the provided widget parameter.<p> 225 * @param param the widget parameter to return the localized help key for 226 * 227 * @return the localized help key for the provided widget parameter 228 */ 229 public static String getHelpKey(I_CmsWidgetParameter param) { 230 231 // calculate the key 232 StringBuffer result = new StringBuffer(64); 233 result.append(LABEL_PREFIX); 234 result.append(param.getKey()); 235 result.append(HELP_POSTFIX); 236 237 return result.toString(); 238 } 239 240 /** 241 * Returns the localized label key for the provided widget parameter.<p> 242 * @param param the widget parameter to return the localized label key for 243 * 244 * @return the localized label key for the provided widget parameter 245 */ 246 public static String getLabelKey(I_CmsWidgetParameter param) { 247 248 // calculate the key 249 StringBuffer result = new StringBuffer(64); 250 result.append(LABEL_PREFIX); 251 result.append(param.getKey()); 252 return result.toString(); 253 } 254 255 /** 256 * @see java.lang.Object#equals(java.lang.Object) 257 */ 258 @Override 259 public boolean equals(Object obj) { 260 261 if (obj == this) { 262 return true; 263 } 264 if (obj instanceof A_CmsWidget) { 265 // widgets are equal if they use the same class 266 return getClass().getName().equals(obj.getClass().getName()); 267 } 268 return false; 269 } 270 271 /** 272 * Returns the configuration string.<p> 273 * 274 * @return the configuration string 275 */ 276 public String getConfiguration() { 277 278 return m_configuration; 279 } 280 281 /** 282 * @see org.opencms.widgets.I_CmsWidget#getDialogHtmlEnd(org.opencms.file.CmsObject, I_CmsWidgetDialog, I_CmsWidgetParameter) 283 */ 284 public String getDialogHtmlEnd(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter value) { 285 286 return getHelpText(widgetDialog, value); 287 } 288 289 /** 290 * @see org.opencms.widgets.I_CmsWidget#getDialogIncludes(org.opencms.file.CmsObject, I_CmsWidgetDialog) 291 */ 292 public String getDialogIncludes(CmsObject cms, I_CmsWidgetDialog widgetDialog) { 293 294 return ""; 295 } 296 297 /** 298 * @see org.opencms.widgets.I_CmsWidget#getDialogInitCall(org.opencms.file.CmsObject, I_CmsWidgetDialog) 299 */ 300 public String getDialogInitCall(CmsObject cms, I_CmsWidgetDialog widgetDialog) { 301 302 return ""; 303 } 304 305 /** 306 * @see org.opencms.widgets.I_CmsWidget#getDialogInitMethod(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog) 307 */ 308 public String getDialogInitMethod(CmsObject cms, I_CmsWidgetDialog widgetDialog) { 309 310 return ""; 311 } 312 313 /** 314 * @see org.opencms.widgets.I_CmsWidget#getHelpBubble(org.opencms.file.CmsObject, I_CmsWidgetDialog, I_CmsWidgetParameter) 315 */ 316 public String getHelpBubble(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) { 317 318 StringBuffer result = new StringBuffer(128); 319 String locKey = getHelpKey(param); 320 String locValue = widgetDialog.getMessages().key(locKey, true); 321 if (!widgetDialog.useNewStyle()) { 322 // use real ID for XML contents to avoid display issues 323 locKey = param.getId(); 324 } 325 if (locValue == null) { 326 // there was no help message found for this key, so return a spacer cell 327 return widgetDialog.dialogHorizontalSpacer(16); 328 } else { 329 result.append("<td>"); 330 result.append("<img id=\"img"); 331 result.append(locKey); 332 result.append("\" "); 333 result.append("title=\""); 334 result.append(CmsEncoder.escapeXml(locValue)); 335 result.append("\" "); 336 result.append("\" src=\""); 337 result.append(OpenCms.getLinkManager().substituteLink(cms, "/system/workplace/resources/commons/help.png")); 338 result.append("\" alt=\"\" border=\"0\""); 339 if (widgetDialog.useNewStyle()) { 340 // static divs are used in admin 341 result.append(getJsHelpMouseHandler(widgetDialog, locKey, null)); 342 } else { 343 // can't use method in CmsEncoder because we need to keep < > for HTML in help text 344 locValue = CmsStringUtil.substitute(locValue, "\"", """); 345 // dynamic help texts in xml content editor 346 result.append(getJsHelpMouseHandler(widgetDialog, locKey, CmsStringUtil.escapeJavaScript(locValue))); 347 } 348 result.append("></td>"); 349 return result.toString(); 350 } 351 } 352 353 /** 354 * @see org.opencms.widgets.I_CmsWidget#getHelpText(I_CmsWidgetDialog, I_CmsWidgetParameter) 355 */ 356 public String getHelpText(I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) { 357 358 String helpId = getHelpKey(param); 359 Set<String> helpIdsShown = widgetDialog.getHelpMessageIds(); 360 if (helpIdsShown.contains(helpId)) { 361 // help hey has already been included in output 362 return ""; 363 } 364 helpIdsShown.add(helpId); 365 366 // calculate the key 367 String locValue = widgetDialog.getMessages().key(helpId, true); 368 if (locValue == null) { 369 // there was no help message found for this key, so return an empty string 370 return ""; 371 } else { 372 if (widgetDialog.useNewStyle()) { 373 StringBuffer result = new StringBuffer(128); 374 result.append("<div class=\"help\" id=\"help"); 375 result.append(helpId); 376 result.append("\""); 377 result.append(getJsHelpMouseHandler(widgetDialog, helpId, helpId)); 378 result.append(">"); 379 result.append(locValue); 380 result.append("</div>\n"); 381 return result.toString(); 382 } else { 383 // create no static divs for xml content editor 384 return ""; 385 } 386 387 } 388 } 389 390 /** 391 * @see org.opencms.widgets.I_CmsWidget#getWidgetStringValue(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter) 392 */ 393 public String getWidgetStringValue(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) { 394 395 if (param != null) { 396 return param.getStringValue(cms); 397 } 398 return null; 399 } 400 401 /** 402 * @see java.lang.Object#hashCode() 403 */ 404 @Override 405 public int hashCode() { 406 407 return getClass().getName().hashCode(); 408 } 409 410 /** 411 * @see org.opencms.widgets.I_CmsWidget#isCompactViewEnabled() 412 */ 413 public boolean isCompactViewEnabled() { 414 415 return true; 416 } 417 418 /** 419 * @see org.opencms.widgets.I_CmsWidget#setConfiguration(java.lang.String) 420 */ 421 public void setConfiguration(String configuration) { 422 423 m_configuration = configuration; 424 } 425 426 /** 427 * @see org.opencms.widgets.I_CmsWidget#setEditorValue(org.opencms.file.CmsObject, java.util.Map, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter) 428 */ 429 public void setEditorValue( 430 CmsObject cms, 431 Map<String, String[]> formParameters, 432 I_CmsWidgetDialog widgetDialog, 433 I_CmsWidgetParameter param) { 434 435 String[] values = formParameters.get(param.getId()); 436 if ((values != null) && (values.length > 0)) { 437 param.setStringValue(cms, values[0]); 438 } 439 } 440 441 /** 442 * Returns the HTML for the JavaScript mouse handlers that show / hide the help text.<p> 443 * 444 * This is required since the handler differs between the "Dialog" and the "Administration" mode.<p> 445 * 446 * @param widgetDialog the dialog where the widget is displayed on 447 * @param key the key for the help bubble 448 * @param value the localized help text, has to be an escaped String for JS usage, is only used in XML content editor 449 * 450 * @return the HTML for the JavaScript mouse handlers that show / hide the help text 451 */ 452 protected String getJsHelpMouseHandler(I_CmsWidgetDialog widgetDialog, String key, String value) { 453 454 String jsShow; 455 String jsHide; 456 String keyHide; 457 if (widgetDialog.useNewStyle()) { 458 // Administration style 459 jsShow = "sMH"; 460 jsHide = "hMH"; 461 keyHide = "'" + key + "'"; 462 } else { 463 // Dialog style 464 jsShow = "showHelpText"; 465 jsHide = "hideHelpText"; 466 keyHide = ""; 467 } 468 StringBuffer result = new StringBuffer(128); 469 result.append(" onmouseover=\""); 470 result.append(jsShow); 471 result.append("('"); 472 result.append(key); 473 if (!widgetDialog.useNewStyle()) { 474 result.append("', '"); 475 result.append(value); 476 } 477 result.append("');\" onmouseout=\""); 478 result.append(jsHide); 479 result.append("("); 480 result.append(keyHide); 481 result.append(");\""); 482 483 return result.toString(); 484 } 485 486 /** 487 * Creates the tags to include external javascript files.<p> 488 * 489 * @param fileName the absolute path to the javascript file 490 * @return the tags to include external javascript files 491 */ 492 protected String getJSIncludeFile(String fileName) { 493 494 StringBuffer result = new StringBuffer(8); 495 result.append("<script src=\""); 496 result.append(fileName); 497 result.append("\"></script>"); 498 return result.toString(); 499 } 500}