001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (C) Alkacon Software (https://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, please see the 018 * company website: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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.acacia.client; 029 030import org.opencms.acacia.client.widgets.CmsFormWidgetWrapper; 031import org.opencms.acacia.client.widgets.CmsStringWidget; 032import org.opencms.acacia.client.widgets.I_CmsEditWidget; 033import org.opencms.acacia.client.widgets.I_CmsFormEditWidget; 034import org.opencms.acacia.shared.CmsAttributeConfiguration; 035import org.opencms.acacia.shared.CmsContentDefinition; 036import org.opencms.acacia.shared.CmsType; 037import org.opencms.gwt.client.ui.input.form.I_CmsHasPlaceholder; 038import org.opencms.util.CmsStringUtil; 039 040import java.util.HashMap; 041import java.util.Map; 042 043import com.google.gwt.dom.client.Element; 044 045/** 046 * Service providing form widget renderer for entity attributes.<p> 047 */ 048public class CmsWidgetService implements I_CmsWidgetService { 049 050 /** The attribute configurations. */ 051 private Map<String, CmsAttributeConfiguration> m_attributeConfigurations; 052 053 /** The in-line renderer. */ 054 private I_CmsEntityRenderer m_defaultRenderer; 055 056 /** Renderers by attribute. */ 057 private Map<String, I_CmsEntityRenderer> m_rendererByAttribute = new HashMap<String, I_CmsEntityRenderer>(); 058 059 /** Map of renderer by type name. */ 060 private Map<String, I_CmsEntityRenderer> m_rendererByType; 061 062 /** Map of renderers by name. */ 063 private Map<String, I_CmsEntityRenderer> m_renderers = new HashMap<String, I_CmsEntityRenderer>(); 064 065 /** The registered widget factories. */ 066 private Map<String, I_CmsWidgetFactory> m_widgetFactories; 067 068 /** 069 * Constructor.<p> 070 */ 071 public CmsWidgetService() { 072 073 m_rendererByType = new HashMap<String, I_CmsEntityRenderer>(); 074 m_widgetFactories = new HashMap<String, I_CmsWidgetFactory>(); 075 m_attributeConfigurations = new HashMap<String, CmsAttributeConfiguration>(); 076 } 077 078 /** 079 * @see org.opencms.acacia.client.I_CmsWidgetService#addChangedOrderPath(java.lang.String) 080 */ 081 public void addChangedOrderPath(String valuePath) { 082 083 // not implemented 084 } 085 086 /** 087 * @see org.opencms.acacia.client.I_CmsWidgetService#addConfigurations(java.util.Map) 088 */ 089 public void addConfigurations(Map<String, CmsAttributeConfiguration> configurations) { 090 091 m_attributeConfigurations.putAll(configurations); 092 } 093 094 /** 095 * @see org.opencms.acacia.client.I_CmsWidgetService#addRenderer(org.opencms.acacia.client.I_CmsEntityRenderer) 096 */ 097 public void addRenderer(I_CmsEntityRenderer renderer) { 098 099 m_renderers.put(renderer.getName(), renderer); 100 } 101 102 /** 103 * Adds a renderer for the given type.<p> 104 * 105 * @param typeName the type name 106 * @param renderer the renderer 107 */ 108 public void addRenderer(String typeName, I_CmsEntityRenderer renderer) { 109 110 m_rendererByType.put(typeName, renderer); 111 } 112 113 /** 114 * @see org.opencms.acacia.client.I_CmsWidgetService#addWidgetFactory(java.lang.String, org.opencms.acacia.client.I_CmsWidgetFactory) 115 */ 116 public void addWidgetFactory(String widgetName, I_CmsWidgetFactory widgetFactory) { 117 118 m_widgetFactories.put(widgetName, widgetFactory); 119 } 120 121 /** 122 * @see org.opencms.acacia.client.I_CmsWidgetService#getAttributeFormWidget(java.lang.String) 123 */ 124 public I_CmsFormEditWidget getAttributeFormWidget(String attributeName) { 125 126 if (m_attributeConfigurations != null) { 127 CmsAttributeConfiguration config = m_attributeConfigurations.get(attributeName); 128 if (config != null) { 129 I_CmsWidgetFactory factory = m_widgetFactories.get(config.getWidgetName()); 130 if (factory != null) { 131 I_CmsFormEditWidget result = factory.createFormWidget(config.getWidgetConfig()); 132 if (result instanceof I_CmsHasPlaceholder) { 133 if (!CmsStringUtil.isEmpty(config.getPlaceholder())) { 134 ((I_CmsHasPlaceholder)result).setPlaceholder(config.getPlaceholder()); 135 } 136 } 137 return result; 138 } 139 } 140 } 141 // no configuration or widget factory found, return default string widget 142 return new CmsFormWidgetWrapper(new CmsStringWidget()); 143 } 144 145 /** 146 * @see org.opencms.acacia.client.I_CmsWidgetService#getAttributeHelp(java.lang.String) 147 */ 148 public String getAttributeHelp(String attributeName) { 149 150 if (m_attributeConfigurations != null) { 151 CmsAttributeConfiguration config = m_attributeConfigurations.get(attributeName); 152 if (config != null) { 153 return config.getHelp(); 154 } 155 } 156 return null; 157 } 158 159 /** 160 * @see org.opencms.acacia.client.I_CmsWidgetService#getAttributeInlineWidget(java.lang.String, com.google.gwt.dom.client.Element) 161 */ 162 public I_CmsEditWidget getAttributeInlineWidget(String attributeName, Element element) { 163 164 if (m_attributeConfigurations != null) { 165 CmsAttributeConfiguration config = m_attributeConfigurations.get(attributeName); 166 if (config != null) { 167 I_CmsWidgetFactory factory = m_widgetFactories.get(config.getWidgetName()); 168 if (factory != null) { 169 return factory.createInlineWidget(config.getWidgetConfig(), element); 170 } 171 } 172 } 173 // no widget configured 174 return null; 175 } 176 177 /** 178 * @see org.opencms.acacia.client.I_CmsWidgetService#getAttributeLabel(java.lang.String) 179 */ 180 public String getAttributeLabel(String attributeName) { 181 182 if (m_attributeConfigurations != null) { 183 CmsAttributeConfiguration config = m_attributeConfigurations.get(attributeName); 184 if (config != null) { 185 return config.getLabel(); 186 } 187 } 188 return attributeName; 189 } 190 191 /** 192 * @see org.opencms.acacia.client.I_CmsWidgetService#getDefaultAttributeValue(java.lang.String, java.lang.String) 193 */ 194 public String getDefaultAttributeValue(String attributeName, String simpleValuePath) { 195 196 CmsAttributeConfiguration config = m_attributeConfigurations.get(attributeName); 197 return (config != null) && (config.getDefaultValue() != null) ? config.getDefaultValue() : ""; 198 } 199 200 /** 201 * Gets the renderer instance for a specific attribute.<p> 202 * 203 * @param attributeName the attribute for which we want the renderer 204 * 205 * @return the renderer instance 206 */ 207 public I_CmsEntityRenderer getRendererForAttribute(String attributeName) { 208 209 int openCmsIndex = attributeName.indexOf("opencms://"); 210 if (openCmsIndex != -1) { 211 attributeName = attributeName.substring(openCmsIndex); 212 } 213 I_CmsEntityRenderer result = m_rendererByAttribute.get(attributeName); 214 if (result == null) { 215 return m_defaultRenderer; 216 } else { 217 return result; 218 } 219 } 220 221 /** 222 * @see org.opencms.acacia.client.I_CmsWidgetService#getRendererForAttribute(java.lang.String, org.opencms.acacia.shared.CmsType) 223 */ 224 public I_CmsEntityRenderer getRendererForAttribute(String attributeName, CmsType attributeType) { 225 226 return getRendererForAttribute(attributeName); 227 } 228 229 /** 230 * @see org.opencms.acacia.client.I_CmsWidgetService#getRendererForType(org.opencms.acacia.shared.CmsType) 231 */ 232 public I_CmsEntityRenderer getRendererForType(CmsType entityType) { 233 234 if (m_rendererByType.containsKey(entityType.getId())) { 235 return m_rendererByType.get(entityType.getId()); 236 } 237 return m_defaultRenderer; 238 } 239 240 /** 241 * Initializes the widget service with the given content definition.<p> 242 * 243 * @param definition the content definition 244 */ 245 public void init(CmsContentDefinition definition) { 246 247 m_attributeConfigurations = definition.getConfigurations(); 248 } 249 250 /** 251 * @see org.opencms.acacia.client.I_CmsWidgetService#isDisplayCompact(java.lang.String) 252 */ 253 public boolean isDisplayCompact(String attributeName) { 254 255 if (m_attributeConfigurations != null) { 256 CmsAttributeConfiguration config = m_attributeConfigurations.get(attributeName); 257 if (config != null) { 258 return config.isDisplayColumn(); 259 } 260 } 261 return false; 262 } 263 264 /** 265 * @see org.opencms.acacia.client.I_CmsWidgetService#isDisplaySingleLine(java.lang.String) 266 */ 267 public boolean isDisplaySingleLine(String attributeName) { 268 269 if (m_attributeConfigurations != null) { 270 CmsAttributeConfiguration config = m_attributeConfigurations.get(attributeName); 271 if (config != null) { 272 return config.isDisplaySingleLine(); 273 } 274 } 275 return false; 276 } 277 278 /** 279 * @see org.opencms.acacia.client.I_CmsWidgetService#isVisible(java.lang.String) 280 */ 281 public boolean isVisible(String attributeName) { 282 283 boolean result = true; 284 if (m_attributeConfigurations.containsKey(attributeName)) { 285 result = m_attributeConfigurations.get(attributeName).isVisible(); 286 } 287 return result; 288 } 289 290 /** 291 * @see org.opencms.acacia.client.I_CmsWidgetService#registerComplexWidgetAttribute(java.lang.String, java.lang.String, java.lang.String) 292 */ 293 public void registerComplexWidgetAttribute(String attrName, String rendererName, String configuration) { 294 295 I_CmsEntityRenderer renderer = m_renderers.get(rendererName); 296 if (renderer != null) { 297 renderer = renderer.configure(configuration); 298 m_rendererByAttribute.put(attrName, renderer); 299 } else { 300 log("Invalid entity renderer: " + rendererName); 301 } 302 } 303 304 /** 305 * Adds the default complex type renderer.<p> 306 * 307 * @param renderer the renderer 308 */ 309 public void setDefaultRenderer(I_CmsEntityRenderer renderer) { 310 311 m_defaultRenderer = renderer; 312 } 313 314 /** 315 * @see org.opencms.acacia.client.I_CmsWidgetService#setWidgetFactories(java.util.Map) 316 */ 317 public void setWidgetFactories(Map<String, I_CmsWidgetFactory> widgetFactories) { 318 319 m_widgetFactories = widgetFactories; 320 } 321 322 /** 323 * @see org.opencms.acacia.client.I_CmsWidgetService#shouldRemoveLastValueAfterUnfocus(org.opencms.acacia.client.widgets.I_CmsEditWidget) 324 */ 325 public boolean shouldRemoveLastValueAfterUnfocus(I_CmsEditWidget widget) { 326 327 return false; 328 } 329 330 /** 331 * Log method for debugging.<p> 332 * 333 * @param message the message to log 334 */ 335 private native void log(String message) /*-{ 336 if ($wnd.console) { 337 $wnd.console.log(message); 338 } 339 }-*/; 340 341}