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.json.JSONArray; 035import org.opencms.json.JSONException; 036import org.opencms.json.JSONObject; 037import org.opencms.util.CmsStringUtil; 038import org.opencms.workplace.CmsWorkplace; 039import org.opencms.workplace.galleries.A_CmsAjaxGallery; 040import org.opencms.xml.content.I_CmsXmlContentHandler.DisplayType; 041import org.opencms.xml.types.A_CmsXmlContentValue; 042 043import java.util.List; 044import java.util.Locale; 045 046/** 047 * Base class for all gallery widget implementations.<p> 048 * 049 * @since 6.0.0 050 */ 051public abstract class A_CmsGalleryWidget extends A_CmsWidget implements I_CmsADEWidget { 052 053 /** 054 * Creates a new gallery widget.<p> 055 */ 056 protected A_CmsGalleryWidget() { 057 058 // empty constructor is required for class registration 059 this(""); 060 } 061 062 /** 063 * Creates a new gallery widget with the given configuration.<p> 064 * 065 * @param configuration the configuration to use 066 */ 067 protected A_CmsGalleryWidget(String configuration) { 068 069 super(configuration); 070 } 071 072 /** 073 * @see org.opencms.widgets.I_CmsADEWidget#getConfiguration(org.opencms.file.CmsObject, org.opencms.xml.types.A_CmsXmlContentValue, org.opencms.i18n.CmsMessages, org.opencms.file.CmsResource, java.util.Locale) 074 */ 075 public String getConfiguration( 076 CmsObject cms, 077 A_CmsXmlContentValue schemaType, 078 CmsMessages messages, 079 CmsResource resource, 080 Locale contentLocale) { 081 082 CmsGalleryWidgetConfiguration config = new CmsGalleryWidgetConfiguration( 083 cms, 084 messages, 085 schemaType, 086 getConfiguration()); 087 JSONObject linkGalleryInfo = new JSONObject(); 088 try { 089 linkGalleryInfo.put("startupfolder", config.getStartup()); 090 linkGalleryInfo.put("startuptype", config.getType()); 091 linkGalleryInfo.put("editedresource", resource.getRootPath()); 092 } catch (JSONException e) { 093 // TODO: Auto-generated catch block 094 e.printStackTrace(); 095 } 096 097 return "¶ms=" + linkGalleryInfo.toString(); 098 } 099 100 /** 101 * @see org.opencms.widgets.I_CmsADEWidget#getCssResourceLinks(org.opencms.file.CmsObject) 102 */ 103 public List<String> getCssResourceLinks(CmsObject cms) { 104 105 // TODO: Auto-generated method stub 106 return null; 107 } 108 109 /** 110 * @see org.opencms.widgets.I_CmsADEWidget#getDefaultDisplayType() 111 */ 112 public DisplayType getDefaultDisplayType() { 113 114 return DisplayType.wide; 115 } 116 117 /** 118 * @see org.opencms.widgets.I_CmsWidget#getDialogIncludes(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog) 119 */ 120 @Override 121 public String getDialogIncludes(CmsObject cms, I_CmsWidgetDialog widgetDialog) { 122 123 StringBuffer result = new StringBuffer(256); 124 // import the JavaScript for the gallery widget 125 result.append( 126 getJSIncludeFile(CmsWorkplace.getSkinUri() + "components/widgets/" + getNameLower() + "gallery.js")); 127 return result.toString(); 128 } 129 130 /** 131 * @see org.opencms.widgets.I_CmsWidget#getDialogInitCall(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog) 132 */ 133 @Override 134 public String getDialogInitCall(CmsObject cms, I_CmsWidgetDialog widgetDialog) { 135 136 return "\tinit" + getNameUpper() + "Gallery();\n"; 137 } 138 139 /** 140 * @see org.opencms.widgets.I_CmsWidget#getDialogInitMethod(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog) 141 */ 142 @Override 143 public String getDialogInitMethod(CmsObject cms, I_CmsWidgetDialog widgetDialog) { 144 145 StringBuffer result = new StringBuffer(16); 146 result.append("function init"); 147 result.append(getNameUpper()); 148 result.append("Gallery() {\n"); 149 result.append("\t"); 150 result.append(getNameLower()); 151 result.append("GalleryPath = '"); 152 // path to download/image/link/html/table gallery 153 result.append(A_CmsAjaxGallery.PATH_GALLERIES); 154 result.append(getNameLower()); 155 result.append("gallery/index.jsp?"); 156 result.append("';\n"); 157 result.append("}\n"); 158 return result.toString(); 159 } 160 161 /** 162 * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter) 163 */ 164 public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) { 165 166 String id = param.getId(); 167 long idHash = id.hashCode(); 168 if (idHash < 0) { 169 // negative hash codes will not work as JS variable names, so convert them 170 idHash = -idHash; 171 // add 2^32 to the value to ensure that it is unique 172 idHash += 4294967296L; 173 } 174 StringBuffer result = new StringBuffer(128); 175 result.append("<td class=\"xmlTd\">"); 176 result.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td class=\"xmlTd\">"); 177 result.append("<input class=\"xmlInput textInput"); 178 if (param.hasError()) { 179 result.append(" xmlInputError"); 180 } 181 result.append("\" value=\""); 182 String value = param.getStringValue(cms); 183 result.append(value); 184 result.append("\" name=\""); 185 result.append(id); 186 result.append("\" id=\""); 187 result.append(id); 188 result.append("\" onkeyup=\"checkPreview('"); 189 result.append(id); 190 result.append("');\"></td>"); 191 result.append(widgetDialog.dialogHorizontalSpacer(10)); 192 result.append( 193 "<td><table class=\"editorbuttonbackground\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>"); 194 195 result.append( 196 widgetDialog.button( 197 "javascript:open" 198 + getNameUpper() 199 + "Gallery('" 200 + A_CmsAjaxGallery.MODE_WIDGET 201 + "', '" 202 + id 203 + "', '" 204 + idHash 205 + "');return false;", 206 null, 207 getNameLower() + "gallery", 208 Messages.getButtonName(getNameLower()), 209 widgetDialog.getButtonStyle())); 210 // create preview button 211 String previewClass = "hide"; 212 if (showPreview(value)) { 213 // show button if preview is enabled 214 previewClass = "show"; 215 } 216 result.append("<td class=\""); 217 result.append(previewClass); 218 result.append("\" id=\"preview"); 219 result.append(id); 220 result.append("\">"); 221 result.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>"); 222 result.append( 223 widgetDialog.button( 224 "javascript:preview" + getNameUpper() + "('" + id + "');return false;", 225 null, 226 "preview.png", 227 Messages.GUI_BUTTON_PREVIEW_0, 228 widgetDialog.getButtonStyle())); 229 result.append("</tr></table>"); 230 231 result.append("</td></tr></table>"); 232 233 result.append("</td>"); 234 result.append("</tr></table>"); 235 236 result.append("</td>"); 237 238 if (getNameLower().equals("image")) { 239 // reads the configuration String for this widget 240 CmsVfsImageWidgetConfiguration configuration = new CmsVfsImageWidgetConfiguration( 241 cms, 242 widgetDialog.getMessages(), 243 param, 244 getConfiguration()); 245 246 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(configuration.getStartup())) { 247 result.append("\n<script >"); 248 result.append("\nvar startupFolder").append(idHash).append(" = \"").append( 249 configuration.getStartup()).append("\";"); 250 result.append("\nvar startupType").append(idHash).append(" = \"").append( 251 configuration.getType()).append("\";"); 252 result.append("\n</script>"); 253 } else { 254 result.append("\n<script >"); 255 result.append("\nvar startupFolder").append(idHash).append(" = null;"); 256 result.append("\nvar startupType").append(idHash).append(" = null;"); 257 result.append("\n</script>"); 258 } 259 260 //This part is not used in javascript for now 261 if (configuration.isShowFormat()) { 262 // create hidden field to store the matching image format value 263 result.append("\n<script >"); 264 JSONArray formatsJson = new JSONArray(configuration.getFormatValues()); 265 result.append("\nvar imgFmts").append(idHash).append(" = ").append(formatsJson).append(";"); 266 result.append("\nvar imgFmtNames").append(idHash).append(" = \"").append( 267 CmsEncoder.escape(configuration.getSelectFormatString(), CmsEncoder.ENCODING_UTF_8)).append("\";"); 268 result.append("\nvar useFmts").append(idHash).append(" = true;"); 269 result.append("\n</script>"); 270 } else { 271 result.append("\n<script >"); 272 result.append("\nvar useFmts").append(idHash).append(" = false;"); 273 result.append("\nvar imgFmts").append(idHash).append(" = null;"); 274 result.append("\nvar imgFmtNames").append(idHash).append(" = null;"); 275 result.append("\n</script>"); 276 } 277 } else { // for download, link, html or table galleries 278 // reads the configuration String for this widget 279 CmsGalleryWidgetConfiguration configuration = new CmsGalleryWidgetConfiguration( 280 cms, 281 widgetDialog.getMessages(), 282 param, 283 getConfiguration()); 284 285 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(configuration.getStartup())) { 286 result.append("\n<script >"); 287 result.append("\nvar startupFolder").append(idHash).append(" = \"").append( 288 configuration.getStartup()).append("\";"); 289 result.append("\nvar startupType").append(idHash).append(" = \"").append( 290 configuration.getType()).append("\";"); 291 result.append("\n</script>"); 292 } else { 293 result.append("\n<script >"); 294 result.append("\nvar startupFolder").append(idHash).append(" = null;"); 295 result.append("\nvar startupType").append(idHash).append(" = null;"); 296 result.append("\n</script>"); 297 } 298 } 299 300 return result.toString(); 301 } 302 303 /** 304 * @see org.opencms.widgets.I_CmsADEWidget#getInitCall() 305 */ 306 public String getInitCall() { 307 308 return null; 309 } 310 311 /** 312 * @see org.opencms.widgets.I_CmsADEWidget#getJavaScriptResourceLinks(org.opencms.file.CmsObject) 313 */ 314 public List<String> getJavaScriptResourceLinks(CmsObject cms) { 315 316 return null; 317 } 318 319 /** 320 * Returns the lower case name of the gallery, for example <code>"html"</code>.<p> 321 * 322 * @return the lower case name of the gallery 323 */ 324 public abstract String getNameLower(); 325 326 /** 327 * Returns the upper case name of the gallery, for example <code>"Html"</code>.<p> 328 * 329 * @return the upper case name of the gallery 330 */ 331 public abstract String getNameUpper(); 332 333 /** 334 * @see org.opencms.widgets.I_CmsADEWidget#getWidgetName() 335 */ 336 public String getWidgetName() { 337 338 return A_CmsGalleryWidget.class.getName(); 339 } 340 341 /** 342 * @see org.opencms.widgets.A_CmsWidget#isCompactViewEnabled() 343 */ 344 @Override 345 public boolean isCompactViewEnabled() { 346 347 return false; 348 } 349 350 /** 351 * @see org.opencms.widgets.I_CmsADEWidget#isInternal() 352 */ 353 public boolean isInternal() { 354 355 return true; 356 } 357 358 /** 359 * Returns <code>true</code> if the preview button should be shown.<p> 360 * 361 * @param value the current widget value 362 * @return <code>true</code> if the preview button should be shown 363 */ 364 public abstract boolean showPreview(String value); 365 366}