001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (C) Alkacon Software (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, 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.acacia.shared.CmsContentDefinition; 031import org.opencms.ade.contenteditor.shared.CmsComplexWidgetData; 032import org.opencms.ade.contenteditor.shared.CmsExternalWidgetConfiguration; 033import org.opencms.file.CmsObject; 034import org.opencms.json.JSONObject; 035import org.opencms.util.CmsStringUtil; 036 037import java.util.ArrayList; 038import java.util.HashMap; 039import java.util.List; 040import java.util.Map; 041 042/** 043 * This is an abstract class which you can inherit from to relatively easily implement complex widgets for rendering nested contents in Javascript.<p> 044 * 045 * Subclasses of this class only need to implement the methods getScripts(), getStyleSheets(), getName(), and configure(). 046 * 047 * The name of the initialization function is generated by prepending the string "init_" to the widget name returned by getName(). 048 */ 049public abstract class A_CmsNativeComplexWidget implements I_CmsComplexWidget { 050 051 /** The prefix which is prepended to the widget name to get the name of the initialization function. */ 052 public static final String INIT_FUNCTION_PREFIX = "init_"; 053 054 /** The configuration string. */ 055 protected String m_configuration; 056 057 /** The configuration map created from the configuration string. */ 058 protected Map<String, String> m_configurationMap; 059 060 /** The configuration map in JSON format. */ 061 protected JSONObject m_jsonConfig; 062 063 /** 064 * @see org.opencms.widgets.I_CmsComplexWidget#getName() 065 * 066 * The result of this method will be used to determine the initialization function name for the widget by prepending 067 * the prefix "init_". For example, if "foo" is returned by this method, "init_foo" will be used as the initialization function 068 * name. 069 */ 070 public abstract String getName(); 071 072 /** 073 * Gets the list of URLs of scripts which the widget reuires.<p> 074 * 075 * @param cms the current CMS context 076 * 077 * @return the list of URLs of scripts needed by the widget 078 */ 079 public abstract List<String> getScripts(CmsObject cms); 080 081 /** 082 * Gets the list of URLs of stylesheets which are required by the widget.<p> 083 * 084 * @param cms the current CMS context 085 * 086 * @return the list of URLs of stylesheets needed by the widget 087 */ 088 public abstract List<String> getStyleSheets(CmsObject cms); 089 090 /** 091 * @see org.opencms.widgets.I_CmsComplexWidget#getWidgetData(org.opencms.file.CmsObject) 092 */ 093 public final CmsComplexWidgetData getWidgetData(CmsObject cms) { 094 095 List<String> scripts = new ArrayList<String>(getScripts(cms)); 096 List<String> styles = new ArrayList<String>(getStyleSheets(cms)); 097 String init = m_configurationMap.get(CmsContentDefinition.PARAM_INIT_CALL); 098 CmsExternalWidgetConfiguration config = new CmsExternalWidgetConfiguration(getName(), init, scripts, styles); 099 return new CmsComplexWidgetData(CmsContentDefinition.NATIVE_RENDERER, m_jsonConfig.toString(), config); 100 } 101 102 /** 103 * Initializes the configuration date from the given configuration string.<p> 104 * 105 * This should be called by subclasses of this class.<p> 106 * 107 * @param config the widget configuration string 108 */ 109 public final void initConfiguration(String config) { 110 111 m_configuration = config; 112 m_configurationMap = CmsStringUtil.splitAsMap(config, "|", ":"); 113 m_configurationMap.put(CmsContentDefinition.PARAM_INIT_CALL, INIT_FUNCTION_PREFIX + getName()); 114 m_jsonConfig = new JSONObject(new HashMap<String, Object>(m_configurationMap)); 115 } 116 117}